Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Oct 5, 2024
1 parent 978085e commit 3e31f07
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 28 deletions.
18 changes: 13 additions & 5 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,11 @@ def getfixturevalue(self, argname: str) -> Any:
"This can happen when the fixture has already been torn down."
)

if (isinstance(fixturedef, FixtureDef)
and fixturedef is not None
and fixturedef.scope == Scope.Invocation.value):
if (
isinstance(fixturedef, FixtureDef)
and fixturedef is not None
and fixturedef.scope == Scope.Invocation.value
):
self._fixture_defs.pop(argname)

return fixturedef.cached_result[0]
Expand Down Expand Up @@ -626,7 +628,10 @@ def _get_active_fixturedef(
finally:
for arg_name in fixturedef.argnames:
arg_fixture = self._fixture_defs.get(arg_name)
if arg_fixture is not None and arg_fixture.scope == Scope.Invocation.value:
if (
arg_fixture is not None
and arg_fixture.scope == Scope.Invocation.value
):
self._fixture_defs.pop(arg_name)

return fixturedef
Expand Down Expand Up @@ -769,7 +774,10 @@ def _check_scope(
requested_fixturedef: FixtureDef[object] | PseudoFixtureDef[object],
requested_scope: Scope,
) -> None:
if isinstance(requested_fixturedef, PseudoFixtureDef) or requested_scope == Scope.Invocation:
if (
isinstance(requested_fixturedef, PseudoFixtureDef)
or requested_scope == Scope.Invocation
):
return
if self._scope > requested_scope:
# Try to report something helpful.
Expand Down
4 changes: 3 additions & 1 deletion src/_pytest/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,6 @@ def from_user(


# Ordered list of scopes which can contain many tests (in practice all except Function).
HIGH_SCOPES = [x for x in Scope if x is not Scope.Function and x is not Scope.Invocation]
HIGH_SCOPES = [
x for x in Scope if x is not Scope.Function and x is not Scope.Invocation
]
60 changes: 38 additions & 22 deletions testing/test_no_cache.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from __future__ import annotations

from _pytest.pytester import Pytester


def test_setup_teardown_executed_for_every_fixture_usage_without_caching(pytester: Pytester) -> None:
def test_setup_teardown_executed_for_every_fixture_usage_without_caching(
pytester: Pytester,
) -> None:
pytester.makepyfile(
"""
import pytest
Expand All @@ -26,17 +30,22 @@ def b(fixt):
def test(a, b, fixt):
assert False
""")
"""
)

result = pytester.runpytest("--log-level=INFO")
assert result.ret == 1
result.stdout.fnmatch_lines([
*["*&&Setting up fixt&&*"] * 3,
*["*&&Tearing down fixt&&*"] * 3,
])
result.stdout.fnmatch_lines(
[
*["*&&Setting up fixt&&*"] * 3,
*["*&&Tearing down fixt&&*"] * 3,
]
)


def test_setup_teardown_executed_for_every_getfixturevalue_usage_without_caching(pytester: Pytester) -> None:
def test_setup_teardown_executed_for_every_getfixturevalue_usage_without_caching(
pytester: Pytester,
) -> None:
pytester.makepyfile(
"""
import pytest
Expand All @@ -56,13 +65,17 @@ def test(request):
)
result = pytester.runpytest("--log-level=INFO")
assert result.ret == 1
result.stdout.fnmatch_lines([
*["*&&Setting up fixt&&*"] * 3,
*["*&&Tearing down fixt&&*"] * 3,
])
result.stdout.fnmatch_lines(
[
*["*&&Setting up fixt&&*"] * 3,
*["*&&Tearing down fixt&&*"] * 3,
]
)


def test_non_cached_fixture_generates_unique_values_per_usage(pytester: Pytester) -> None:
def test_non_cached_fixture_generates_unique_values_per_usage(
pytester: Pytester,
) -> None:
pytester.makepyfile(
"""
import pytest
Expand All @@ -71,25 +84,28 @@ def test_non_cached_fixture_generates_unique_values_per_usage(pytester: Pytester
def random_num():
import random
return random.randint(-100_000_000_000, 100_000_000_000)
@pytest.fixture()
def a(random_num):
return random_num
@pytest.fixture()
def b(random_num):
return random_num
def test(a, b, random_num):
assert a != b != random_num
""")
"""
)
pytester.runpytest().assert_outcomes(passed=1)


def test_non_cached_fixture_generates_unique_values_per_getfixturevalue_usage(pytester: Pytester) -> None:
def test_non_cached_fixture_generates_unique_values_per_getfixturevalue_usage(
pytester: Pytester,
) -> None:
pytester.makepyfile(
"""
import pytest
Expand All @@ -98,8 +114,8 @@ def test_non_cached_fixture_generates_unique_values_per_getfixturevalue_usage(py
def random_num():
import random
yield random.randint(-100_000_000_000, 100_000_000_000)
def test(request):
random_nums = [request.getfixturevalue('random_num') for _ in range(3)]
assert random_nums[0] != random_nums[1] != random_nums[2]
Expand Down

0 comments on commit 3e31f07

Please sign in to comment.