|
38 | 38 | from _pytest._code.code import ExceptionInfo
|
39 | 39 | from _pytest._code.code import TerminalRepr
|
40 | 40 | from _pytest._code.code import Traceback
|
41 |
| -from _pytest._io import TerminalWriter |
42 | 41 | from _pytest._io.saferepr import saferepr
|
43 | 42 | from _pytest.compat import ascii_escaped
|
44 | 43 | from _pytest.compat import get_default_arg_names
|
45 | 44 | from _pytest.compat import get_real_func
|
46 | 45 | from _pytest.compat import getimfunc
|
47 |
| -from _pytest.compat import getlocation |
48 | 46 | from _pytest.compat import is_async_function
|
49 | 47 | from _pytest.compat import is_generator
|
50 | 48 | from _pytest.compat import NOTSET
|
51 | 49 | from _pytest.compat import safe_getattr
|
52 | 50 | from _pytest.compat import safe_isclass
|
53 | 51 | from _pytest.compat import STRING_TYPES
|
54 | 52 | from _pytest.config import Config
|
55 |
| -from _pytest.config import ExitCode |
56 | 53 | from _pytest.config import hookimpl
|
57 | 54 | from _pytest.config.argparsing import Parser
|
58 | 55 | from _pytest.deprecated import check_ispytest
|
|
69 | 66 | from _pytest.mark.structures import normalize_mark_list
|
70 | 67 | from _pytest.outcomes import fail
|
71 | 68 | from _pytest.outcomes import skip
|
72 |
| -from _pytest.pathlib import bestrelpath |
73 | 69 | from _pytest.pathlib import fnmatch_ex
|
74 | 70 | from _pytest.pathlib import import_path
|
75 | 71 | from _pytest.pathlib import ImportPathMismatchError
|
|
82 | 78 | from _pytest.warning_types import PytestUnhandledCoroutineWarning
|
83 | 79 |
|
84 | 80 |
|
85 |
| -_PYTEST_DIR = Path(_pytest.__file__).parent |
86 |
| - |
87 |
| - |
88 | 81 | def pytest_addoption(parser: Parser) -> None:
|
89 |
| - group = parser.getgroup("general") |
90 |
| - group.addoption( |
91 |
| - "--fixtures", |
92 |
| - "--funcargs", |
93 |
| - action="store_true", |
94 |
| - dest="showfixtures", |
95 |
| - default=False, |
96 |
| - help="Show available fixtures, sorted by plugin appearance " |
97 |
| - "(fixtures with leading '_' are only shown with '-v')", |
98 |
| - ) |
99 |
| - group.addoption( |
100 |
| - "--fixtures-per-test", |
101 |
| - action="store_true", |
102 |
| - dest="show_fixtures_per_test", |
103 |
| - default=False, |
104 |
| - help="Show fixtures per test", |
105 |
| - ) |
106 | 82 | parser.addini(
|
107 | 83 | "python_files",
|
108 | 84 | type="args",
|
@@ -131,16 +107,6 @@ def pytest_addoption(parser: Parser) -> None:
|
131 | 107 | )
|
132 | 108 |
|
133 | 109 |
|
134 |
| -def pytest_cmdline_main(config: Config) -> Optional[Union[int, ExitCode]]: |
135 |
| - if config.option.showfixtures: |
136 |
| - showfixtures(config) |
137 |
| - return 0 |
138 |
| - if config.option.show_fixtures_per_test: |
139 |
| - show_fixtures_per_test(config) |
140 |
| - return 0 |
141 |
| - return None |
142 |
| - |
143 |
| - |
144 | 110 | def pytest_generate_tests(metafunc: "Metafunc") -> None:
|
145 | 111 | for marker in metafunc.definition.iter_markers(name="parametrize"):
|
146 | 112 | metafunc.parametrize(*marker.args, **marker.kwargs, _param_mark=marker)
|
@@ -1519,137 +1485,6 @@ def _ascii_escaped_by_config(val: Union[str, bytes], config: Optional[Config]) -
|
1519 | 1485 | return val if escape_option else ascii_escaped(val) # type: ignore
|
1520 | 1486 |
|
1521 | 1487 |
|
1522 |
| -def _pretty_fixture_path(invocation_dir: Path, func) -> str: |
1523 |
| - loc = Path(getlocation(func, invocation_dir)) |
1524 |
| - prefix = Path("...", "_pytest") |
1525 |
| - try: |
1526 |
| - return str(prefix / loc.relative_to(_PYTEST_DIR)) |
1527 |
| - except ValueError: |
1528 |
| - return bestrelpath(invocation_dir, loc) |
1529 |
| - |
1530 |
| - |
1531 |
| -def show_fixtures_per_test(config): |
1532 |
| - from _pytest.main import wrap_session |
1533 |
| - |
1534 |
| - return wrap_session(config, _show_fixtures_per_test) |
1535 |
| - |
1536 |
| - |
1537 |
| -def _show_fixtures_per_test(config: Config, session: Session) -> None: |
1538 |
| - import _pytest.config |
1539 |
| - |
1540 |
| - session.perform_collect() |
1541 |
| - invocation_dir = config.invocation_params.dir |
1542 |
| - tw = _pytest.config.create_terminal_writer(config) |
1543 |
| - verbose = config.getvalue("verbose") |
1544 |
| - |
1545 |
| - def get_best_relpath(func) -> str: |
1546 |
| - loc = getlocation(func, invocation_dir) |
1547 |
| - return bestrelpath(invocation_dir, Path(loc)) |
1548 |
| - |
1549 |
| - def write_fixture(fixture_def: fixtures.FixtureDef[object]) -> None: |
1550 |
| - argname = fixture_def.argname |
1551 |
| - if verbose <= 0 and argname.startswith("_"): |
1552 |
| - return |
1553 |
| - prettypath = _pretty_fixture_path(invocation_dir, fixture_def.func) |
1554 |
| - tw.write(f"{argname}", green=True) |
1555 |
| - tw.write(f" -- {prettypath}", yellow=True) |
1556 |
| - tw.write("\n") |
1557 |
| - fixture_doc = inspect.getdoc(fixture_def.func) |
1558 |
| - if fixture_doc: |
1559 |
| - write_docstring( |
1560 |
| - tw, fixture_doc.split("\n\n")[0] if verbose <= 0 else fixture_doc |
1561 |
| - ) |
1562 |
| - else: |
1563 |
| - tw.line(" no docstring available", red=True) |
1564 |
| - |
1565 |
| - def write_item(item: nodes.Item) -> None: |
1566 |
| - # Not all items have _fixtureinfo attribute. |
1567 |
| - info: Optional[FuncFixtureInfo] = getattr(item, "_fixtureinfo", None) |
1568 |
| - if info is None or not info.name2fixturedefs: |
1569 |
| - # This test item does not use any fixtures. |
1570 |
| - return |
1571 |
| - tw.line() |
1572 |
| - tw.sep("-", f"fixtures used by {item.name}") |
1573 |
| - # TODO: Fix this type ignore. |
1574 |
| - tw.sep("-", f"({get_best_relpath(item.function)})") # type: ignore[attr-defined] |
1575 |
| - # dict key not used in loop but needed for sorting. |
1576 |
| - for _, fixturedefs in sorted(info.name2fixturedefs.items()): |
1577 |
| - assert fixturedefs is not None |
1578 |
| - if not fixturedefs: |
1579 |
| - continue |
1580 |
| - # Last item is expected to be the one used by the test item. |
1581 |
| - write_fixture(fixturedefs[-1]) |
1582 |
| - |
1583 |
| - for session_item in session.items: |
1584 |
| - write_item(session_item) |
1585 |
| - |
1586 |
| - |
1587 |
| -def showfixtures(config: Config) -> Union[int, ExitCode]: |
1588 |
| - from _pytest.main import wrap_session |
1589 |
| - |
1590 |
| - return wrap_session(config, _showfixtures_main) |
1591 |
| - |
1592 |
| - |
1593 |
| -def _showfixtures_main(config: Config, session: Session) -> None: |
1594 |
| - import _pytest.config |
1595 |
| - |
1596 |
| - session.perform_collect() |
1597 |
| - invocation_dir = config.invocation_params.dir |
1598 |
| - tw = _pytest.config.create_terminal_writer(config) |
1599 |
| - verbose = config.getvalue("verbose") |
1600 |
| - |
1601 |
| - fm = session._fixturemanager |
1602 |
| - |
1603 |
| - available = [] |
1604 |
| - seen: Set[Tuple[str, str]] = set() |
1605 |
| - |
1606 |
| - for argname, fixturedefs in fm._arg2fixturedefs.items(): |
1607 |
| - assert fixturedefs is not None |
1608 |
| - if not fixturedefs: |
1609 |
| - continue |
1610 |
| - for fixturedef in fixturedefs: |
1611 |
| - loc = getlocation(fixturedef.func, invocation_dir) |
1612 |
| - if (fixturedef.argname, loc) in seen: |
1613 |
| - continue |
1614 |
| - seen.add((fixturedef.argname, loc)) |
1615 |
| - available.append( |
1616 |
| - ( |
1617 |
| - len(fixturedef.baseid), |
1618 |
| - fixturedef.func.__module__, |
1619 |
| - _pretty_fixture_path(invocation_dir, fixturedef.func), |
1620 |
| - fixturedef.argname, |
1621 |
| - fixturedef, |
1622 |
| - ) |
1623 |
| - ) |
1624 |
| - |
1625 |
| - available.sort() |
1626 |
| - currentmodule = None |
1627 |
| - for baseid, module, prettypath, argname, fixturedef in available: |
1628 |
| - if currentmodule != module: |
1629 |
| - if not module.startswith("_pytest."): |
1630 |
| - tw.line() |
1631 |
| - tw.sep("-", f"fixtures defined from {module}") |
1632 |
| - currentmodule = module |
1633 |
| - if verbose <= 0 and argname.startswith("_"): |
1634 |
| - continue |
1635 |
| - tw.write(f"{argname}", green=True) |
1636 |
| - if fixturedef.scope != "function": |
1637 |
| - tw.write(" [%s scope]" % fixturedef.scope, cyan=True) |
1638 |
| - tw.write(f" -- {prettypath}", yellow=True) |
1639 |
| - tw.write("\n") |
1640 |
| - doc = inspect.getdoc(fixturedef.func) |
1641 |
| - if doc: |
1642 |
| - write_docstring(tw, doc.split("\n\n")[0] if verbose <= 0 else doc) |
1643 |
| - else: |
1644 |
| - tw.line(" no docstring available", red=True) |
1645 |
| - tw.line() |
1646 |
| - |
1647 |
| - |
1648 |
| -def write_docstring(tw: TerminalWriter, doc: str, indent: str = " ") -> None: |
1649 |
| - for line in doc.split("\n"): |
1650 |
| - tw.line(indent + line) |
1651 |
| - |
1652 |
| - |
1653 | 1488 | class Function(PyobjMixin, nodes.Item):
|
1654 | 1489 | """Item responsible for setting up and executing a Python test function.
|
1655 | 1490 |
|
|
0 commit comments