diff --git a/AUTHORS b/AUTHORS index 7c791cde8..6efc41d97 100644 --- a/AUTHORS +++ b/AUTHORS @@ -94,6 +94,7 @@ Elizaveta Shashkova Endre Galaczi Eric Hunsberger Eric Siegerman +Erik Aronesty Erik M. Bray Evan Kepner Fabien Zarifian diff --git a/changelog/6285.feature.rst b/changelog/6285.feature.rst new file mode 100644 index 000000000..bac353c86 --- /dev/null +++ b/changelog/6285.feature.rst @@ -0,0 +1,2 @@ +Exposed the `pytest.FixtureLookupError` exception which is raised by `request.getfixturevalue()` +(where `request` is a `FixtureRequest` fixture) when a fixture with the given name cannot be returned. diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 3111e28f5..f9e0f3b28 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -482,6 +482,9 @@ class FixtureRequest: 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: + If the given fixture could not be found. """ return self._get_active_fixturedef(argname).cached_result[0] diff --git a/src/pytest/__init__.py b/src/pytest/__init__.py index 5c93decc3..8629569b2 100644 --- a/src/pytest/__init__.py +++ b/src/pytest/__init__.py @@ -14,6 +14,7 @@ from _pytest.config import UsageError from _pytest.debugging import pytestPDB as __pytestPDB from _pytest.fixtures import fillfixtures as _fillfuncargs from _pytest.fixtures import fixture +from _pytest.fixtures import FixtureLookupError from _pytest.fixtures import yield_fixture from _pytest.freeze_support import freeze_includes from _pytest.main import Session @@ -62,6 +63,7 @@ __all__ = [ "fail", "File", "fixture", + "FixtureLookupError", "freeze_includes", "Function", "hookimpl", diff --git a/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/sub1/conftest.py b/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/sub1/conftest.py index 79af4bc47..be5adbeb6 100644 --- a/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/sub1/conftest.py +++ b/testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/sub1/conftest.py @@ -3,5 +3,5 @@ import pytest @pytest.fixture def arg1(request): - with pytest.raises(Exception): + with pytest.raises(pytest.FixtureLookupError): request.getfixturevalue("arg2") diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index bfbe35951..36e55a0e1 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -3,7 +3,6 @@ import textwrap import pytest from _pytest import fixtures -from _pytest.fixtures import FixtureLookupError from _pytest.fixtures import FixtureRequest from _pytest.pathlib import Path from _pytest.pytester import get_public_names @@ -654,7 +653,7 @@ class TestRequestBasic: ) req = item._request - with pytest.raises(FixtureLookupError): + with pytest.raises(pytest.FixtureLookupError): req.getfixturevalue("notexists") val = req.getfixturevalue("something") assert val == 1