Skip to content

Commit 8ee6d0a

Browse files
committed
Always use getfixturemarker() to access _pytestfixturefunction
Keep knowledge of how the marker is stored encapsulated in one place.
1 parent ade253c commit 8ee6d0a

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/_pytest/nose.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
from _pytest import python
33
from _pytest import unittest
44
from _pytest.config import hookimpl
5+
from _pytest.fixtures import getfixturemarker
56
from _pytest.nodes import Item
67

78

89
@hookimpl(trylast=True)
9-
def pytest_runtest_setup(item):
10+
def pytest_runtest_setup(item) -> None:
1011
if is_potential_nosetest(item):
1112
if not call_optional(item.obj, "setup"):
1213
# Call module level setup if there is no object level one.
@@ -15,7 +16,7 @@ def pytest_runtest_setup(item):
1516
item.session._setupstate.addfinalizer((lambda: teardown_nose(item)), item)
1617

1718

18-
def teardown_nose(item):
19+
def teardown_nose(item) -> None:
1920
if is_potential_nosetest(item):
2021
if not call_optional(item.obj, "teardown"):
2122
call_optional(item.parent.obj, "teardown")
@@ -29,11 +30,16 @@ def is_potential_nosetest(item: Item) -> bool:
2930
)
3031

3132

32-
def call_optional(obj, name):
33+
def call_optional(obj: object, name: str) -> bool:
3334
method = getattr(obj, name, None)
34-
isfixture = hasattr(method, "_pytestfixturefunction")
35-
if method is not None and not isfixture and callable(method):
36-
# If there's any problems allow the exception to raise rather than
37-
# silently ignoring them.
38-
method()
39-
return True
35+
if method is None:
36+
return False
37+
is_fixture = getfixturemarker(method) is not None
38+
if is_fixture:
39+
return False
40+
if not callable(method):
41+
return False
42+
# If there are any problems allow the exception to raise rather than
43+
# silently ignoring it.
44+
method()
45+
return True

testing/python/integration.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44
from _pytest import runner
55
from _pytest._code import getfslineno
6+
from _pytest.fixtures import getfixturemarker
67
from _pytest.pytester import Pytester
78

89

@@ -334,7 +335,8 @@ def test_fix(fix):
334335
def test_pytestconfig_is_session_scoped() -> None:
335336
from _pytest.fixtures import pytestconfig
336337

337-
marker = pytestconfig._pytestfixturefunction # type: ignore
338+
marker = getfixturemarker(pytestconfig)
339+
assert marker is not None
338340
assert marker.scope == "session"
339341

340342

0 commit comments

Comments
 (0)