MCPcopy Create free account
hub / github.com/pytest-dev/pytest / FixtureRequest

Class FixtureRequest

src/_pytest/fixtures.py:433–758  ·  view source on GitHub ↗

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.

Source from the content-addressed store, hash-verified

431
432
433class FixtureRequest:
434 """A request for a fixture from a test or fixture function.
435
436 A request object gives access to the requesting test context and has
437 an optional ``param`` attribute in case the fixture is parametrized
438 indirectly.
439 """
440
441 def __init__(self, pyfuncitem, *, _ispytest: bool = False) -> None:
442 check_ispytest(_ispytest)
443 self._pyfuncitem = pyfuncitem
444 #: Fixture for which this request is being performed.
445 self.fixturename: Optional[str] = None
446 self._scope = Scope.Function
447 self._fixture_defs: Dict[str, FixtureDef[Any]] = {}
448 fixtureinfo: FuncFixtureInfo = pyfuncitem._fixtureinfo
449 self._arg2fixturedefs = fixtureinfo.name2fixturedefs.copy()
450 self._arg2index: Dict[str, int] = {}
451 self._fixturemanager: FixtureManager = pyfuncitem.session._fixturemanager
452
453 @property
454 def scope(self) -> "_ScopeName":
455 """Scope string, one of "function", "class", "module", "package", "session"."""
456 return self._scope.value
457
458 @property
459 def fixturenames(self) -> List[str]:
460 """Names of all active fixtures in this request."""
461 result = list(self._pyfuncitem._fixtureinfo.names_closure)
462 result.extend(set(self._fixture_defs).difference(result))
463 return result
464
465 @property
466 def node(self):
467 """Underlying collection node (depends on current request scope)."""
468 return self._getscopeitem(self._scope)
469
470 def _getnextfixturedef(self, argname: str) -> "FixtureDef[Any]":
471 fixturedefs = self._arg2fixturedefs.get(argname, None)
472 if fixturedefs is None:
473 # We arrive here because of a dynamic call to
474 # getfixturevalue(argname) usage which was naturally
475 # not known at parsing/collection time.
476 assert self._pyfuncitem.parent is not None
477 parentid = self._pyfuncitem.parent.nodeid
478 fixturedefs = self._fixturemanager.getfixturedefs(argname, parentid)
479 # TODO: Fix this type ignore. Either add assert or adjust types.
480 # Can this be None here?
481 self._arg2fixturedefs[argname] = fixturedefs # type: ignore[assignment]
482 # fixturedefs list is immutable so we maintain a decreasing index.
483 index = self._arg2index.get(argname, 0) - 1
484 if fixturedefs is None or (-index > len(fixturedefs)):
485 raise FixtureLookupError(argname, self)
486 self._arg2index[argname] = index
487 return fixturedefs[index]
488
489 @property
490 def config(self) -> Config:

Calls

no outgoing calls