Skip to content

Commit d189059

Browse files
committed
test: Fix integration test isolation
1 parent 28f08d8 commit d189059

File tree

2 files changed

+63
-70
lines changed

2 files changed

+63
-70
lines changed

tests/integration/apify_api/conftest.py

Lines changed: 0 additions & 70 deletions
This file was deleted.

tests/integration/conftest.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
from __future__ import annotations
22

33
import os
4+
from typing import TYPE_CHECKING
45

56
import pytest
67

78
from apify_client import ApifyClientAsync
9+
from apify_shared.consts import ApifyEnvVars
10+
from crawlee import service_locator
11+
12+
import apify._actor
13+
from apify.storage_clients._apify._alias_resolving import AliasResolver
14+
15+
if TYPE_CHECKING:
16+
from collections.abc import Callable
17+
from pathlib import Path
818

919
_TOKEN_ENV_VAR = 'APIFY_TEST_USER_API_TOKEN'
1020
_API_URL_ENV_VAR = 'APIFY_INTEGRATION_TESTS_API_URL'
@@ -26,3 +36,56 @@ def apify_client_async(apify_token: str) -> ApifyClientAsync:
2636
api_url = os.getenv(_API_URL_ENV_VAR)
2737

2838
return ApifyClientAsync(apify_token, api_url=api_url)
39+
40+
41+
@pytest.fixture
42+
def prepare_test_env(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Callable[[], None]:
43+
"""Prepare the testing environment by resetting the global state before each test.
44+
45+
This fixture ensures that the global state of the package is reset to a known baseline before each test runs.
46+
It also configures a temporary storage directory for test isolation.
47+
48+
Args:
49+
monkeypatch: Test utility provided by pytest for patching.
50+
tmp_path: A unique temporary directory path provided by pytest for test isolation.
51+
52+
Returns:
53+
A callable that prepares the test environment.
54+
"""
55+
56+
def _prepare_test_env() -> None:
57+
if hasattr(apify._actor.Actor, '__wrapped__'):
58+
delattr(apify._actor.Actor, '__wrapped__')
59+
60+
apify._actor.Actor._is_initialized = False
61+
62+
# Set the environment variable for the local storage directory to the temporary path.
63+
monkeypatch.setenv(ApifyEnvVars.LOCAL_STORAGE_DIR, str(tmp_path))
64+
65+
# Reset the services in the service locator.
66+
service_locator._configuration = None
67+
service_locator._event_manager = None
68+
service_locator._storage_client = None
69+
service_locator.storage_instance_manager.clear_cache()
70+
71+
# Reset the AliasResolver class state.
72+
AliasResolver._alias_map = {}
73+
AliasResolver._alias_init_lock = None
74+
75+
# Verify that the test environment was set up correctly.
76+
assert os.environ.get(ApifyEnvVars.LOCAL_STORAGE_DIR) == str(tmp_path)
77+
78+
return _prepare_test_env
79+
80+
81+
@pytest.fixture(autouse=True)
82+
def _isolate_test_environment(prepare_test_env: Callable[[], None]) -> None:
83+
"""Isolate the testing environment by resetting global state before each test.
84+
85+
This fixture ensures that each test starts with a clean slate and that any modifications during the test
86+
do not affect subsequent tests. It runs automatically for all tests.
87+
88+
Args:
89+
prepare_test_env: Fixture to prepare the environment before each test.
90+
"""
91+
prepare_test_env()

0 commit comments

Comments
 (0)