Skip to content

fixtures: some tweaks & improvements #11209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 15, 2023
Merged
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
2 changes: 2 additions & 0 deletions changelog/11208.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The (internal) ``FixtureDef.cached_result`` type has changed.
Now the third item ``cached_result[2]``, when set, is an exception instance instead of an exception triplet.
1 change: 1 addition & 0 deletions changelog/7469.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:class:`~pytest.FixtureDef` is now exported as ``pytest.FixtureDef`` for typing purposes.
2 changes: 1 addition & 1 deletion doc/en/reference/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ ExitCode
FixtureDef
~~~~~~~~~~

.. autoclass:: _pytest.fixtures.FixtureDef()
.. autoclass:: pytest.FixtureDef()
:members:
:show-inheritance:

Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def is_async_function(func: object) -> bool:
return iscoroutinefunction(func) or inspect.isasyncgenfunction(func)


def getlocation(function, curdir: str | None = None) -> str:
def getlocation(function, curdir: str | os.PathLike[str] | None = None) -> str:
function = get_real_func(function)
fn = Path(inspect.getfile(function))
lineno = function.__code__.co_firstlineno
Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ def func() -> None:
doctest_item._fixtureinfo = fm.getfixtureinfo( # type: ignore[attr-defined]
node=doctest_item, func=func, cls=None, funcargs=False
)
fixture_request = FixtureRequest(doctest_item, _ispytest=True)
fixture_request = FixtureRequest(doctest_item, _ispytest=True) # type: ignore[arg-type]
fixture_request._fillfixtures()
return fixture_request

Expand Down
174 changes: 102 additions & 72 deletions src/_pytest/fixtures.py

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from _pytest.debugging import pytestPDB as __pytestPDB
from _pytest.doctest import DoctestItem
from _pytest.fixtures import fixture
from _pytest.fixtures import FixtureDef
from _pytest.fixtures import FixtureLookupError
from _pytest.fixtures import FixtureRequest
from _pytest.fixtures import yield_fixture
Expand Down Expand Up @@ -102,6 +103,7 @@
"fail",
"File",
"fixture",
"FixtureDef",
"FixtureLookupError",
"FixtureRequest",
"freeze_includes",
Expand Down
9 changes: 9 additions & 0 deletions testing/python/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ def test_method(self, something):
"""
)
(item1,) = pytester.genitems([modcol])
assert isinstance(item1, Function)
assert item1.name == "test_method"
arg2fixturedefs = fixtures.FixtureRequest(
item1, _ispytest=True
Expand Down Expand Up @@ -967,6 +968,7 @@ def test_second():
def test_request_getmodulepath(self, pytester: Pytester) -> None:
modcol = pytester.getmodulecol("def test_somefunc(): pass")
(item,) = pytester.genitems([modcol])
assert isinstance(item, Function)
req = fixtures.FixtureRequest(item, _ispytest=True)
assert req.path == modcol.path

Expand Down Expand Up @@ -1125,6 +1127,7 @@ def test_func2(self, something):
pass
"""
)
assert isinstance(item1, Function)
req1 = fixtures.FixtureRequest(item1, _ispytest=True)
assert "xfail" not in item1.keywords
req1.applymarker(pytest.mark.xfail)
Expand Down Expand Up @@ -4009,6 +4012,7 @@ def test_func(m1):
"""
)
items, _ = pytester.inline_genitems()
assert isinstance(items[0], Function)
request = FixtureRequest(items[0], _ispytest=True)
assert request.fixturenames == "m1 f1".split()

Expand Down Expand Up @@ -4057,6 +4061,7 @@ def test_foo(f1, p1, m1, f2, s1): pass
"""
)
items, _ = pytester.inline_genitems()
assert isinstance(items[0], Function)
request = FixtureRequest(items[0], _ispytest=True)
# order of fixtures based on their scope and position in the parameter list
assert (
Expand Down Expand Up @@ -4084,6 +4089,7 @@ def test_func(f1, m1):
"""
)
items, _ = pytester.inline_genitems()
assert isinstance(items[0], Function)
request = FixtureRequest(items[0], _ispytest=True)
assert request.fixturenames == "m1 f1".split()

Expand Down Expand Up @@ -4117,6 +4123,7 @@ def test_func(self, f2, f1, c1, m1, s1):
"""
)
items, _ = pytester.inline_genitems()
assert isinstance(items[0], Function)
request = FixtureRequest(items[0], _ispytest=True)
assert request.fixturenames == "s1 m1 c1 f2 f1".split()

Expand Down Expand Up @@ -4159,6 +4166,7 @@ def test_func(m_test, f1):
}
)
items, _ = pytester.inline_genitems()
assert isinstance(items[0], Function)
request = FixtureRequest(items[0], _ispytest=True)
assert request.fixturenames == "p_sub m_conf m_sub m_test f1".split()

Expand Down Expand Up @@ -4203,6 +4211,7 @@ def test_func(self, f2, f1, m2):
"""
)
items, _ = pytester.inline_genitems()
assert isinstance(items[0], Function)
request = FixtureRequest(items[0], _ispytest=True)
assert request.fixturenames == "s1 p1 m1 m2 c1 f2 f1".split()

Expand Down
1 change: 1 addition & 0 deletions testing/test_legacypath.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def test_cache_makedir(cache: pytest.Cache) -> None:
def test_fixturerequest_getmodulepath(pytester: pytest.Pytester) -> None:
modcol = pytester.getmodulecol("def test_somefunc(): pass")
(item,) = pytester.genitems([modcol])
assert isinstance(item, pytest.Function)
req = pytest.FixtureRequest(item, _ispytest=True)
assert req.path == modcol.path
assert req.fspath == modcol.fspath # type: ignore[attr-defined]
Expand Down