@@ -37,12 +37,6 @@ def pytest_addoption(parser):
3737 '--test-pipeline-options' ,
3838 help = 'Options to use in test pipelines. NOTE: Tests may '
3939 'ignore some or all of these options.' )
40- parser .addoption (
41- '--enable-test-cleanup' ,
42- action = 'store_true' ,
43- default = False ,
44- help = 'Enable expensive cleanup operations. Auto-enabled in CI by default. '
45- 'Use this flag to explicitly enable cleanup in local development.' )
4640
4741
4842# See pytest.ini for main collection rules.
@@ -110,86 +104,51 @@ def configure_beam_rpc_timeouts():
110104 print ("Successfully configured Beam RPC timeouts" )
111105
112106
113- def _running_in_ci ():
114- """Returns True if running in a CI environment."""
115- return (
116- os .getenv ('GITHUB_ACTIONS' ) == 'true' or
117- os .getenv ('CI' ) == 'true' or
118- os .getenv ('CONTINUOUS_INTEGRATION' ) == 'true'
119- )
120-
121-
122- def _should_enable_test_cleanup (config ):
123- """Returns True if expensive cleanup operations should run.
124-
125- Result is cached on config object to avoid re-computation per test.
107+ @pytest .fixture (scope = "class" , autouse = True )
108+ def ensure_clean_state ():
126109 """
127- if hasattr (config , '_should_enable_test_cleanup_result' ):
128- return config ._should_enable_test_cleanup_result
129-
130- if config .getoption ('--enable-test-cleanup' ):
131- result = True
132- reason = "enabled via --enable-test-cleanup"
133- else :
134- if _running_in_ci ():
135- result = True
136- reason = "CI detected"
137- else :
138- result = False
139- reason = "local development"
140-
141- # Log once per session
142- if not hasattr (config , '_cleanup_decision_logged' ):
143- print (f"\n [Test Cleanup] Enabled: { result } ({ reason } )" )
144- config ._cleanup_decision_logged = True
145-
146- config ._should_enable_test_cleanup_result = result
147- return result
148-
149-
150- @pytest .fixture (autouse = True )
151- def ensure_clean_state (request ):
110+ Ensure clean state before each test class
111+ to prevent cross-test contamination.
112+ Runs once per test class instead of per test to reduce overhead.
152113 """
153- Ensures clean state between tests to prevent contamination.
154-
155- Expensive operations (sleeps, extra GC) only run in CI or when
156- explicitly enabled to keep local tests fast.
157- """
158- enable_cleanup = _should_enable_test_cleanup (request .config )
159-
160- if enable_cleanup :
161- gc .collect ()
114+ # Force garbage collection to clean up any lingering resources
115+ gc .collect ()
162116
117+ # Log active thread count for debugging
163118 thread_count = threading .active_count ()
164119 if thread_count > 50 :
165- print (f"Warning: { thread_count } active threads detected before test" )
166- if enable_cleanup :
167- time .sleep (0.5 )
168- gc .collect ()
120+ print (f"Warning: { thread_count } active threads detected before test class " )
121+ # Force a brief pause to let threads settle
122+ time .sleep (0.5 )
123+ gc .collect ()
169124
170125 yield
171126
127+ # Enhanced cleanup after test class
172128 try :
173- if enable_cleanup :
174- gc .collect ()
175- time .sleep (0.1 )
176- gc .collect ()
129+ # Force more aggressive cleanup
130+ gc .collect ()
131+ # Brief pause to let any async operations complete
132+ time .sleep (0.1 )
133+ # Additional garbage collection
134+ gc .collect ()
177135 except Exception as e :
178136 print (f"Warning: Cleanup error: { e } " )
179137
180138
181- @pytest .fixture (autouse = True )
182- def enhance_mock_stability (request ):
183- """Improves mock stability in DinD environment."""
184- enable_cleanup = _should_enable_test_cleanup (request .config )
185-
186- if enable_cleanup :
187- time .sleep (0.05 )
139+ @pytest .fixture (scope = "class" , autouse = True )
140+ def enhance_mock_stability ():
141+ """
142+ Enhance mock stability in DinD environment.
143+ Runs once per test class instead of per test to reduce overhead.
144+ """
145+ # Brief pause before test class to ensure clean mock state
146+ time .sleep (0.05 )
188147
189148 yield
190149
191- if enable_cleanup :
192- time .sleep (0.05 )
150+ # Brief pause after test class to let mocks clean up
151+ time .sleep (0.05 )
193152
194153
195154def pytest_configure (config ):
0 commit comments