@@ -211,34 +211,27 @@ def pytest_sessionfinish(self, session, exitstatus) -> None: # noqa: ANN001, AR
211211 # Close the database connection
212212 self .close ()
213213
214- @staticmethod
215- def pytest_addoption (parser : pytest .Parser ) -> None :
216- parser .addoption ("--codeflash-trace" , action = "store_true" , default = False , help = "Enable CodeFlash tracing" )
217-
218- @staticmethod
219- def pytest_configure (config : pytest .Config ) -> None :
220- """Register the benchmark marker and disable conflicting plugins."""
221- config .addinivalue_line (
222- "markers" , "benchmark: mark test as a benchmark that should be run with codeflash tracing"
223- )
224-
225- if config .getoption ("--codeflash-trace" ) and IS_PYTEST_BENCHMARK_INSTALLED :
226- object .__setattr__ (config .option , "benchmark_disable" , True )
227- config .pluginmanager .set_blocked ("pytest_benchmark" )
228- config .pluginmanager .set_blocked ("pytest-benchmark" )
229-
230214 @staticmethod
231215 def pytest_collection_modifyitems (config : pytest .Config , items : list [pytest .Item ]) -> None :
232- # Skip tests that don't have the benchmark marker
216+ # Skip tests that don't have the benchmark fixture
233217 if not config .getoption ("--codeflash-trace" ):
234218 return
235219
220+ skip_no_benchmark = pytest .mark .skip (reason = "Test requires benchmark fixture" )
236221 for item in items :
222+ # Check for direct benchmark fixture usage
223+ has_fixture = hasattr (item , "fixturenames" ) and "benchmark" in item .fixturenames
224+
237225 # Check for @pytest.mark.benchmark marker
226+ has_marker = False
238227 if hasattr (item , "get_closest_marker" ):
239228 marker = item .get_closest_marker ("benchmark" )
240- if marker is None :
241- item .add_marker (pytest .mark .skip (reason = "Test requires benchmark marker" ))
229+ if marker is not None :
230+ has_marker = True
231+
232+ # Skip if neither fixture nor marker is present
233+ if not (has_fixture or has_marker ):
234+ item .add_marker (skip_no_benchmark )
242235
243236 # Benchmark fixture
244237 class Benchmark : # noqa: D106
@@ -289,21 +282,19 @@ def _run_benchmark(self, func, *args, **kwargs): # noqa: ANN001, ANN002, ANN003
289282
290283 return result
291284
292- @staticmethod
293- @pytest .fixture
294- def benchmark (request : pytest .FixtureRequest ) -> object :
295- """Fixture to provide the benchmark functionality."""
296- if not request .config .getoption ("--codeflash-trace" ):
297- return None
298- return CodeFlashBenchmarkPlugin .Benchmark (request )
299-
300285
301286codeflash_benchmark_plugin = CodeFlashBenchmarkPlugin ()
302287
303288
304289def pytest_configure (config : pytest .Config ) -> None :
290+ """Register the benchmark marker and disable conflicting plugins."""
305291 config .addinivalue_line ("markers" , "benchmark: mark test as a benchmark that should be run with codeflash tracing" )
306292
293+ if config .getoption ("--codeflash-trace" ) and IS_PYTEST_BENCHMARK_INSTALLED :
294+ config .option .benchmark_disable = True
295+ config .pluginmanager .set_blocked ("pytest_benchmark" )
296+ config .pluginmanager .set_blocked ("pytest-benchmark" )
297+
307298
308299def pytest_addoption (parser : pytest .Parser ) -> None :
309300 parser .addoption (
0 commit comments