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
(self, argname: str)
| 602 | raise FixtureLookupError(argname, self, msg) |
| 603 | |
| 604 | def getfixturevalue(self, argname: str) -> Any: |
| 605 | """Dynamically run a named fixture function. |
| 606 | |
| 607 | Declaring fixtures via function argument is recommended where possible. |
| 608 | But if you can only decide whether to use another fixture at test |
| 609 | setup time, you may use this function to retrieve it inside a fixture |
| 610 | or test function body. |
| 611 | |
| 612 | This method can be used during the test setup phase or the test run |
| 613 | phase. Avoid using it during the teardown phase. |
| 614 | |
| 615 | .. versionchanged:: 9.1 |
| 616 | Calling ``request.getfixturevalue()`` during teardown to request a |
| 617 | fixture that was not already requested |
| 618 | :ref:`is deprecated <dynamic-fixture-request-during-teardown>`. |
| 619 | |
| 620 | :param argname: |
| 621 | The fixture name. |
| 622 | :raises pytest.FixtureLookupError: |
| 623 | If the given fixture could not be found. |
| 624 | """ |
| 625 | # Note that in addition to the use case described in the docstring, |
| 626 | # getfixturevalue() is also called by pytest itself during item and fixture |
| 627 | # setup to evaluate the fixtures that are requested statically |
| 628 | # (using function parameters, autouse, etc). |
| 629 | |
| 630 | fixturedef = self._get_active_fixturedef(argname) |
| 631 | assert fixturedef.cached_result is not None, ( |
| 632 | f'The fixture value for "{argname}" is not available. ' |
| 633 | "This can happen when the fixture has already been torn down." |
| 634 | ) |
| 635 | return fixturedef.cached_result[0] |
| 636 | |
| 637 | def _iter_chain(self) -> Iterator[SubRequest]: |
| 638 | """Yield all SubRequests in the chain, from self up. |