MCPcopy Create free account
hub / github.com/pytest-dev/pytest / _show_fixtures_per_test

Function _show_fixtures_per_test

src/_pytest/python.py:1478–1525  ·  view source on GitHub ↗
(config: Config, session: Session)

Source from the content-addressed store, hash-verified

1476
1477
1478def _show_fixtures_per_test(config: Config, session: Session) -> None:
1479 import _pytest.config
1480
1481 session.perform_collect()
1482 curdir = Path.cwd()
1483 tw = _pytest.config.create_terminal_writer(config)
1484 verbose = config.getvalue("verbose")
1485
1486 def get_best_relpath(func) -> str:
1487 loc = getlocation(func, str(curdir))
1488 return bestrelpath(curdir, Path(loc))
1489
1490 def write_fixture(fixture_def: fixtures.FixtureDef[object]) -> None:
1491 argname = fixture_def.argname
1492 if verbose <= 0 and argname.startswith("_"):
1493 return
1494 prettypath = _pretty_fixture_path(fixture_def.func)
1495 tw.write(f"{argname}", green=True)
1496 tw.write(f" -- {prettypath}", yellow=True)
1497 tw.write("\n")
1498 fixture_doc = inspect.getdoc(fixture_def.func)
1499 if fixture_doc:
1500 write_docstring(
1501 tw, fixture_doc.split("\n\n")[0] if verbose <= 0 else fixture_doc
1502 )
1503 else:
1504 tw.line(" no docstring available", red=True)
1505
1506 def write_item(item: nodes.Item) -> None:
1507 # Not all items have _fixtureinfo attribute.
1508 info: Optional[FuncFixtureInfo] = getattr(item, "_fixtureinfo", None)
1509 if info is None or not info.name2fixturedefs:
1510 # This test item does not use any fixtures.
1511 return
1512 tw.line()
1513 tw.sep("-", f"fixtures used by {item.name}")
1514 # TODO: Fix this type ignore.
1515 tw.sep("-", f"({get_best_relpath(item.function)})") # type: ignore[attr-defined]
1516 # dict key not used in loop but needed for sorting.
1517 for _, fixturedefs in sorted(info.name2fixturedefs.items()):
1518 assert fixturedefs is not None
1519 if not fixturedefs:
1520 continue
1521 # Last item is expected to be the one used by the test item.
1522 write_fixture(fixturedefs[-1])
1523
1524 for session_item in session.items:
1525 write_item(session_item)
1526
1527
1528def showfixtures(config: Config) -> Union[int, ExitCode]:

Callers

nothing calls this directly

Calls 3

write_itemFunction · 0.85
perform_collectMethod · 0.80
getvalueMethod · 0.45

Tested by

no test coverage detected