Skip to content

Commit

Permalink
fix: support benchmark.extra_info parameters on the fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
art049 committed Jun 27, 2023
1 parent 76ac297 commit b30840c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/pytest_codspeed/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pkgutil
import sys
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, Callable, List, Optional, Tuple, Union
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union

import pytest
from _pytest.fixtures import FixtureManager
Expand Down Expand Up @@ -226,21 +226,27 @@ def pytest_runtest_protocol(item: "pytest.Item", nextitem: Union["pytest.Item",
return reports # Deny further protocol hooks execution


@pytest.fixture(scope="function")
def codspeed_benchmark(request: "pytest.FixtureRequest") -> Callable:
plugin = get_plugin(request.config)
class BenchmarkFixture:
def __init__(self, request: "pytest.FixtureRequest"):
self.extra_info: Dict = {}

self._request = request

def run(func: Callable[..., Any], *args: Any, **kwargs: Any) -> Any:
def __call__(self, func: Callable[..., Any], *args: Any, **kwargs: Any) -> Any:
plugin = get_plugin(self._request.config)
plugin.benchmark_count += 1
if plugin.is_codspeed_enabled and plugin.should_measure:
assert plugin.lib is not None
_run_with_instrumentation(
plugin.lib, request.node.nodeid, func, *args, **kwargs
plugin.lib, self._request.node.nodeid, func, *args, **kwargs
)
else:
func(*args, **kwargs)

return run

@pytest.fixture(scope="function")
def codspeed_benchmark(request: "pytest.FixtureRequest") -> Callable:
return BenchmarkFixture(request)


if not IS_PYTEST_BENCHMARK_INSTALLED:
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 @@ -245,6 +245,21 @@ def _():
)


def test_pytest_benchmark_extra_info(pytester: pytest.Pytester) -> None:
"""https://pytest-benchmark.readthedocs.io/en/latest/usage.html#extra-info"""
pytester.makepyfile(
"""
import time
def test_my_stuff(benchmark):
benchmark.extra_info['foo'] = 'bar'
benchmark(time.sleep, 0.02)
"""
)
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 b30840c

Please sign in to comment.