summaryrefslogtreecommitdiff
path: root/src/_pytest/fixtures.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/_pytest/fixtures.py')
-rw-r--r--src/_pytest/fixtures.py334
1 files changed, 168 insertions, 166 deletions
diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py
index d9f918745..5dbaf9e06 100644
--- a/src/_pytest/fixtures.py
+++ b/src/_pytest/fixtures.py
@@ -166,15 +166,16 @@ def get_scope_node(node, scope):
def add_funcarg_pseudo_fixture_def(
collector, metafunc: "Metafunc", fixturemanager: "FixtureManager"
) -> None:
- # this function will transform all collected calls to a functions
+ # This function will transform all collected calls to functions
# if they use direct funcargs (i.e. direct parametrization)
# because we want later test execution to be able to rely on
# an existing FixtureDef structure for all arguments.
# XXX we can probably avoid this algorithm if we modify CallSpec2
# to directly care for creating the fixturedefs within its methods.
if not metafunc._calls[0].funcargs:
- return # this function call does not have direct parametrization
- # collect funcargs of all callspecs into a list of values
+ # This function call does not have direct parametrization.
+ return
+ # Collect funcargs of all callspecs into a list of values.
arg2params = {} # type: Dict[str, List[object]]
arg2scope = {} # type: Dict[str, _Scope]
for callspec in metafunc._calls:
@@ -189,11 +190,11 @@ def add_funcarg_pseudo_fixture_def(
arg2scope[argname] = scopes[scopenum]
callspec.funcargs.clear()
- # register artificial FixtureDef's so that later at test execution
+ # Register artificial FixtureDef's so that later at test execution
# time we can rely on a proper FixtureDef to exist for fixture setup.
arg2fixturedefs = metafunc._arg2fixturedefs
for argname, valuelist in arg2params.items():
- # if we have a scope that is higher than function we need
+ # If we have a scope that is higher than function, we need
# to make sure we only ever create an according fixturedef on
# a per-scope basis. We thus store and cache the fixturedef on the
# node related to the scope.
@@ -203,7 +204,7 @@ def add_funcarg_pseudo_fixture_def(
node = get_scope_node(collector, scope)
if node is None:
assert scope == "class" and isinstance(collector, _pytest.python.Module)
- # use module-level collector for class-scope (for now)
+ # Use module-level collector for class-scope (for now).
node = collector
if node and argname in node._name2pseudofixturedef:
arg2fixturedefs[argname] = [node._name2pseudofixturedef[argname]]
@@ -224,7 +225,7 @@ def add_funcarg_pseudo_fixture_def(
def getfixturemarker(obj: object) -> Optional["FixtureFunctionMarker"]:
- """ return fixturemarker or None if it doesn't exist or raised
+ """Return fixturemarker or None if it doesn't exist or raised
exceptions."""
try:
fixturemarker = getattr(
@@ -242,7 +243,7 @@ _Key = Tuple[object, ...]
def get_parametrized_fixture_keys(item: "nodes.Item", scopenum: int) -> Iterator[_Key]:
- """ return list of keys for all parametrized arguments which match
+ """Return list of keys for all parametrized arguments which match
the specified scope. """
assert scopenum < scopenum_function # function
try:
@@ -269,10 +270,10 @@ def get_parametrized_fixture_keys(item: "nodes.Item", scopenum: int) -> Iterator
yield key
-# algorithm for sorting on a per-parametrized resource setup basis
-# it is called for scopenum==0 (session) first and performs sorting
+# Algorithm for sorting on a per-parametrized resource setup basis.
+# It is called for scopenum==0 (session) first and performs sorting
# down to the lower scopes such as to minimize number of "high scope"
-# setups and teardowns
+# setups and teardowns.
def reorder_items(items: "Sequence[nodes.Item]") -> "List[nodes.Item]":
@@ -339,7 +340,8 @@ def reorder_items_atscope(
no_argkey_group[item] = None
else:
slicing_argkey, _ = argkeys.popitem()
- # we don't have to remove relevant items from later in the deque because they'll just be ignored
+ # We don't have to remove relevant items from later in the
+ # deque because they'll just be ignored.
matching_items = [
i for i in scoped_items_by_argkey[slicing_argkey] if i in items
]
@@ -358,7 +360,7 @@ def reorder_items_atscope(
def fillfixtures(function: "Function") -> None:
- """ fill missing funcargs for a test function. """
+ """Fill missing funcargs for a test function."""
# Uncomment this after 6.0 release (#7361)
# warnings.warn(FILLFUNCARGS, stacklevel=2)
try:
@@ -373,7 +375,7 @@ def fillfixtures(function: "Function") -> None:
function._fixtureinfo = fi
request = function._request = FixtureRequest(function)
request._fillfixtures()
- # prune out funcargs for jstests
+ # Prune out funcargs for jstests.
newfuncargs = {}
for name in fi.argnames:
newfuncargs[name] = function.funcargs[name]
@@ -388,9 +390,9 @@ def get_direct_param_fixture_func(request):
@attr.s(slots=True)
class FuncFixtureInfo:
- # original function argument names
+ # Original function argument names.
argnames = attr.ib(type=Tuple[str, ...])
- # argnames that function immediately requires. These include argnames +
+ # Argnames that function immediately requires. These include argnames +
# fixture names specified via usefixtures and via autouse=True in fixture
# definitions.
initialnames = attr.ib(type=Tuple[str, ...])
@@ -398,7 +400,7 @@ class FuncFixtureInfo:
name2fixturedefs = attr.ib(type=Dict[str, Sequence["FixtureDef"]])
def prune_dependency_tree(self) -> None:
- """Recompute names_closure from initialnames and name2fixturedefs
+ """Recompute names_closure from initialnames and name2fixturedefs.
Can only reduce names_closure, which means that the new closure will
always be a subset of the old one. The order is preserved.
@@ -412,7 +414,7 @@ class FuncFixtureInfo:
working_set = set(self.initialnames)
while working_set:
argname = working_set.pop()
- # argname may be smth not included in the original names_closure,
+ # Argname may be smth not included in the original names_closure,
# in which case we ignore it. This currently happens with pseudo
# FixtureDefs which wrap 'get_direct_param_fixture_func(request)'.
# So they introduce the new dependency 'request' which might have
@@ -426,18 +428,18 @@ class FuncFixtureInfo:
class FixtureRequest:
- """ A request for a fixture from a test or fixture function.
+ """A request for a fixture from a test or fixture function.
- A request object gives access to the requesting test context
- and has an optional ``param`` attribute in case
- the fixture is parametrized indirectly.
+ A request object gives access to the requesting test context and has
+ an optional ``param`` attribute in case the fixture is parametrized
+ indirectly.
"""
def __init__(self, pyfuncitem) -> None:
self._pyfuncitem = pyfuncitem
- #: fixture for which this request is being performed
+ #: Fixture for which this request is being performed.
self.fixturename = None # type: Optional[str]
- #: Scope string, one of "function", "class", "module", "session"
+ #: Scope string, one of "function", "class", "module", "session".
self.scope = "function" # type: _Scope
self._fixture_defs = {} # type: Dict[str, FixtureDef]
fixtureinfo = pyfuncitem._fixtureinfo # type: FuncFixtureInfo
@@ -449,35 +451,35 @@ class FixtureRequest:
@property
def fixturenames(self) -> List[str]:
- """names of all active fixtures in this request"""
+ """Names of all active fixtures in this request."""
result = list(self._pyfuncitem._fixtureinfo.names_closure)
result.extend(set(self._fixture_defs).difference(result))
return result
@property
def funcargnames(self) -> List[str]:
- """ alias attribute for ``fixturenames`` for pre-2.3 compatibility"""
+ """Alias attribute for ``fixturenames`` for pre-2.3 compatibility."""
warnings.warn(FUNCARGNAMES, stacklevel=2)
return self.fixturenames
@property
def node(self):
- """ underlying collection node (depends on current request scope)"""
+ """Underlying collection node (depends on current request scope)."""
return self._getscopeitem(self.scope)
def _getnextfixturedef(self, argname: str) -> "FixtureDef":
fixturedefs = self._arg2fixturedefs.get(argname, None)
if fixturedefs is None:
- # we arrive here because of a dynamic call to
+ # We arrive here because of a dynamic call to
# getfixturevalue(argname) usage which was naturally
- # not known at parsing/collection time
+ # not known at parsing/collection time.
assert self._pyfuncitem.parent is not None
parentid = self._pyfuncitem.parent.nodeid
fixturedefs = self._fixturemanager.getfixturedefs(argname, parentid)
# TODO: Fix this type ignore. Either add assert or adjust types.
# Can this be None here?
self._arg2fixturedefs[argname] = fixturedefs # type: ignore[assignment]
- # fixturedefs list is immutable so we maintain a decreasing index
+ # fixturedefs list is immutable so we maintain a decreasing index.
index = self._arg2index.get(argname, 0) - 1
if fixturedefs is None or (-index > len(fixturedefs)):
raise FixtureLookupError(argname, self)
@@ -486,25 +488,25 @@ class FixtureRequest:
@property
def config(self) -> Config:
- """ the pytest config object associated with this request. """
+ """The pytest config object associated with this request."""
return self._pyfuncitem.config # type: ignore[no-any-return] # noqa: F723
@scopeproperty()
def function(self):
- """ test function object if the request has a per-function scope. """
+ """Test function object if the request has a per-function scope."""
return self._pyfuncitem.obj
@scopeproperty("class")
def cls(self):
- """ class (can be None) where the test function was collected. """
+ """Class (can be None) where the test function was collected."""
clscol = self._pyfuncitem.getparent(_pytest.python.Class)
if clscol:
return clscol.obj
@property
def instance(self):
- """ instance (can be None) on which test function was collected. """
- # unittest support hack, see _pytest.unittest.TestCaseFunction
+ """Instance (can be None) on which test function was collected."""
+ # unittest support hack, see _pytest.unittest.TestCaseFunction.
try:
return self._pyfuncitem._testcase
except AttributeError:
@@ -513,30 +515,29 @@ class FixtureRequest:
@scopeproperty()
def module(self):
- """ python module object where the test function was collected. """
+ """Python module object where the test function was collected."""
return self._pyfuncitem.getparent(_pytest.python.Module).obj
@scopeproperty()
def fspath(self) -> py.path.local:
- """ the file system path of the test module which collected this test. """
+ """The file system path of the test module which collected this test."""
# TODO: Remove ignore once _pyfuncitem is properly typed.
return self._pyfuncitem.fspath # type: ignore
@property
def keywords(self):
- """ keywords/markers dictionary for the underlying node. """
+ """Keywords/markers dictionary for the underlying node."""
return self.node.keywords
@property
def session(self):
- """ pytest session object. """
+ """Pytest session object."""
return self._pyfuncitem.session
def addfinalizer(self, finalizer: Callable[[], object]) -> None:
- """ add finalizer/teardown function to be called after the
- last test within the requesting test context finished
- execution. """
- # XXX usually this method is shadowed by fixturedef specific ones
+ """Add finalizer/teardown function to be called after the last test
+ within the requesting test context finished execution."""
+ # XXX usually this method is shadowed by fixturedef specific ones.
self._addfinalizer(finalizer, scope=self.scope)
def _addfinalizer(self, finalizer: Callable[[], object], scope) -> None:
@@ -546,17 +547,19 @@ class FixtureRequest:
)
def applymarker(self, marker) -> None:
- """ Apply a marker to a single test function invocation.
+ """Apply a marker to a single test function invocation.
+
This method is useful if you don't want to have a keyword/marker
on all function invocations.
- :arg marker: a :py:class:`_pytest.mark.MarkDecorator` object
- created by a call to ``pytest.mark.NAME(...)``.
+ :param marker:
+ A :py:class:`_pytest.mark.MarkDecorator` object created by a call
+ to ``pytest.mark.NAME(...)``.
"""
self.node.add_marker(marker)
def raiseerror(self, msg: Optional[str]) -> "NoReturn":
- """ raise a FixtureLookupError with the given message. """
+ """Raise a FixtureLookupError with the given message."""
raise self._fixturemanager.FixtureLookupError(None, self, msg)
def _fillfixtures(self) -> None:
@@ -567,14 +570,14 @@ class FixtureRequest:
item.funcargs[argname] = self.getfixturevalue(argname)
def getfixturevalue(self, argname: str) -> Any:
- """ Dynamically run a named fixture function.
+ """Dynamically run a named fixture function.
Declaring fixtures via function argument is recommended where possible.
But if you can only decide whether to use another fixture at test
setup time, you may use this function to retrieve it inside a fixture
or test function body.
- :raise pytest.FixtureLookupError:
+ :raises pytest.FixtureLookupError:
If the given fixture could not be found.
"""
fixturedef = self._get_active_fixturedef(argname)
@@ -595,8 +598,8 @@ class FixtureRequest:
scope = "function" # type: _Scope
return PseudoFixtureDef(cached_result, scope)
raise
- # remove indent to prevent the python3 exception
- # from leaking into the call
+ # Remove indent to prevent the python3 exception
+ # from leaking into the call.
self._compute_fixture_value(fixturedef)
self._fixture_defs[argname] = fixturedef
return fixturedef
@@ -614,10 +617,12 @@ class FixtureRequest:
current = current._parent_request
def _compute_fixture_value(self, fixturedef: "FixtureDef") -> None:
- """
- Creates a SubRequest based on "self" and calls the execute method of the given fixturedef object. This will
- force the FixtureDef object to throw away any previous results and compute a new fixture value, which
- will be stored into the FixtureDef object itself.
+ """Create a SubRequest based on "self" and call the execute method
+ of the given FixtureDef object.
+
+ This will force the FixtureDef object to throw away any previous
+ results and compute a new fixture value, which will be stored into
+ the FixtureDef object itself.
"""
# prepare a subrequest object before calling fixture function
# (latter managed by fixturedef)
@@ -667,18 +672,18 @@ class FixtureRequest:
fail(msg, pytrace=False)
else:
param_index = funcitem.callspec.indices[argname]
- # if a parametrize invocation set a scope it will override
- # the static scope defined with the fixture function
+ # If a parametrize invocation set a scope it will override
+ # the static scope defined with the fixture function.
paramscopenum = funcitem.callspec._arg2scopenum.get(argname)
if paramscopenum is not None:
scope = scopes[paramscopenum]
subrequest = SubRequest(self, scope, param, param_index, fixturedef)
- # check if a higher-level scoped fixture accesses a lower level one
+ # Check if a higher-level scoped fixture accesses a lower level one.
subrequest._check_scope(argname, self.scope, scope)
try:
- # call the fixture function
+ # Call the fixture function.
fixturedef.execute(request=subrequest)
finally:
self._schedule_finalizers(fixturedef, subrequest)
@@ -686,7 +691,7 @@ class FixtureRequest:
def _schedule_finalizers(
self, fixturedef: "FixtureDef", subrequest: "SubRequest"
) -> None:
- # if fixture function failed it might have registered finalizers
+ # If fixture function failed it might have registered finalizers.
self.session._setupstate.addfinalizer(
functools.partial(fixturedef.finish, request=subrequest), subrequest.node
)
@@ -695,7 +700,7 @@ class FixtureRequest:
if argname == "request":
return
if scopemismatch(invoking_scope, requested_scope):
- # try to report something helpful
+ # Try to report something helpful.
lines = self._factorytraceback()
fail(
"ScopeMismatch: You tried to access the %r scoped "
@@ -717,7 +722,7 @@ class FixtureRequest:
def _getscopeitem(self, scope):
if scope == "function":
- # this might also be a non-function Item despite its attribute name
+ # This might also be a non-function Item despite its attribute name.
return self._pyfuncitem
if scope == "package":
# FIXME: _fixturedef is not defined on FixtureRequest (this class),
@@ -726,7 +731,7 @@ class FixtureRequest:
else:
node = get_scope_node(self._pyfuncitem, scope)
if node is None and scope == "class":
- # fallback to function item itself
+ # Fallback to function item itself.
node = self._pyfuncitem
assert node, 'Could not obtain a node for scope "{}" for function {!r}'.format(
scope, self._pyfuncitem
@@ -738,8 +743,7 @@ class FixtureRequest:
class SubRequest(FixtureRequest):
- """ a sub request for handling getting a fixture from a
- test function/fixture. """
+ """A sub request for handling getting a fixture from a test function/fixture."""
def __init__(
self,
@@ -750,7 +754,7 @@ class SubRequest(FixtureRequest):
fixturedef: "FixtureDef",
) -> None:
self._parent_request = request
- self.fixturename = fixturedef.argname # type: str
+ self.fixturename = fixturedef.argname
if param is not NOTSET:
self.param = param
self.param_index = param_index
@@ -771,9 +775,9 @@ class SubRequest(FixtureRequest):
def _schedule_finalizers(
self, fixturedef: "FixtureDef", subrequest: "SubRequest"
) -> None:
- # if the executing fixturedef was not explicitly requested in the argument list (via
+ # If the executing fixturedef was not explicitly requested in the argument list (via
# getfixturevalue inside the fixture call) then ensure this fixture def will be finished
- # first
+ # first.
if fixturedef.argname not in self.fixturenames:
fixturedef.addfinalizer(
functools.partial(self._fixturedef.finish, request=self)
@@ -791,8 +795,7 @@ def scopemismatch(currentscope: "_Scope", newscope: "_Scope") -> bool:
def scope2index(scope: str, descr: str, where: Optional[str] = None) -> int:
"""Look up the index of ``scope`` and raise a descriptive value error
- if not defined.
- """
+ if not defined."""
strscopes = scopes # type: Sequence[str]
try:
return strscopes.index(scope)
@@ -806,7 +809,7 @@ def scope2index(scope: str, descr: str, where: Optional[str] = None) -> int:
class FixtureLookupError(LookupError):
- """ could not return a requested Fixture (missing or invalid). """
+ """Could not return a requested fixture (missing or invalid)."""
def __init__(
self, argname: Optional[str], request: FixtureRequest, msg: Optional[str] = None
@@ -823,8 +826,8 @@ class FixtureLookupError(LookupError):
stack.extend(map(lambda x: x.func, self.fixturestack))
msg = self.msg
if msg is not None:
- # the last fixture raise an error, let's present
- # it at the requesting side
+ # The last fixture raise an error, let's present
+ # it at the requesting side.
stack = stack[:-1]
for function in stack:
fspath, lineno = getfslineno(function)
@@ -925,8 +928,9 @@ def call_fixture_func(
def _teardown_yield_fixture(fixturefunc, it) -> None:
- """Executes the teardown of a fixture function by advancing the iterator after the
- yield and ensure the iteration ends (if not it means there is more than one yield in the function)"""
+ """Execute the teardown of a fixture function by advancing the iterator
+ after the yield and ensure the iteration ends (if not it means there is
+ more than one yield in the function)."""
try:
next(it)
except StopIteration:
@@ -961,7 +965,7 @@ def _eval_scope_callable(
class FixtureDef(Generic[_FixtureValue]):
- """ A container for a factory definition. """
+ """A container for a factory definition."""
def __init__(
self,
@@ -1023,16 +1027,15 @@ class FixtureDef(Generic[_FixtureValue]):
finally:
hook = self._fixturemanager.session.gethookproxy(request.node.fspath)
hook.pytest_fixture_post_finalizer(fixturedef=self, request=request)
- # even if finalization fails, we invalidate
- # the cached fixture value and remove
- # all finalizers because they may be bound methods which will
- # keep instances alive
+ # Even if finalization fails, we invalidate the cached fixture
+ # value and remove all finalizers because they may be bound methods
+ # which will keep instances alive.
self.cached_result = None
self._finalizers = []
def execute(self, request: SubRequest) -> _FixtureValue:
- # get required arguments and register our own finish()
- # with their finalization
+ # Get required arguments and register our own finish()
+ # with their finalization.
for argname in self.argnames:
fixturedef = request._get_active_fixturedef(argname)
if argname != "request":
@@ -1043,7 +1046,7 @@ class FixtureDef(Generic[_FixtureValue]):
my_cache_key = self.cache_key(request)
if self.cached_result is not None:
# note: comparison with `==` can fail (or be expensive) for e.g.
- # numpy arrays (#6497)
+ # numpy arrays (#6497).
cache_key = self.cached_result[1]
if my_cache_key is cache_key:
if self.cached_result[2] is not None:
@@ -1052,8 +1055,8 @@ class FixtureDef(Generic[_FixtureValue]):
else:
result = self.cached_result[0]
return result
- # we have a previous but differently parametrized fixture instance
- # so we need to tear it down before creating a new one
+ # We have a previous but differently parametrized fixture instance
+ # so we need to tear it down before creating a new one.
self.finish(request)
assert self.cached_result is None
@@ -1073,21 +1076,20 @@ class FixtureDef(Generic[_FixtureValue]):
def resolve_fixture_function(
fixturedef: FixtureDef[_FixtureValue], request: FixtureRequest
) -> "_FixtureFunc[_FixtureValue]":
- """Gets the actual callable that can be called to obtain the fixture value, dealing with unittest-specific
- instances and bound methods.
- """
+ """Get the actual callable that can be called to obtain the fixture
+ value, dealing with unittest-specific instances and bound methods."""
fixturefunc = fixturedef.func
if fixturedef.unittest:
if request.instance is not None:
- # bind the unbound method to the TestCase instance
+ # Bind the unbound method to the TestCase instance.
fixturefunc = fixturedef.func.__get__(request.instance) # type: ignore[union-attr]
else:
- # the fixture function needs to be bound to the actual
+ # The fixture function needs to be bound to the actual
# request.instance so that code working with "fixturedef" behaves
# as expected.
if request.instance is not None:
- # handle the case where fixture is defined not in a test class, but some other class
- # (for example a plugin class with a fixture), see #2270
+ # Handle the case where fixture is defined not in a test class, but some other class
+ # (for example a plugin class with a fixture), see #2270.
if hasattr(fixturefunc, "__self__") and not isinstance(
request.instance, fixturefunc.__self__.__class__ # type: ignore[union-attr]
):
@@ -1101,7 +1103,7 @@ def resolve_fixture_function(
def pytest_fixture_setup(
fixturedef: FixtureDef[_FixtureValue], request: SubRequest
) -> _FixtureValue:
- """ Execution of fixture setup. """
+ """Execution of fixture setup."""
kwargs = {}
for argname in fixturedef.argnames:
fixdef = request._get_active_fixturedef(argname)
@@ -1151,8 +1153,7 @@ def _params_converter(
def wrap_function_to_error_out_if_called_directly(function, fixture_marker):
"""Wrap the given fixture function so we can raise an error about it being called directly,
- instead of used as an argument in a test function.
- """
+ instead of used as an argument in a test function."""
message = (
'Fixture "{name}" called directly. Fixtures are not meant to be called directly,\n'
"but are created automatically when test functions request them as parameters.\n"
@@ -1164,8 +1165,8 @@ def wrap_function_to_error_out_if_called_directly(function, fixture_marker):
def result(*args, **kwargs):
fail(message, pytrace=False)
- # keep reference to the original function in our own custom attribute so we don't unwrap
- # further than this point and lose useful wrappings like @mock.patch (#3774)
+ # Keep reference to the original function in our own custom attribute so we don't unwrap
+ # further than this point and lose useful wrappings like @mock.patch (#3774).
result.__pytest_wrapped__ = _PytestWrapper(function) # type: ignore[attr-defined]
return result
@@ -1268,47 +1269,49 @@ def fixture( # noqa: F811
fixture function.
The name of the fixture function can later be referenced to cause its
- invocation ahead of running tests: test
- modules or classes can use the ``pytest.mark.usefixtures(fixturename)``
- marker.
-
- Test functions can directly use fixture names as input
- arguments in which case the fixture instance returned from the fixture
- function will be injected.
-
- Fixtures can provide their values to test functions using ``return`` or ``yield``
- statements. When using ``yield`` the code block after the ``yield`` statement is executed
- as teardown code regardless of the test outcome, and must yield exactly once.
-
- :arg scope: the scope for which this fixture is shared, one of
- ``"function"`` (default), ``"class"``, ``"module"``,
- ``"package"`` or ``"session"``.
-
- This parameter may also be a callable which receives ``(fixture_name, config)``
- as parameters, and must return a ``str`` with one of the values mentioned above.
-
- See :ref:`dynamic scope` in the docs for more information.
-
- :arg params: an optional list of parameters which will cause multiple
- invocations of the fixture function and all of the tests
- using it.
- The current parameter is available in ``request.param``.
-
- :arg autouse: if True, the fixture func is activated for all tests that
- can see it. If False (the default) then an explicit
- reference is needed to activate the fixture.
-
- :arg ids: list of string ids each corresponding to the params
- so that they are part of the test id. If no ids are provided
- they will be generated automatically from the params.
-
- :arg name: the name of the fixture. This defaults to the name of the
- decorated function. If a fixture is used in the same module in
- which it is defined, the function name of the fixture will be
- shadowed by the function arg that requests the fixture; one way
- to resolve this is to name the decorated function
- ``fixture_<fixturename>`` and then use
- ``@pytest.fixture(name='<fixturename>')``.
+ invocation ahead of running tests: test modules or classes can use the
+ ``pytest.mark.usefixtures(fixturename)`` marker.
+
+ Test functions can directly use fixture names as input arguments in which
+ case the fixture instance returned from the fixture function will be
+ injected.
+
+ Fixtures can provide their values to test functions using ``return`` or
+ ``yield`` statements. When using ``yield`` the code block after the
+ ``yield`` statement is executed as teardown code regardless of the test
+ outcome, and must yield exactly once.
+
+ :param scope:
+ The scope for which this fixture is shared; one of ``"function"``
+ (default), ``"class"``, ``"module"``, ``"package"`` or ``"session"``.
+
+ This parameter may also be a callable which receives ``(fixture_name, config)``
+ as parameters, and must return a ``str`` with one of the values mentioned above.
+
+ See :ref:`dynamic scope` in the docs for more information.
+
+ :param params:
+ An optional list of parameters which will cause multiple invocations
+ of the fixture function and all of the tests using it. The current
+ parameter is available in ``request.param``.
+
+ :param autouse:
+ If True, the fixture func is activated for all tests that can see it.
+ If False (the default), an explicit reference is needed to activate
+ the fixture.
+
+ :param ids:
+ List of string ids each corresponding to the params so that they are
+ part of the test id. If no ids are provided they will be generated
+ automatically from the params.
+
+ :param name:
+ The name of the fixture. This defaults to the name of the decorated
+ function. If a fixture is used in the same module in which it is
+ defined, the function name of the fixture will be shadowed by the
+ function arg that requests the fixture; one way to resolve this is to
+ name the decorated function ``fixture_<fixturename>`` and then use
+ ``@pytest.fixture(name='<fixturename>')``.
"""
# Positional arguments backward compatibility.
# If a kwarg is equal to its default, assume it was not explicitly
@@ -1377,7 +1380,7 @@ def yield_fixture(
ids=None,
name=None
):
- """ (return a) decorator to mark a yield-fixture factory function.
+ """(Return a) decorator to mark a yield-fixture factory function.
.. deprecated:: 3.0
Use :py:func:`pytest.fixture` directly instead.
@@ -1417,8 +1420,7 @@ def pytest_addoption(parser: Parser) -> None:
class FixtureManager:
- """
- pytest fixtures definitions and information is stored and managed
+ """pytest fixture definitions and information is stored and managed
from this class.
During collection fm.parsefactories() is called multiple times to parse
@@ -1431,7 +1433,7 @@ class FixtureManager:
which themselves offer a fixturenames attribute.
The FuncFixtureInfo object holds information about fixtures and FixtureDefs
- relevant for a particular function. An initial list of fixtures is
+ relevant for a particular function. An initial list of fixtures is
assembled like this:
- ini-defined usefixtures
@@ -1441,7 +1443,7 @@ class FixtureManager:
Subsequently the funcfixtureinfo.fixturenames attribute is computed
as the closure of the fixtures needed to setup the initial fixtures,
- i. e. fixtures needed by fixture functions themselves are appended
+ i.e. fixtures needed by fixture functions themselves are appended
to the fixturenames list.
Upon the test-setup phases all fixturenames are instantiated, retrieved
@@ -1462,13 +1464,13 @@ class FixtureManager:
session.config.pluginmanager.register(self, "funcmanage")
def _get_direct_parametrize_args(self, node: "nodes.Node") -> List[str]:
- """This function returns all the direct parametrization
- arguments of a node, so we don't mistake them for fixtures
+ """Return all direct parametrization arguments of a node, so we don't
+ mistake them for fixtures.
- Check https://github.com/pytest-dev/pytest/issues/5036
+ Check https://github.com/pytest-dev/pytest/issues/5036.
- This things are done later as well when dealing with parametrization
- so this could be improved
+ These things are done later as well when dealing with parametrization
+ so this could be improved.
"""
parametrize_argnames = [] # type: List[str]
for marker in node.iter_markers(name="parametrize"):
@@ -1507,9 +1509,9 @@ class FixtureManager:
else:
from _pytest import nodes
- # construct the base nodeid which is later used to check
+ # Construct the base nodeid which is later used to check
# what fixtures are visible for particular tests (as denoted
- # by their test id)
+ # by their test id).
if p.basename.startswith("conftest.py"):
nodeid = p.dirpath().relto(self.config.rootdir)
if p.sep != nodes.SEP:
@@ -1518,7 +1520,7 @@ class FixtureManager:
self.parsefactories(plugin, nodeid)
def _getautousenames(self, nodeid: str) -> List[str]:
- """ return a tuple of fixture names to be used. """
+ """Return a list of fixture names to be used."""
autousenames = [] # type: List[str]
for baseid, basenames in self._nodeid_and_autousenames:
if nodeid.startswith(baseid):
@@ -1533,12 +1535,12 @@ class FixtureManager:
def getfixtureclosure(
self, fixturenames: Tuple[str, ...], parentnode, ignore_args: Sequence[str] = ()
) -> Tuple[Tuple[str, ...], List[str], Dict[str, Sequence[FixtureDef]]]:
- # collect the closure of all fixtures , starting with the given
+ # Collect the closure of all fixtures, starting with the given
# fixturenames as the initial set. As we have to visit all
# factory definitions anyway, we also return an arg2fixturedefs
# mapping so that the caller can reuse it and does not have
# to re-discover fixturedefs again for each fixturename
- # (discovering matching fixtures for a given name/node is expensive)
+ # (discovering matching fixtures for a given name/node is expensive).
parentid = parentnode.nodeid
fixturenames_closure = self._getautousenames(parentid)
@@ -1550,7 +1552,7 @@ class FixtureManager:
merge(fixturenames)
- # at this point, fixturenames_closure contains what we call "initialnames",
+ # At this point, fixturenames_closure contains what we call "initialnames",
# which is a set of fixturenames the function immediately requests. We
# need to return it as well, so save this.
initialnames = tuple(fixturenames_closure)
@@ -1608,10 +1610,10 @@ class FixtureManager:
ids=fixturedef.ids,
)
else:
- continue # will raise FixtureLookupError at setup time
+ continue # Will raise FixtureLookupError at setup time.
def pytest_collection_modifyitems(self, items: "List[nodes.Item]") -> None:
- # separate parametrized setups
+ # Separate parametrized setups.
items[:] = reorder_items(items)
def parsefactories(
@@ -1633,16 +1635,17 @@ class FixtureManager:
obj = safe_getattr(holderobj, name, None)
marker = getfixturemarker(obj)
if not isinstance(marker, FixtureFunctionMarker):
- # magic globals with __getattr__ might have got us a wrong
- # fixture attribute
+ # Magic globals with __getattr__ might have got us a wrong
+ # fixture attribute.
continue
if marker.name:
name = marker.name
- # during fixture definition we wrap the original fixture function
- # to issue a warning if called directly, so here we unwrap it in order to not emit the warning
- # when pytest itself calls the fixture function
+ # During fixture definition we wrap the original fixture function
+ # to issue a warning if called directly, so here we unwrap it in
+ # order to not emit the warning when pytest itself calls the
+ # fixture function.
obj = get_real_method(obj, holderobj)
fixture_def = FixtureDef(
@@ -1675,12 +1678,11 @@ class FixtureManager:
def getfixturedefs(
self, argname: str, nodeid: str
) -> Optional[Sequence[FixtureDef]]:
- """
- Gets a list of fixtures which are applicable to the given node id.
+ """Get a list of fixtures which are applicable to the given node id.
- :param str argname: name of the fixture to search for
- :param str nodeid: full node id of the requesting test.
- :return: list[FixtureDef]
+ :param str argname: Name of the fixture to search for.
+ :param str nodeid: Full node id of the requesting test.
+ :rtype: Sequence[FixtureDef]
"""
try:
fixturedefs = self._arg2fixturedefs[argname]