Skip to content

Commit 723ca2e

Browse files
committed
docs: describe the test fixtures and helpers in tests/README.md
1 parent 035f7a4 commit 723ca2e

1 file changed

Lines changed: 50 additions & 11 deletions

File tree

‎tests/README.md‎

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,41 @@
22

33
The tests have two levels:
44

5-
- [Unit tests](#unit-tests) (`tests/unit`) are isolated from the Apify API.
5+
- [Unit tests](#unit-tests) (`tests/unit`) run against a local HTTP server and need no credentials or network access.
66
- [Integration tests](#integration-tests) (`tests/integration`) call the real Apify API.
77

8-
A test that calls the Apify API is an integration test. Every other test is a unit test.
8+
A test that calls the Apify API is an integration test. Every other test is a unit test. Helpers shared by both levels live in `tests/_utils.py`.
99

1010
## Unit tests
1111

12-
Unit tests are fully isolated from the real Apify API. They use mocks, fakes, or a local HTTP test server, and they need no API tokens and no network access.
12+
Unit tests are fully isolated from the real Apify API. They use mocks, fakes, or a local HTTP server, so they need no API tokens and no network access.
1313

1414
### Running unit tests
1515

1616
```bash
1717
uv run poe unit-tests
1818
```
1919

20-
### Mocking HTTP
20+
To run a single test:
2121

22-
Mock HTTP with the `httpserver` fixture from `pytest-httpserver`:
22+
```bash
23+
uv run pytest tests/unit/<path>.py::<test_name>
24+
```
25+
26+
### Unit test fixtures
27+
28+
- `httpserver`: a local HTTP server (from `pytest-httpserver`) on `127.0.0.1`, shared by the session and cleared after each test.
29+
- `sync_client` and `async_client`: an `ApifyClient` and an `ApifyClientAsync` pointed at `httpserver`.
30+
- `http_client_classes`: runs the test once per built-in HTTP transport (`impit` and `httpx2`) and makes it the default transport the clients construct. A module opts all its tests in with `pytestmark = pytest.mark.usefixtures('http_client_classes')`. Tests that construct a transport directly use `http_client_class` and `http_client_async_class`.
31+
32+
### Mocking the API
33+
34+
Register the expected requests on `httpserver` and point the client at it:
2335

2436
```python
25-
def test_example(httpserver: HTTPServer) -> None:
37+
def test_example(httpserver: HTTPServer, sync_client: ApifyClient) -> None:
2638
httpserver.expect_request('/v2/endpoint').respond_with_json({'data': ...})
27-
client = ApifyClient(token='test', api_url=httpserver.url_for('/').removesuffix('/'))
28-
# assert client behavior
39+
# call sync_client and assert its behavior
2940
```
3041

3142
## Integration tests
@@ -34,10 +45,10 @@ Integration tests communicate with the real Apify API.
3445

3546
### Running integration tests
3647

37-
The tests need valid API tokens in these environment variables:
48+
The tests need API tokens of two test users:
3849

39-
- `APIFY_TEST_USER_API_TOKEN`
40-
- `APIFY_TEST_USER_2_API_TOKEN`, for the permission-related tests
50+
- `APIFY_TEST_USER_API_TOKEN`: the main test user.
51+
- `APIFY_TEST_USER_2_API_TOKEN`: a second user, who owns the storages for the permission tests.
4152

4253
```bash
4354
export APIFY_TEST_USER_API_TOKEN=<your-token>
@@ -46,3 +57,31 @@ uv run poe integration-tests
4657
```
4758

4859
To run them against a different Apify API environment, also set `APIFY_INTEGRATION_TESTS_API_URL`.
60+
61+
### Integration test fixtures
62+
63+
- `client`: an `ApifyClient` or `ApifyClientAsync` for the main test user. Each test that uses it runs four times, once for each combination of sync or async client and `impit` or `httpx2` transport.
64+
- `is_async`: `True` when the test runs with the async client.
65+
- `test_dataset_of_another_user` and `test_kvs_of_another_user`: a dataset and a key-value store owned by the second user, with known content and the signatures for accessing it.
66+
67+
### Writing integration tests
68+
69+
One test function covers both clients. Wrap each client call in `maybe_await`, which awaits the result of the async client and returns the result of the sync one as is:
70+
71+
```python
72+
async def test_dataset_get_or_create(client: ApifyClient | ApifyClientAsync) -> None:
73+
"""A dataset created by name can be retrieved by its ID."""
74+
dataset_name = get_random_resource_name('dataset')
75+
dataset = await maybe_await(client.datasets().get_or_create(name=dataset_name))
76+
dataset_client = client.dataset(dataset.id)
77+
78+
try:
79+
retrieved = await maybe_await(dataset_client.get())
80+
assert retrieved is not None
81+
assert retrieved.name == dataset_name
82+
finally:
83+
await maybe_await(dataset_client.delete())
84+
```
85+
86+
- Name the resources a test creates with `get_random_resource_name`, so parallel test runs don't collide, and delete them in a `finally` block.
87+
- The API is eventually consistent, so a resource you just created or changed may take a moment to show up. Poll with `poll_until_condition`, or with `collect_iterate_until_present` for `iterate()` listings, instead of sleeping for a fixed time. Where a test has to sleep, use `maybe_sleep`, which blocks only for the sync client.

0 commit comments

Comments
 (0)