Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Alex Lambson
Alex Chen
Alexander Johnson
Alexander King
Alexander683
Alexei Kozlenok
algojogacor
Alice Purcell
Expand Down
1 change: 1 addition & 0 deletions changelog/14920.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``--setup-show``, ``--setup-only``, ``--setup-plan``, and ``--fixtures-per-test`` now mark autouse fixtures with ``[autouse]``.
7 changes: 4 additions & 3 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/_pytest/setuponly.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
64 changes: 64 additions & 0 deletions testing/python/show_fixtures_per_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
35 changes: 33 additions & 2 deletions testing/test_setuponly.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
)

Expand Down
Loading