Skip to content

Commit ab2d827

Browse files
authored
feat: add support for returning values (#12)
* Add support for returning values * Add test file * Better test * Black
1 parent 48e4228 commit ab2d827

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/pytest_codspeed/plugin.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,11 @@ def _run_with_instrumentation(
166166
gc.collect()
167167
gc.disable()
168168

169+
result = None
170+
169171
def __codspeed_root_frame__():
170-
fn(*args, **kwargs)
172+
nonlocal result
173+
result = fn(*args, **kwargs)
171174

172175
if SUPPORTS_PERF_TRAMPOLINE:
173176
# Warmup CPython performance map cache
@@ -181,6 +184,8 @@ def __codspeed_root_frame__():
181184
if is_gc_enabled:
182185
gc.enable()
183186

187+
return result
188+
184189

185190
@pytest.hookimpl(tryfirst=True)
186191
def pytest_runtest_protocol(item: "pytest.Item", nextitem: Union["pytest.Item", None]):
@@ -241,11 +246,11 @@ def __call__(self, func: Callable[..., Any], *args: Any, **kwargs: Any) -> Any:
241246
plugin.benchmark_count += 1
242247
if plugin.is_codspeed_enabled and plugin.should_measure:
243248
assert plugin.lib is not None
244-
_run_with_instrumentation(
249+
return _run_with_instrumentation(
245250
plugin.lib, self._request.node.nodeid, func, *args, **kwargs
246251
)
247252
else:
248-
func(*args, **kwargs)
253+
return func(*args, **kwargs)
249254

250255

251256
@pytest.fixture(scope="function")

tests/test_pytest_plugin.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,21 @@ def test_my_stuff(benchmark):
264264
assert result.ret == 0, "the run should have succeeded"
265265

266266

267+
def test_pytest_benchmark_return_value(pytester: pytest.Pytester) -> None:
268+
pytester.makepyfile(
269+
"""
270+
def calculate_something():
271+
return 1 + 1
272+
273+
def test_my_stuff(benchmark):
274+
value = benchmark(calculate_something)
275+
assert value == 2
276+
"""
277+
)
278+
result = pytester.runpytest("--codspeed")
279+
assert result.ret == 0, "the run should have succeeded"
280+
281+
267282
@skip_without_valgrind
268283
@skip_without_perf_trampoline
269284
def test_perf_maps_generation(pytester: pytest.Pytester, codspeed_env) -> None:

0 commit comments

Comments
 (0)