Skip to content

Commit 7cf6ace

Browse files
authored
Merge pull request #1761 from dandi/enh-test-versions
Add dependency versions to pytest header output
2 parents 598e3da + 7a663cf commit 7cf6ace

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

dandi/pytest_plugin.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from __future__ import annotations
22

3+
from importlib.metadata import PackageNotFoundError, requires, version
4+
35
from dandischema.models import DandiBaseModel
6+
from packaging.requirements import Requirement
47
from pytest import Config, Item, Parser
58

69
from .tests.fixtures import * # noqa: F401, F403 # lgtm [py/polluting-import]
@@ -32,6 +35,29 @@ def pytest_configure(config):
3235
config.addinivalue_line("markers", marker)
3336

3437

38+
def pytest_report_header(config: Config) -> list[str]:
39+
"""Add version information for key dependencies to the pytest header."""
40+
try:
41+
# Extract package names from requirement strings.
42+
# Format: "package-name >= 1.0" or "package-name ; condition"
43+
# and by regex thus we skip extras (in square brackets like [test])
44+
deps = {Requirement(dep).name for dep in (requires("dandi") or [])}
45+
except PackageNotFoundError:
46+
# Use defaults if we didn't get deps from metadata
47+
deps = {"dandischema", "h5py", "hdmf"}
48+
49+
# Format versions for display (sorted for consistent output)
50+
versions = []
51+
for pkg in sorted(deps):
52+
try:
53+
version_str = f"-{version(pkg)}"
54+
except PackageNotFoundError:
55+
version_str = " NOT INSTALLED"
56+
versions.append(f"{pkg}{version_str}")
57+
58+
return [f"dependencies: {', '.join(versions)}"] if versions else []
59+
60+
3561
def pytest_collection_modifyitems(items: list[Item], config: Config) -> None:
3662
# Based on <https://pythontesting.net/framework/pytest/pytest-run-tests
3763
# -using-particular-fixture/>

0 commit comments

Comments
 (0)