18
18
from crawlee .storages import _creation_management
19
19
20
20
if TYPE_CHECKING :
21
- from collections .abc import AsyncGenerator , Generator
21
+ from collections .abc import AsyncGenerator
22
22
from pathlib import Path
23
23
24
24
@@ -38,6 +38,12 @@ def prepare_test_env(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Callabl
38
38
"""
39
39
40
40
def _prepare_test_env () -> None :
41
+ # Reset the flags in the service locator to indicate that no services are explicitly set. This ensures
42
+ # a clean state, as services might have been set during a previous test and not reset properly.
43
+ service_locator ._service_locator ._configuration_was_set = False
44
+ service_locator ._service_locator ._storage_client_was_set = False
45
+ service_locator ._service_locator ._event_manager_was_set = False
46
+
41
47
# Set the environment variable for the local storage directory to the temporary path.
42
48
monkeypatch .setenv ('CRAWLEE_STORAGE_DIR' , str (tmp_path ))
43
49
@@ -68,46 +74,17 @@ def _prepare_test_env() -> None:
68
74
return _prepare_test_env
69
75
70
76
71
- @pytest .fixture
72
- def cleanup_test_env () -> Callable [[], None ]:
73
- """Clean up and reset global state after a test has completed.
74
-
75
- This fixture ensures that any modifications to the global state during a test are undone
76
- after the test finishes. Restoring the environment for the subsequent tests.
77
-
78
- Returns:
79
- A callable that cleans up the test environment.
80
- """
81
-
82
- def _cleanup_test_env () -> None :
83
- # Reset the flags in the service locator to indicate no services are explicitly set.
84
- service_locator ._service_locator ._configuration_was_set = False
85
- service_locator ._service_locator ._storage_client_was_set = False
86
- service_locator ._service_locator ._event_manager_was_set = False
87
-
88
- return _cleanup_test_env
89
-
90
-
91
77
@pytest .fixture (autouse = True )
92
- def _isolate_test_environment (
93
- prepare_test_env : Callable [[], None ],
94
- cleanup_test_env : Callable [[], None ],
95
- ) -> Generator [None , None , None ]:
78
+ def _isolate_test_environment (prepare_test_env : Callable [[], None ]) -> None :
96
79
"""Isolate the testing environment by resetting global state before and after each test.
97
80
98
81
This fixture ensures that each test starts with a clean slate and that any modifications during the test
99
82
do not affect subsequent tests. It runs automatically for all tests.
100
83
101
84
Args:
102
85
prepare_test_env: Fixture to prepare the environment before each test.
103
- cleanup_test_env: Fixture to clean up the environment after each test.
104
-
105
- Yields:
106
- None. This fixture works as a setup and teardown mechanism.
107
86
"""
108
87
prepare_test_env ()
109
- yield
110
- cleanup_test_env ()
111
88
112
89
113
90
@pytest .fixture
0 commit comments