Skip to content

Commit

Permalink
Fix broken hooks on pytest 8.1+ (and set that as the min supported ve…
Browse files Browse the repository at this point in the history
…rsion). Fixes #263.
  • Loading branch information
ionelmc committed Oct 30, 2024
1 parent 41302ed commit 8dfeeec
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
Changelog
=========

5.1.0 (2024-10-30)
------------------

* Fixed broken hooks handling on pytest 8.1 or later (the ``TypeError: import_path() missing 1 required keyword-only argument: 'consider_namespace_packages'`` issue).
Unfortunately this sets the minimum supported pytest version to 8.1.

5.0.1 (2024-10-30)
------------------

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def read(*names, **kwargs):
],
python_requires='>=3.9',
install_requires=[
'pytest>=3.8',
'pytest>=8.1',
'py-cpuinfo',
],
extras_require={
Expand Down
11 changes: 9 additions & 2 deletions src/pytest_benchmark/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from _pytest import pathlib
from _pytest._io import TerminalWriter
from _pytest.config.findpaths import locate_config

from pytest_benchmark.csv import CSVResults

Expand Down Expand Up @@ -108,10 +109,16 @@ def make_parser():


class HookDispatch:
def __init__(self, **kwargs):
def __init__(self, *, root, **kwargs):
_, _, config = locate_config(invocation_dir=root, args=())
conftest_file = pathlib.Path('conftest.py')
if conftest_file.exists():
self.conftest = pathlib.import_path(conftest_file, **kwargs)
self.conftest = pathlib.import_path(
conftest_file,
**kwargs,
root=root,
consider_namespace_packages=bool(config.get('consider_namespace_packages')),
)
else:
self.conftest = None

Expand Down
47 changes: 47 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path

import pytest
from _pytest.legacypath import Testdir
from _pytest.pytester import LineMatcher

pytest_plugins = ('pytester',)
Expand Down Expand Up @@ -143,6 +144,52 @@ def test_help_compare(testdir, args):
assert result.ret == 0


def test_hooks(testdir: Testdir):
testdir.makepyfile(
conftest="""
def pytest_benchmark_scale_unit(config, unit, benchmarks, best, worst, sort):
return '', 1
"""
)
result = testdir.run('py.test-benchmark', '--storage', STORAGE, 'list')
assert result.stderr.lines == []
result.stdout.fnmatch_lines(
[
'*0001_*.json',
'*0002_*.json',
'*0003_*.json',
'*0004_*.json',
'*0005_*.json',
'*0006_*.json',
'*0007_*.json',
'*0008_*.json',
'*0009_*.json',
'*0010_*.json',
'*0011_*.json',
'*0012_*.json',
'*0013_*.json',
'*0014_*.json',
'*0015_*.json',
'*0016_*.json',
'*0017_*.json',
'*0018_*.json',
'*0019_*.json',
'*0020_*.json',
'*0021_*.json',
'*0022_*.json',
'*0023_*.json',
'*0024_*.json',
'*0025_*.json',
'*0026_*.json',
'*0027_*.json',
'*0028_*.json',
'*0029_*.json',
'*0030_*.json',
]
)
assert result.ret == 0


def test_list(testdir):
result = testdir.run('py.test-benchmark', '--storage', STORAGE, 'list')
assert result.stderr.lines == []
Expand Down

0 comments on commit 8dfeeec

Please sign in to comment.