Skip to content

Commit c6776dc

Browse files
committed
cleanup plugin
1 parent 32d9919 commit c6776dc

File tree

2 files changed

+19
-28
lines changed

2 files changed

+19
-28
lines changed

codeflash/benchmarking/plugin/plugin.py

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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

301286
codeflash_benchmark_plugin = CodeFlashBenchmarkPlugin()
302287

303288

304289
def 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

308299
def pytest_addoption(parser: pytest.Parser) -> None:
309300
parser.addoption(

tests/test_trace_benchmarks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_trace_benchmarks() -> None:
3333
function_calls = cursor.fetchall()
3434

3535
# Assert the length of function calls
36-
assert len(function_calls) == 8, f"Expected 8 function calls, but got {len(function_calls)}"
36+
assert len(function_calls) == 7, f"Expected 7 function calls, but got {len(function_calls)}"
3737

3838
bubble_sort_path = (project_root / "bubble_sort_codeflash_trace.py").as_posix()
3939
process_and_bubble_sort_path = (project_root / "process_and_bubble_sort_codeflash_trace.py").as_posix()

0 commit comments

Comments
 (0)