Skip to content

Commit

Permalink
feat: add support for returning values (#12)
Browse files Browse the repository at this point in the history
* Add support for returning values

* Add test file

* Better test

* Black
  • Loading branch information
patrick91 committed Jul 19, 2023
1 parent 48e4228 commit ab2d827
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/pytest_codspeed/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,11 @@ def _run_with_instrumentation(
gc.collect()
gc.disable()

result = None

def __codspeed_root_frame__():
fn(*args, **kwargs)
nonlocal result
result = fn(*args, **kwargs)

if SUPPORTS_PERF_TRAMPOLINE:
# Warmup CPython performance map cache
Expand All @@ -181,6 +184,8 @@ def __codspeed_root_frame__():
if is_gc_enabled:
gc.enable()

return result


@pytest.hookimpl(tryfirst=True)
def pytest_runtest_protocol(item: "pytest.Item", nextitem: Union["pytest.Item", None]):
Expand Down Expand Up @@ -241,11 +246,11 @@ def __call__(self, func: Callable[..., Any], *args: Any, **kwargs: Any) -> Any:
plugin.benchmark_count += 1
if plugin.is_codspeed_enabled and plugin.should_measure:
assert plugin.lib is not None
_run_with_instrumentation(
return _run_with_instrumentation(
plugin.lib, self._request.node.nodeid, func, *args, **kwargs
)
else:
func(*args, **kwargs)
return func(*args, **kwargs)


@pytest.fixture(scope="function")
Expand Down
15 changes: 15 additions & 0 deletions tests/test_pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,21 @@ def test_my_stuff(benchmark):
assert result.ret == 0, "the run should have succeeded"


def test_pytest_benchmark_return_value(pytester: pytest.Pytester) -> None:
pytester.makepyfile(
"""
def calculate_something():
return 1 + 1
def test_my_stuff(benchmark):
value = benchmark(calculate_something)
assert value == 2
"""
)
result = pytester.runpytest("--codspeed")
assert result.ret == 0, "the run should have succeeded"


@skip_without_valgrind
@skip_without_perf_trampoline
def test_perf_maps_generation(pytester: pytest.Pytester, codspeed_env) -> None:
Expand Down

0 comments on commit ab2d827

Please sign in to comment.