Skip to content

Commit 2e8fb9f

Browse files
bluetechnicoddemus
authored andcommitted
fixtures: extract a _check_fixturedef method
This stuff is less interesting when reading `_get_active_fixturedef`.
1 parent acf2971 commit 2e8fb9f

File tree

1 file changed

+35
-38
lines changed

1 file changed

+35
-38
lines changed

src/_pytest/fixtures.py

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,6 @@ def _get_active_fixturedef(
560560
# The are no fixtures with this name applicable for the function.
561561
if not fixturedefs:
562562
raise FixtureLookupError(argname, self)
563-
564563
# A fixture may override another fixture with the same name, e.g. a
565564
# fixture in a module can override a fixture in a conftest, a fixture in
566565
# a class can override a fixture in the module, and so on.
@@ -577,13 +576,11 @@ def _get_active_fixturedef(
577576
# If already consumed all of the available levels, fail.
578577
if -index > len(fixturedefs):
579578
raise FixtureLookupError(argname, self)
580-
581579
fixturedef = fixturedefs[index]
582580

583581
# Prepare a SubRequest object for calling the fixture.
584-
funcitem = self._pyfuncitem
585582
try:
586-
callspec = funcitem.callspec
583+
callspec = self._pyfuncitem.callspec
587584
except AttributeError:
588585
callspec = None
589586
if callspec is not None and argname in callspec.params:
@@ -595,41 +592,8 @@ def _get_active_fixturedef(
595592
param = NOTSET
596593
param_index = 0
597594
scope = fixturedef._scope
598-
599-
has_params = fixturedef.params is not None
600-
fixtures_not_supported = getattr(funcitem, "nofuncargs", False)
601-
if has_params and fixtures_not_supported:
602-
msg = (
603-
f"{funcitem.name} does not support fixtures, maybe unittest.TestCase subclass?\n"
604-
f"Node id: {funcitem.nodeid}\n"
605-
f"Function type: {type(funcitem).__name__}"
606-
)
607-
fail(msg, pytrace=False)
608-
if has_params:
609-
frame = inspect.stack()[2]
610-
frameinfo = inspect.getframeinfo(frame[0])
611-
source_path = absolutepath(frameinfo.filename)
612-
source_lineno = frameinfo.lineno
613-
try:
614-
source_path_str = str(
615-
source_path.relative_to(funcitem.config.rootpath)
616-
)
617-
except ValueError:
618-
source_path_str = str(source_path)
619-
location = getlocation(fixturedef.func, funcitem.config.rootpath)
620-
msg = (
621-
"The requested fixture has no parameter defined for test:\n"
622-
f" {funcitem.nodeid}\n\n"
623-
f"Requested fixture '{fixturedef.argname}' defined in:\n"
624-
f"{location}\n\n"
625-
f"Requested here:\n"
626-
f"{source_path_str}:{source_lineno}"
627-
)
628-
fail(msg, pytrace=False)
629-
630-
# Check if a higher-level scoped fixture accesses a lower level one.
595+
self._check_fixturedef_without_param(fixturedef)
631596
self._check_scope(fixturedef, scope)
632-
633597
subrequest = SubRequest(
634598
self, scope, param, param_index, fixturedef, _ispytest=True
635599
)
@@ -640,6 +604,39 @@ def _get_active_fixturedef(
640604
self._fixture_defs[argname] = fixturedef
641605
return fixturedef
642606

607+
def _check_fixturedef_without_param(self, fixturedef: "FixtureDef[object]") -> None:
608+
"""Check that this request is allowed to execute this fixturedef without
609+
a param."""
610+
funcitem = self._pyfuncitem
611+
has_params = fixturedef.params is not None
612+
fixtures_not_supported = getattr(funcitem, "nofuncargs", False)
613+
if has_params and fixtures_not_supported:
614+
msg = (
615+
f"{funcitem.name} does not support fixtures, maybe unittest.TestCase subclass?\n"
616+
f"Node id: {funcitem.nodeid}\n"
617+
f"Function type: {type(funcitem).__name__}"
618+
)
619+
fail(msg, pytrace=False)
620+
if has_params:
621+
frame = inspect.stack()[3]
622+
frameinfo = inspect.getframeinfo(frame[0])
623+
source_path = absolutepath(frameinfo.filename)
624+
source_lineno = frameinfo.lineno
625+
try:
626+
source_path_str = str(source_path.relative_to(funcitem.config.rootpath))
627+
except ValueError:
628+
source_path_str = str(source_path)
629+
location = getlocation(fixturedef.func, funcitem.config.rootpath)
630+
msg = (
631+
"The requested fixture has no parameter defined for test:\n"
632+
f" {funcitem.nodeid}\n\n"
633+
f"Requested fixture '{fixturedef.argname}' defined in:\n"
634+
f"{location}\n\n"
635+
f"Requested here:\n"
636+
f"{source_path_str}:{source_lineno}"
637+
)
638+
fail(msg, pytrace=False)
639+
643640
def _get_fixturestack(self) -> List["FixtureDef[Any]"]:
644641
values = [request._fixturedef for request in self._iter_chain()]
645642
values.reverse()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy