Isolation of tests with external services is a mandatory condition for reliable automation.
Background: Early automation systems "struggled" with external APIs and databases that were often either unavailable or returned unexpected data. Without isolation of automated tests, it is impossible to reproduce the result: flakiness, failures due to external issues, and random crashes.
Problem:
Solution:
Use of "mocks" and "stubs" — local placeholders that simulate responses from external APIs. Popular ones include WireMock (Java), httpmock (Python), MockServer, TestContainers.
Emulation of a database using in-memory solutions or fixtures that are cleaned and repopulated before each test.
Moving test data IDs to variables so that tests can run in parallel without "overlapping" each other.
import requests BASE_URL = "http://localhost:1080/api" def test_order_creation(): mock_response = {"orderId": 12345, "state": "created"} # In actual tests, the response would be returned by the mock server # Here, the requests.post call and assert ...
Key features:
Is it necessary to conduct integration tests through real services on every run?
No. Mocks/stubs can be used regularly, while integration tests can be run separately, less frequently, and under control.
Will tests with real external APIs always yield more reliable results?
No. On the contrary, they are less stable and may fail due to changes on the partner's side. Constant flaky tests degrade the quality of the pipeline.
Can the same test data be used for parallel automated tests with external services?
No. This may lead to collisions, "races," and instability. Identifiers and state must be unique for each test/thread.
The company decided to run all automated tests on real third-party APIs (payment gateway) for speed. Several times, tests were banned, limits were hit, access had to be restored, data "leaked" into real reports, and false positives emerged.
Pros:
Cons:
Set up MockServer and a dummy in-memory database. Before each test, the state was reset, and data was unique. Real integration tests were run separately and less frequently.
Pros:
Cons: