Skip to content

Commit 4c97ec0

Browse files
fix(tests): skip pyinstrument profiling for benchmark tests
pyinstrument 5.1.2 uses a C-level ProfilerState object via sys.setprofile() that is not a callable Python function. pytest-benchmark's PauseInstrumentation saves and restores the profiler via sys.setprofile(), which fails with TypeError on the non-callable ProfilerState. Skip the auto_profile fixture for tests using the benchmark fixture, since profiling during benchmarks is counterproductive anyway (adds overhead that corrupts measurements).
1 parent 824e343 commit 4c97ec0

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

tests/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,16 @@ def auto_profile(request: Any) -> Generator[Any, Any]:
2828
"""
2929
Generate an HTML file for each test node in your test suite inside the .profiles directory.
3030
31+
Skips profiling for benchmark tests to avoid incompatibility between pyinstrument's
32+
C-level ProfilerState and pytest-benchmark's PauseInstrumentation (which calls
33+
``sys.setprofile()`` with the non-callable ProfilerState object).
34+
3135
https://pyinstrument.readthedocs.io/en/latest/guide.html#profile-pytest-tests
3236
"""
37+
if "benchmark" in request.fixturenames:
38+
yield
39+
return
40+
3341
profile_root = PROJECT__ROOT / "build/.profiles"
3442
logger.info("Starting to profile Pytest unit tests...")
3543
# Turn profiling on

0 commit comments

Comments
 (0)