You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 723ca2e
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: tests/README.md
+50-11Lines changed: 50 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,30 +2,41 @@
2
2
3
3
The tests have two levels:
4
4
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.
6
6
-[Integration tests](#integration-tests) (`tests/integration`) call the real Apify API.
7
7
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`.
9
9
10
10
## Unit tests
11
11
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.
13
13
14
14
### Running unit tests
15
15
16
16
```bash
17
17
uv run poe unit-tests
18
18
```
19
19
20
-
### Mocking HTTP
20
+
To run a single test:
21
21
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:
@@ -34,10 +45,10 @@ Integration tests communicate with the real Apify API.
34
45
35
46
### Running integration tests
36
47
37
-
The tests need valid API tokens in these environment variables:
48
+
The tests need API tokens of two test users:
38
49
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.
41
52
42
53
```bash
43
54
export APIFY_TEST_USER_API_TOKEN=<your-token>
@@ -46,3 +57,31 @@ uv run poe integration-tests
46
57
```
47
58
48
59
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:
- 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