diff --git a/AUTHORS b/AUTHORS index f04403e2d5c..258efc1867a 100644 --- a/AUTHORS +++ b/AUTHORS @@ -24,6 +24,7 @@ Alex Lambson Alex Chen Alexander Johnson Alexander King +Alexander683 Alexei Kozlenok algojogacor Alice Purcell diff --git a/changelog/14920.improvement.rst b/changelog/14920.improvement.rst new file mode 100644 index 00000000000..a6b30262634 --- /dev/null +++ b/changelog/14920.improvement.rst @@ -0,0 +1 @@ +``--setup-show``, ``--setup-only``, ``--setup-plan``, and ``--fixtures-per-test`` now mark autouse fixtures with ``[autouse]``. diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 82b45c6964d..f5a2f45de42 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -1134,7 +1134,6 @@ def __init__( ids: tuple[object | None, ...] | Callable[[Any], object | None] | None = None, *, node: nodes.Node | NotSetType = NOTSET, - # only used in a deprecationwarning msg, can be removed in pytest9 _autouse: bool = False, _ispytest: bool = False, ) -> None: @@ -1196,8 +1195,8 @@ def __init__( self.cached_result: _FixtureCachedResult[FixtureValue] | None = None self._finalizers: Final[list[Callable[[], object]]] = [] - # only used to emit a deprecationwarning, can be removed in pytest9 - self._autouse = _autouse + # Whether this fixture was defined with autouse=True. + self._autouse: Final = _autouse @property def scope(self) -> ScopeName: @@ -2485,6 +2484,8 @@ def write_fixture(fixture_def: FixtureDef[object]) -> None: return prettypath = _pretty_fixture_path(invocation_dir, fixture_def.func) tw.write(f"{argname}", green=True) + if fixture_def._autouse: + tw.write(" [autouse]", cyan=True) tw.write(f" -- {prettypath}", yellow=True) tw.write("\n") fixture_doc = inspect.getdoc(fixture_def.func) diff --git a/src/_pytest/setuponly.py b/src/_pytest/setuponly.py index 7e6b46bcdb4..9c0812bfe87 100644 --- a/src/_pytest/setuponly.py +++ b/src/_pytest/setuponly.py @@ -85,6 +85,9 @@ def _show_fixture_action( if hasattr(fixturedef, "cached_param"): tw.write(f"[{saferepr(fixturedef.cached_param, maxsize=42)}]") + if fixturedef._autouse: + tw.write(" [autouse]") + tw.flush() if capman: diff --git a/testing/python/show_fixtures_per_test.py b/testing/python/show_fixtures_per_test.py index 2362847f338..a0c71452a06 100644 --- a/testing/python/show_fixtures_per_test.py +++ b/testing/python/show_fixtures_per_test.py @@ -326,3 +326,67 @@ def test_indirectly_parametrized_fixture(indirectly): " indirectly parametrized fixture", ] ) + + +def test_autouse_fixtures(pytester: Pytester) -> None: + pytester.makeconftest( + ''' + import pytest + + @pytest.fixture(scope="session", autouse=True) + def session_auto(): + """session autouse fixture""" + + @pytest.fixture(autouse=True) + def _private_auto(): + """private autouse fixture""" + ''' + ) + p = pytester.makepyfile( + ''' + import pytest + + @pytest.fixture(autouse=True) + def module_auto(): + """module autouse fixture""" + + @pytest.fixture + def explicit_fix(): + """explicit fixture""" + + def test_one(explicit_fix): + pass + ''' + ) + + result = pytester.runpytest("--fixtures-per-test", p) + assert result.ret == 0 + result.stdout.fnmatch_lines( + [ + "*fixtures used by test_one*", + "*(test_autouse_fixtures.py:11)*", + "explicit_fix -- test_autouse_fixtures.py:7", + " explicit fixture", + "module_auto [[]autouse[]] -- test_autouse_fixtures.py:3", + " module autouse fixture", + "session_auto [[]autouse[]] -- conftest.py:3", + " session autouse fixture", + ] + ) + result.stdout.no_fnmatch_line("*_private_auto*") + + result_verbose = pytester.runpytest("--fixtures-per-test", "-v", p) + assert result_verbose.ret == 0 + result_verbose.stdout.fnmatch_lines( + [ + "_private_auto [[]autouse[]] -- conftest.py:7", + " private autouse fixture", + ] + ) + + result_color = pytester.runpytest("--fixtures-per-test", "--color=yes", p) + assert result_color.ret == 0 + assert ( + "\x1b[32mmodule_auto\x1b[0m\x1b[36m [autouse]\x1b[0m" + in result_color.stdout.str() + ) diff --git a/testing/test_setuponly.py b/testing/test_setuponly.py index 1d2261f75a8..27e872069fd 100644 --- a/testing/test_setuponly.py +++ b/testing/test_setuponly.py @@ -122,9 +122,40 @@ def test_arg1(arg_function): result.stdout.fnmatch_lines( [ - "SETUP S arg_session*", - "*SETUP F arg_function*", + "SETUP S arg_session [[]autouse[]]", + " SETUP F arg_function", "*test_arg1 (fixtures used: arg_function, arg_session)*", + " TEARDOWN F arg_function", + "TEARDOWN S arg_session [[]autouse[]]", + ] + ) + + +def test_show_fixtures_with_autouse_params_and_deps(pytester: Pytester, mode) -> None: + p = pytester.makepyfile( + """ + import pytest + @pytest.fixture + def dep(): + pass + @pytest.fixture(params=['foo'], autouse=True) + def auto_fix(dep): + pass + def test_it(): + pass + """ + ) + + result = pytester.runpytest(mode, p) + assert result.ret == 0 + + result.stdout.fnmatch_lines( + [ + " SETUP F dep", + " SETUP F auto_fix (fixtures used: dep)[[]'foo'[]] [[]autouse[]]", + "*test_it[[]foo[]] (fixtures used: auto_fix, dep)*", + " TEARDOWN F auto_fix[[]'foo'[]] [[]autouse[]]", + " TEARDOWN F dep", ] )