Skip to content

Commit 7335c1e

Browse files
committed
Elide pytest-internal paths for --fixtures
Fixes pytest-dev#8822
1 parent 86446ed commit 7335c1e

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

changelog/8822.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
When showing fixture paths in `--fixtures` or `--fixtures-by-test`, fixtures coming from pytest itself now display an elided path, rather than the full path to the file in the `site-packages` directory.

src/_pytest/python.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@
8383
from _pytest.scope import _ScopeName
8484

8585

86+
_PYTEST_DIR = Path(_pytest.__file__).parent
87+
88+
8689
def pytest_addoption(parser: Parser) -> None:
8790
group = parser.getgroup("general")
8891
group.addoption(
@@ -1443,6 +1446,16 @@ def idmaker(
14431446
return resolved_ids
14441447

14451448

1449+
def _pretty_fixture_path(func) -> str:
1450+
cwd = Path.cwd()
1451+
loc = Path(getlocation(func, str(cwd)))
1452+
prefix = Path("...", "_pytest")
1453+
try:
1454+
return str(prefix / loc.relative_to(_PYTEST_DIR))
1455+
except ValueError:
1456+
return bestrelpath(cwd, loc)
1457+
1458+
14461459
def show_fixtures_per_test(config):
14471460
from _pytest.main import wrap_session
14481461

@@ -1465,9 +1478,9 @@ def write_fixture(fixture_def: fixtures.FixtureDef[object]) -> None:
14651478
argname = fixture_def.argname
14661479
if verbose <= 0 and argname.startswith("_"):
14671480
return
1468-
bestrel = get_best_relpath(fixture_def.func)
1481+
prettypath = _pretty_fixture_path(fixture_def.func)
14691482
tw.write(f"{argname}", green=True)
1470-
tw.write(f" -- {bestrel}", yellow=True)
1483+
tw.write(f" -- {prettypath}", yellow=True)
14711484
tw.write("\n")
14721485
fixture_doc = inspect.getdoc(fixture_def.func)
14731486
if fixture_doc:
@@ -1531,15 +1544,15 @@ def _showfixtures_main(config: Config, session: Session) -> None:
15311544
(
15321545
len(fixturedef.baseid),
15331546
fixturedef.func.__module__,
1534-
bestrelpath(curdir, Path(loc)),
1547+
_pretty_fixture_path(fixturedef.func),
15351548
fixturedef.argname,
15361549
fixturedef,
15371550
)
15381551
)
15391552

15401553
available.sort()
15411554
currentmodule = None
1542-
for baseid, module, bestrel, argname, fixturedef in available:
1555+
for baseid, module, prettypath, argname, fixturedef in available:
15431556
if currentmodule != module:
15441557
if not module.startswith("_pytest."):
15451558
tw.line()
@@ -1550,14 +1563,13 @@ def _showfixtures_main(config: Config, session: Session) -> None:
15501563
tw.write(f"{argname}", green=True)
15511564
if fixturedef.scope != "function":
15521565
tw.write(" [%s scope]" % fixturedef.scope, cyan=True)
1553-
tw.write(f" -- {bestrel}", yellow=True)
1566+
tw.write(f" -- {prettypath}", yellow=True)
15541567
tw.write("\n")
1555-
loc = getlocation(fixturedef.func, str(curdir))
15561568
doc = inspect.getdoc(fixturedef.func)
15571569
if doc:
15581570
write_docstring(tw, doc.split("\n\n")[0] if verbose <= 0 else doc)
15591571
else:
1560-
tw.line(f" {loc}: no docstring available", red=True)
1572+
tw.line(f" no docstring available", red=True)
15611573
tw.line()
15621574

15631575

testing/python/fixtures.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3346,9 +3346,9 @@ def test_show_fixtures(self, pytester: Pytester) -> None:
33463346
result = pytester.runpytest("--fixtures")
33473347
result.stdout.fnmatch_lines(
33483348
[
3349-
"tmp_path_factory [[]session scope[]] -- *tmpdir.py*",
3349+
"tmp_path_factory [[]session scope[]] -- .../_pytest/tmpdir.py:*",
33503350
"*for the test session*",
3351-
"tmp_path -- *",
3351+
"tmp_path -- .../_pytest/tmpdir.py:*",
33523352
"*temporary directory*",
33533353
]
33543354
)
@@ -3357,9 +3357,9 @@ def test_show_fixtures_verbose(self, pytester: Pytester) -> None:
33573357
result = pytester.runpytest("--fixtures", "-v")
33583358
result.stdout.fnmatch_lines(
33593359
[
3360-
"tmp_path_factory [[]session scope[]] -- *tmpdir.py*",
3360+
"tmp_path_factory [[]session scope[]] -- .../_pytest/tmpdir.py:*",
33613361
"*for the test session*",
3362-
"tmp_path -- *tmpdir.py*",
3362+
"tmp_path -- .../_pytest/tmpdir.py:*",
33633363
"*temporary directory*",
33643364
]
33653365
)

0 commit comments

Comments
 (0)