Skip to content
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

Add package property to FixtureRequest #12853

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
11 changes: 11 additions & 0 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,17 @@
assert mod is not None
return mod.obj

@property
def package(self):
"""Python package object where the test function was collected."""
if self.scope not in ("function", "class", "module", "package"):
raise AttributeError(

Check warning on line 471 in src/_pytest/fixtures.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/fixtures.py#L471

Added line #L471 was not covered by tests
f"package not available in {self.scope}-scoped context"
)
pkg = self._pyfuncitem.getparent(_pytest.python.Package)
assert pkg is not None
return pkg.obj

Check warning on line 476 in src/_pytest/fixtures.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/fixtures.py#L474-L476

Added lines #L474 - L476 were not covered by tests

@property
def path(self) -> Path:
"""Path where the test function was collected."""
Expand Down
4 changes: 3 additions & 1 deletion testing/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1366,7 +1366,7 @@ def auto(request):
result.stdout.no_fnmatch_line("*FAILURES*")
result.stdout.fnmatch_lines(["*=== 1 passed in *"])

@pytest.mark.parametrize("scope", SCOPES)
@pytest.mark.parametrize("scope", [*SCOPES, "package"])
def test_auto_use_request_attributes(self, pytester, scope):
"""Check that all attributes of a request in an autouse fixture
behave as expected when requested for a doctest item.
Expand All @@ -1377,6 +1377,8 @@ def test_auto_use_request_attributes(self, pytester, scope):

@pytest.fixture(autouse=True, scope="{scope}")
def auto(request):
if "{scope}" == 'package':
assert request.package is None
if "{scope}" == 'module':
assert request.module is None
if "{scope}" == 'class':
Expand Down
Loading