Skip to content

Commit 000f0bb

Browse files
committed
fix: support benchmark.extra_info parameters on the fixture
1 parent fc4fbad commit 000f0bb

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/pytest_codspeed/plugin.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pkgutil
44
import sys
55
from dataclasses import dataclass, field
6-
from typing import TYPE_CHECKING, Any, Callable, List, Optional, Tuple, Union
6+
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union
77

88
import pytest
99
from _pytest.fixtures import FixtureManager
@@ -230,21 +230,27 @@ def pytest_runtest_protocol(item: "pytest.Item", nextitem: Union["pytest.Item",
230230
return reports # Deny further protocol hooks execution
231231

232232

233-
@pytest.fixture(scope="function")
234-
def codspeed_benchmark(request: "pytest.FixtureRequest") -> Callable:
235-
plugin = get_plugin(request.config)
233+
class BenchmarkFixture:
234+
def __init__(self, request: "pytest.FixtureRequest"):
235+
self.extra_info: Dict = {}
236+
237+
self._request = request
236238

237-
def run(func: Callable[..., Any], *args: Any, **kwargs: Any) -> Any:
239+
def __call__(self, func: Callable[..., Any], *args: Any, **kwargs: Any) -> Any:
240+
plugin = get_plugin(self._request.config)
238241
plugin.benchmark_count += 1
239242
if plugin.is_codspeed_enabled and plugin.should_measure:
240243
assert plugin.lib is not None
241244
_run_with_instrumentation(
242-
plugin.lib, request.node.nodeid, func, *args, **kwargs
245+
plugin.lib, self._request.node.nodeid, func, *args, **kwargs
243246
)
244247
else:
245248
func(*args, **kwargs)
246249

247-
return run
250+
251+
@pytest.fixture(scope="function")
252+
def codspeed_benchmark(request: "pytest.FixtureRequest") -> Callable:
253+
return BenchmarkFixture(request)
248254

249255

250256
if not IS_PYTEST_BENCHMARK_INSTALLED:

tests/test_pytest_plugin.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,21 @@ def _():
249249
)
250250

251251

252+
def test_pytest_benchmark_extra_info(pytester: pytest.Pytester) -> None:
253+
"""https://pytest-benchmark.readthedocs.io/en/latest/usage.html#extra-info"""
254+
pytester.makepyfile(
255+
"""
256+
import time
257+
258+
def test_my_stuff(benchmark):
259+
benchmark.extra_info['foo'] = 'bar'
260+
benchmark(time.sleep, 0.02)
261+
"""
262+
)
263+
result = pytester.runpytest("--codspeed")
264+
assert result.ret == 0, "the run should have succeeded"
265+
266+
252267
@skip_without_valgrind
253268
@skip_without_perf_trampoline
254269
def test_perf_maps_generation(pytester: pytest.Pytester, codspeed_env) -> None:

0 commit comments

Comments
 (0)