|
73 | 73 | from _pytest.scope import _ScopeName
|
74 | 74 | from _pytest.scope import HIGH_SCOPES
|
75 | 75 | from _pytest.scope import Scope
|
| 76 | +from _pytest.warning_types import PytestRemovedIn9Warning |
76 | 77 |
|
77 | 78 |
|
78 | 79 | if sys.version_info < (3, 11):
|
@@ -575,6 +576,7 @@ def _get_active_fixturedef(
|
575 | 576 | # The are no fixtures with this name applicable for the function.
|
576 | 577 | if not fixturedefs:
|
577 | 578 | raise FixtureLookupError(argname, self)
|
| 579 | + |
578 | 580 | # A fixture may override another fixture with the same name, e.g. a
|
579 | 581 | # fixture in a module can override a fixture in a conftest, a fixture in
|
580 | 582 | # a class can override a fixture in the module, and so on.
|
@@ -968,6 +970,8 @@ def __init__(
|
968 | 970 | ids: tuple[object | None, ...] | Callable[[Any], object | None] | None = None,
|
969 | 971 | *,
|
970 | 972 | _ispytest: bool = False,
|
| 973 | + # only used in a deprecationwarning msg, can be removed in pytest9 |
| 974 | + _autouse: bool = False, |
971 | 975 | ) -> None:
|
972 | 976 | check_ispytest(_ispytest)
|
973 | 977 | # The "base" node ID for the fixture.
|
@@ -1014,6 +1018,9 @@ def __init__(
|
1014 | 1018 | self.cached_result: _FixtureCachedResult[FixtureValue] | None = None
|
1015 | 1019 | self._finalizers: Final[list[Callable[[], object]]] = []
|
1016 | 1020 |
|
| 1021 | + # only used to emit a deprecationwarning, can be removed in pytest9 |
| 1022 | + self._autouse = _autouse |
| 1023 | + |
1017 | 1024 | @property
|
1018 | 1025 | def scope(self) -> _ScopeName:
|
1019 | 1026 | """Scope string, one of "function", "class", "module", "package", "session"."""
|
@@ -1145,6 +1152,25 @@ def pytest_fixture_setup(
|
1145 | 1152 |
|
1146 | 1153 | fixturefunc = resolve_fixture_function(fixturedef, request)
|
1147 | 1154 | my_cache_key = fixturedef.cache_key(request)
|
| 1155 | + |
| 1156 | + if inspect.isasyncgenfunction(fixturefunc) or inspect.iscoroutinefunction( |
| 1157 | + fixturefunc |
| 1158 | + ): |
| 1159 | + auto_str = " with autouse=True" if fixturedef._autouse else "" |
| 1160 | + |
| 1161 | + warnings.warn( |
| 1162 | + PytestRemovedIn9Warning( |
| 1163 | + f"{request.node.name!r} requested an async fixture " |
| 1164 | + f"{request.fixturename!r}{auto_str}, with no plugin or hook that " |
| 1165 | + "handled it. This is usually an error, as pytest does not natively " |
| 1166 | + "support it. " |
| 1167 | + "This will turn into an error in pytest 9.\n" |
| 1168 | + "See: https://docs.pytest.org/en/stable/deprecations.html#sync-test-depending-on-async-fixture" |
| 1169 | + ), |
| 1170 | + # no stacklevel will point at users code, so we just point here |
| 1171 | + stacklevel=1, |
| 1172 | + ) |
| 1173 | + |
1148 | 1174 | try:
|
1149 | 1175 | result = call_fixture_func(fixturefunc, request, kwargs)
|
1150 | 1176 | except TEST_OUTCOME as e:
|
@@ -1675,6 +1701,7 @@ def _register_fixture(
|
1675 | 1701 | params=params,
|
1676 | 1702 | ids=ids,
|
1677 | 1703 | _ispytest=True,
|
| 1704 | + _autouse=autouse, |
1678 | 1705 | )
|
1679 | 1706 |
|
1680 | 1707 | faclist = self._arg2fixturedefs.setdefault(name, [])
|
|
0 commit comments