fixtures.py 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  1. import dataclasses
  2. import functools
  3. import inspect
  4. import os
  5. import sys
  6. import warnings
  7. from collections import defaultdict
  8. from collections import deque
  9. from contextlib import suppress
  10. from pathlib import Path
  11. from types import TracebackType
  12. from typing import Any
  13. from typing import Callable
  14. from typing import cast
  15. from typing import Dict
  16. from typing import Generator
  17. from typing import Generic
  18. from typing import Iterable
  19. from typing import Iterator
  20. from typing import List
  21. from typing import MutableMapping
  22. from typing import NoReturn
  23. from typing import Optional
  24. from typing import Sequence
  25. from typing import Set
  26. from typing import Tuple
  27. from typing import Type
  28. from typing import TYPE_CHECKING
  29. from typing import TypeVar
  30. from typing import Union
  31. import _pytest
  32. from _pytest import nodes
  33. from _pytest._code import getfslineno
  34. from _pytest._code.code import FormattedExcinfo
  35. from _pytest._code.code import TerminalRepr
  36. from _pytest._io import TerminalWriter
  37. from _pytest.compat import _format_args
  38. from _pytest.compat import _PytestWrapper
  39. from _pytest.compat import assert_never
  40. from _pytest.compat import final
  41. from _pytest.compat import get_real_func
  42. from _pytest.compat import get_real_method
  43. from _pytest.compat import getfuncargnames
  44. from _pytest.compat import getimfunc
  45. from _pytest.compat import getlocation
  46. from _pytest.compat import is_generator
  47. from _pytest.compat import NOTSET
  48. from _pytest.compat import NotSetType
  49. from _pytest.compat import overload
  50. from _pytest.compat import safe_getattr
  51. from _pytest.config import _PluggyPlugin
  52. from _pytest.config import Config
  53. from _pytest.config.argparsing import Parser
  54. from _pytest.deprecated import check_ispytest
  55. from _pytest.deprecated import YIELD_FIXTURE
  56. from _pytest.mark import Mark
  57. from _pytest.mark import ParameterSet
  58. from _pytest.mark.structures import MarkDecorator
  59. from _pytest.outcomes import fail
  60. from _pytest.outcomes import skip
  61. from _pytest.outcomes import TEST_OUTCOME
  62. from _pytest.pathlib import absolutepath
  63. from _pytest.pathlib import bestrelpath
  64. from _pytest.scope import HIGH_SCOPES
  65. from _pytest.scope import Scope
  66. from _pytest.stash import StashKey
  67. if TYPE_CHECKING:
  68. from typing import Deque
  69. from _pytest.scope import _ScopeName
  70. from _pytest.main import Session
  71. from _pytest.python import CallSpec2
  72. from _pytest.python import Metafunc
  73. # The value of the fixture -- return/yield of the fixture function (type variable).
  74. FixtureValue = TypeVar("FixtureValue")
  75. # The type of the fixture function (type variable).
  76. FixtureFunction = TypeVar("FixtureFunction", bound=Callable[..., object])
  77. # The type of a fixture function (type alias generic in fixture value).
  78. _FixtureFunc = Union[
  79. Callable[..., FixtureValue], Callable[..., Generator[FixtureValue, None, None]]
  80. ]
  81. # The type of FixtureDef.cached_result (type alias generic in fixture value).
  82. _FixtureCachedResult = Union[
  83. Tuple[
  84. # The result.
  85. FixtureValue,
  86. # Cache key.
  87. object,
  88. None,
  89. ],
  90. Tuple[
  91. None,
  92. # Cache key.
  93. object,
  94. # Exc info if raised.
  95. Tuple[Type[BaseException], BaseException, TracebackType],
  96. ],
  97. ]
  98. @dataclasses.dataclass(frozen=True)
  99. class PseudoFixtureDef(Generic[FixtureValue]):
  100. cached_result: "_FixtureCachedResult[FixtureValue]"
  101. _scope: Scope
  102. def pytest_sessionstart(session: "Session") -> None:
  103. session._fixturemanager = FixtureManager(session)
  104. def get_scope_package(
  105. node: nodes.Item,
  106. fixturedef: "FixtureDef[object]",
  107. ) -> Optional[Union[nodes.Item, nodes.Collector]]:
  108. from _pytest.python import Package
  109. current: Optional[Union[nodes.Item, nodes.Collector]] = node
  110. fixture_package_name = "{}/{}".format(fixturedef.baseid, "__init__.py")
  111. while current and (
  112. not isinstance(current, Package) or fixture_package_name != current.nodeid
  113. ):
  114. current = current.parent # type: ignore[assignment]
  115. if current is None:
  116. return node.session
  117. return current
  118. def get_scope_node(
  119. node: nodes.Node, scope: Scope
  120. ) -> Optional[Union[nodes.Item, nodes.Collector]]:
  121. import _pytest.python
  122. if scope is Scope.Function:
  123. return node.getparent(nodes.Item)
  124. elif scope is Scope.Class:
  125. return node.getparent(_pytest.python.Class)
  126. elif scope is Scope.Module:
  127. return node.getparent(_pytest.python.Module)
  128. elif scope is Scope.Package:
  129. return node.getparent(_pytest.python.Package)
  130. elif scope is Scope.Session:
  131. return node.getparent(_pytest.main.Session)
  132. else:
  133. assert_never(scope)
  134. # Used for storing artificial fixturedefs for direct parametrization.
  135. name2pseudofixturedef_key = StashKey[Dict[str, "FixtureDef[Any]"]]()
  136. def add_funcarg_pseudo_fixture_def(
  137. collector: nodes.Collector, metafunc: "Metafunc", fixturemanager: "FixtureManager"
  138. ) -> None:
  139. # This function will transform all collected calls to functions
  140. # if they use direct funcargs (i.e. direct parametrization)
  141. # because we want later test execution to be able to rely on
  142. # an existing FixtureDef structure for all arguments.
  143. # XXX we can probably avoid this algorithm if we modify CallSpec2
  144. # to directly care for creating the fixturedefs within its methods.
  145. if not metafunc._calls[0].funcargs:
  146. # This function call does not have direct parametrization.
  147. return
  148. # Collect funcargs of all callspecs into a list of values.
  149. arg2params: Dict[str, List[object]] = {}
  150. arg2scope: Dict[str, Scope] = {}
  151. for callspec in metafunc._calls:
  152. for argname, argvalue in callspec.funcargs.items():
  153. assert argname not in callspec.params
  154. callspec.params[argname] = argvalue
  155. arg2params_list = arg2params.setdefault(argname, [])
  156. callspec.indices[argname] = len(arg2params_list)
  157. arg2params_list.append(argvalue)
  158. if argname not in arg2scope:
  159. scope = callspec._arg2scope.get(argname, Scope.Function)
  160. arg2scope[argname] = scope
  161. callspec.funcargs.clear()
  162. # Register artificial FixtureDef's so that later at test execution
  163. # time we can rely on a proper FixtureDef to exist for fixture setup.
  164. arg2fixturedefs = metafunc._arg2fixturedefs
  165. for argname, valuelist in arg2params.items():
  166. # If we have a scope that is higher than function, we need
  167. # to make sure we only ever create an according fixturedef on
  168. # a per-scope basis. We thus store and cache the fixturedef on the
  169. # node related to the scope.
  170. scope = arg2scope[argname]
  171. node = None
  172. if scope is not Scope.Function:
  173. node = get_scope_node(collector, scope)
  174. if node is None:
  175. assert scope is Scope.Class and isinstance(
  176. collector, _pytest.python.Module
  177. )
  178. # Use module-level collector for class-scope (for now).
  179. node = collector
  180. if node is None:
  181. name2pseudofixturedef = None
  182. else:
  183. default: Dict[str, FixtureDef[Any]] = {}
  184. name2pseudofixturedef = node.stash.setdefault(
  185. name2pseudofixturedef_key, default
  186. )
  187. if name2pseudofixturedef is not None and argname in name2pseudofixturedef:
  188. arg2fixturedefs[argname] = [name2pseudofixturedef[argname]]
  189. else:
  190. fixturedef = FixtureDef(
  191. fixturemanager=fixturemanager,
  192. baseid="",
  193. argname=argname,
  194. func=get_direct_param_fixture_func,
  195. scope=arg2scope[argname],
  196. params=valuelist,
  197. unittest=False,
  198. ids=None,
  199. )
  200. arg2fixturedefs[argname] = [fixturedef]
  201. if name2pseudofixturedef is not None:
  202. name2pseudofixturedef[argname] = fixturedef
  203. def getfixturemarker(obj: object) -> Optional["FixtureFunctionMarker"]:
  204. """Return fixturemarker or None if it doesn't exist or raised
  205. exceptions."""
  206. return cast(
  207. Optional[FixtureFunctionMarker],
  208. safe_getattr(obj, "_pytestfixturefunction", None),
  209. )
  210. # Parametrized fixture key, helper alias for code below.
  211. _Key = Tuple[object, ...]
  212. def get_parametrized_fixture_keys(item: nodes.Item, scope: Scope) -> Iterator[_Key]:
  213. """Return list of keys for all parametrized arguments which match
  214. the specified scope."""
  215. assert scope is not Scope.Function
  216. try:
  217. callspec = item.callspec # type: ignore[attr-defined]
  218. except AttributeError:
  219. pass
  220. else:
  221. cs: CallSpec2 = callspec
  222. # cs.indices.items() is random order of argnames. Need to
  223. # sort this so that different calls to
  224. # get_parametrized_fixture_keys will be deterministic.
  225. for argname, param_index in sorted(cs.indices.items()):
  226. if cs._arg2scope[argname] != scope:
  227. continue
  228. if scope is Scope.Session:
  229. key: _Key = (argname, param_index)
  230. elif scope is Scope.Package:
  231. key = (argname, param_index, item.path.parent)
  232. elif scope is Scope.Module:
  233. key = (argname, param_index, item.path)
  234. elif scope is Scope.Class:
  235. item_cls = item.cls # type: ignore[attr-defined]
  236. key = (argname, param_index, item.path, item_cls)
  237. else:
  238. assert_never(scope)
  239. yield key
  240. # Algorithm for sorting on a per-parametrized resource setup basis.
  241. # It is called for Session scope first and performs sorting
  242. # down to the lower scopes such as to minimize number of "high scope"
  243. # setups and teardowns.
  244. def reorder_items(items: Sequence[nodes.Item]) -> List[nodes.Item]:
  245. argkeys_cache: Dict[Scope, Dict[nodes.Item, Dict[_Key, None]]] = {}
  246. items_by_argkey: Dict[Scope, Dict[_Key, Deque[nodes.Item]]] = {}
  247. for scope in HIGH_SCOPES:
  248. d: Dict[nodes.Item, Dict[_Key, None]] = {}
  249. argkeys_cache[scope] = d
  250. item_d: Dict[_Key, Deque[nodes.Item]] = defaultdict(deque)
  251. items_by_argkey[scope] = item_d
  252. for item in items:
  253. keys = dict.fromkeys(get_parametrized_fixture_keys(item, scope), None)
  254. if keys:
  255. d[item] = keys
  256. for key in keys:
  257. item_d[key].append(item)
  258. items_dict = dict.fromkeys(items, None)
  259. return list(
  260. reorder_items_atscope(items_dict, argkeys_cache, items_by_argkey, Scope.Session)
  261. )
  262. def fix_cache_order(
  263. item: nodes.Item,
  264. argkeys_cache: Dict[Scope, Dict[nodes.Item, Dict[_Key, None]]],
  265. items_by_argkey: Dict[Scope, Dict[_Key, "Deque[nodes.Item]"]],
  266. ) -> None:
  267. for scope in HIGH_SCOPES:
  268. for key in argkeys_cache[scope].get(item, []):
  269. items_by_argkey[scope][key].appendleft(item)
  270. def reorder_items_atscope(
  271. items: Dict[nodes.Item, None],
  272. argkeys_cache: Dict[Scope, Dict[nodes.Item, Dict[_Key, None]]],
  273. items_by_argkey: Dict[Scope, Dict[_Key, "Deque[nodes.Item]"]],
  274. scope: Scope,
  275. ) -> Dict[nodes.Item, None]:
  276. if scope is Scope.Function or len(items) < 3:
  277. return items
  278. ignore: Set[Optional[_Key]] = set()
  279. items_deque = deque(items)
  280. items_done: Dict[nodes.Item, None] = {}
  281. scoped_items_by_argkey = items_by_argkey[scope]
  282. scoped_argkeys_cache = argkeys_cache[scope]
  283. while items_deque:
  284. no_argkey_group: Dict[nodes.Item, None] = {}
  285. slicing_argkey = None
  286. while items_deque:
  287. item = items_deque.popleft()
  288. if item in items_done or item in no_argkey_group:
  289. continue
  290. argkeys = dict.fromkeys(
  291. (k for k in scoped_argkeys_cache.get(item, []) if k not in ignore), None
  292. )
  293. if not argkeys:
  294. no_argkey_group[item] = None
  295. else:
  296. slicing_argkey, _ = argkeys.popitem()
  297. # We don't have to remove relevant items from later in the
  298. # deque because they'll just be ignored.
  299. matching_items = [
  300. i for i in scoped_items_by_argkey[slicing_argkey] if i in items
  301. ]
  302. for i in reversed(matching_items):
  303. fix_cache_order(i, argkeys_cache, items_by_argkey)
  304. items_deque.appendleft(i)
  305. break
  306. if no_argkey_group:
  307. no_argkey_group = reorder_items_atscope(
  308. no_argkey_group, argkeys_cache, items_by_argkey, scope.next_lower()
  309. )
  310. for item in no_argkey_group:
  311. items_done[item] = None
  312. ignore.add(slicing_argkey)
  313. return items_done
  314. def get_direct_param_fixture_func(request: "FixtureRequest") -> Any:
  315. return request.param
  316. @dataclasses.dataclass
  317. class FuncFixtureInfo:
  318. __slots__ = ("argnames", "initialnames", "names_closure", "name2fixturedefs")
  319. # Original function argument names.
  320. argnames: Tuple[str, ...]
  321. # Argnames that function immediately requires. These include argnames +
  322. # fixture names specified via usefixtures and via autouse=True in fixture
  323. # definitions.
  324. initialnames: Tuple[str, ...]
  325. names_closure: List[str]
  326. name2fixturedefs: Dict[str, Sequence["FixtureDef[Any]"]]
  327. def prune_dependency_tree(self) -> None:
  328. """Recompute names_closure from initialnames and name2fixturedefs.
  329. Can only reduce names_closure, which means that the new closure will
  330. always be a subset of the old one. The order is preserved.
  331. This method is needed because direct parametrization may shadow some
  332. of the fixtures that were included in the originally built dependency
  333. tree. In this way the dependency tree can get pruned, and the closure
  334. of argnames may get reduced.
  335. """
  336. closure: Set[str] = set()
  337. working_set = set(self.initialnames)
  338. while working_set:
  339. argname = working_set.pop()
  340. # Argname may be smth not included in the original names_closure,
  341. # in which case we ignore it. This currently happens with pseudo
  342. # FixtureDefs which wrap 'get_direct_param_fixture_func(request)'.
  343. # So they introduce the new dependency 'request' which might have
  344. # been missing in the original tree (closure).
  345. if argname not in closure and argname in self.names_closure:
  346. closure.add(argname)
  347. if argname in self.name2fixturedefs:
  348. working_set.update(self.name2fixturedefs[argname][-1].argnames)
  349. self.names_closure[:] = sorted(closure, key=self.names_closure.index)
  350. class FixtureRequest:
  351. """A request for a fixture from a test or fixture function.
  352. A request object gives access to the requesting test context and has
  353. an optional ``param`` attribute in case the fixture is parametrized
  354. indirectly.
  355. """
  356. def __init__(self, pyfuncitem, *, _ispytest: bool = False) -> None:
  357. check_ispytest(_ispytest)
  358. self._pyfuncitem = pyfuncitem
  359. #: Fixture for which this request is being performed.
  360. self.fixturename: Optional[str] = None
  361. self._scope = Scope.Function
  362. self._fixture_defs: Dict[str, FixtureDef[Any]] = {}
  363. fixtureinfo: FuncFixtureInfo = pyfuncitem._fixtureinfo
  364. self._arg2fixturedefs = fixtureinfo.name2fixturedefs.copy()
  365. self._arg2index: Dict[str, int] = {}
  366. self._fixturemanager: FixtureManager = pyfuncitem.session._fixturemanager
  367. # Notes on the type of `param`:
  368. # -`request.param` is only defined in parametrized fixtures, and will raise
  369. # AttributeError otherwise. Python typing has no notion of "undefined", so
  370. # this cannot be reflected in the type.
  371. # - Technically `param` is only (possibly) defined on SubRequest, not
  372. # FixtureRequest, but the typing of that is still in flux so this cheats.
  373. # - In the future we might consider using a generic for the param type, but
  374. # for now just using Any.
  375. self.param: Any
  376. @property
  377. def scope(self) -> "_ScopeName":
  378. """Scope string, one of "function", "class", "module", "package", "session"."""
  379. return self._scope.value
  380. @property
  381. def fixturenames(self) -> List[str]:
  382. """Names of all active fixtures in this request."""
  383. result = list(self._pyfuncitem._fixtureinfo.names_closure)
  384. result.extend(set(self._fixture_defs).difference(result))
  385. return result
  386. @property
  387. def node(self):
  388. """Underlying collection node (depends on current request scope)."""
  389. scope = self._scope
  390. if scope is Scope.Function:
  391. # This might also be a non-function Item despite its attribute name.
  392. node: Optional[Union[nodes.Item, nodes.Collector]] = self._pyfuncitem
  393. elif scope is Scope.Package:
  394. # FIXME: _fixturedef is not defined on FixtureRequest (this class),
  395. # but on FixtureRequest (a subclass).
  396. node = get_scope_package(self._pyfuncitem, self._fixturedef) # type: ignore[attr-defined]
  397. else:
  398. node = get_scope_node(self._pyfuncitem, scope)
  399. if node is None and scope is Scope.Class:
  400. # Fallback to function item itself.
  401. node = self._pyfuncitem
  402. assert node, 'Could not obtain a node for scope "{}" for function {!r}'.format(
  403. scope, self._pyfuncitem
  404. )
  405. return node
  406. def _getnextfixturedef(self, argname: str) -> "FixtureDef[Any]":
  407. fixturedefs = self._arg2fixturedefs.get(argname, None)
  408. if fixturedefs is None:
  409. # We arrive here because of a dynamic call to
  410. # getfixturevalue(argname) usage which was naturally
  411. # not known at parsing/collection time.
  412. assert self._pyfuncitem.parent is not None
  413. parentid = self._pyfuncitem.parent.nodeid
  414. fixturedefs = self._fixturemanager.getfixturedefs(argname, parentid)
  415. # TODO: Fix this type ignore. Either add assert or adjust types.
  416. # Can this be None here?
  417. self._arg2fixturedefs[argname] = fixturedefs # type: ignore[assignment]
  418. # fixturedefs list is immutable so we maintain a decreasing index.
  419. index = self._arg2index.get(argname, 0) - 1
  420. if fixturedefs is None or (-index > len(fixturedefs)):
  421. raise FixtureLookupError(argname, self)
  422. self._arg2index[argname] = index
  423. return fixturedefs[index]
  424. @property
  425. def config(self) -> Config:
  426. """The pytest config object associated with this request."""
  427. return self._pyfuncitem.config # type: ignore[no-any-return]
  428. @property
  429. def function(self):
  430. """Test function object if the request has a per-function scope."""
  431. if self.scope != "function":
  432. raise AttributeError(
  433. f"function not available in {self.scope}-scoped context"
  434. )
  435. return self._pyfuncitem.obj
  436. @property
  437. def cls(self):
  438. """Class (can be None) where the test function was collected."""
  439. if self.scope not in ("class", "function"):
  440. raise AttributeError(f"cls not available in {self.scope}-scoped context")
  441. clscol = self._pyfuncitem.getparent(_pytest.python.Class)
  442. if clscol:
  443. return clscol.obj
  444. @property
  445. def instance(self):
  446. """Instance (can be None) on which test function was collected."""
  447. # unittest support hack, see _pytest.unittest.TestCaseFunction.
  448. try:
  449. return self._pyfuncitem._testcase
  450. except AttributeError:
  451. function = getattr(self, "function", None)
  452. return getattr(function, "__self__", None)
  453. @property
  454. def module(self):
  455. """Python module object where the test function was collected."""
  456. if self.scope not in ("function", "class", "module"):
  457. raise AttributeError(f"module not available in {self.scope}-scoped context")
  458. return self._pyfuncitem.getparent(_pytest.python.Module).obj
  459. @property
  460. def path(self) -> Path:
  461. """Path where the test function was collected."""
  462. if self.scope not in ("function", "class", "module", "package"):
  463. raise AttributeError(f"path not available in {self.scope}-scoped context")
  464. # TODO: Remove ignore once _pyfuncitem is properly typed.
  465. return self._pyfuncitem.path # type: ignore
  466. @property
  467. def keywords(self) -> MutableMapping[str, Any]:
  468. """Keywords/markers dictionary for the underlying node."""
  469. node: nodes.Node = self.node
  470. return node.keywords
  471. @property
  472. def session(self) -> "Session":
  473. """Pytest session object."""
  474. return self._pyfuncitem.session # type: ignore[no-any-return]
  475. def addfinalizer(self, finalizer: Callable[[], object]) -> None:
  476. """Add finalizer/teardown function to be called without arguments after
  477. the last test within the requesting test context finished execution."""
  478. # XXX usually this method is shadowed by fixturedef specific ones.
  479. self.node.addfinalizer(finalizer)
  480. def applymarker(self, marker: Union[str, MarkDecorator]) -> None:
  481. """Apply a marker to a single test function invocation.
  482. This method is useful if you don't want to have a keyword/marker
  483. on all function invocations.
  484. :param marker:
  485. An object created by a call to ``pytest.mark.NAME(...)``.
  486. """
  487. self.node.add_marker(marker)
  488. def raiseerror(self, msg: Optional[str]) -> NoReturn:
  489. """Raise a FixtureLookupError exception.
  490. :param msg:
  491. An optional custom error message.
  492. """
  493. raise self._fixturemanager.FixtureLookupError(None, self, msg)
  494. def _fillfixtures(self) -> None:
  495. item = self._pyfuncitem
  496. fixturenames = getattr(item, "fixturenames", self.fixturenames)
  497. for argname in fixturenames:
  498. if argname not in item.funcargs:
  499. item.funcargs[argname] = self.getfixturevalue(argname)
  500. def getfixturevalue(self, argname: str) -> Any:
  501. """Dynamically run a named fixture function.
  502. Declaring fixtures via function argument is recommended where possible.
  503. But if you can only decide whether to use another fixture at test
  504. setup time, you may use this function to retrieve it inside a fixture
  505. or test function body.
  506. This method can be used during the test setup phase or the test run
  507. phase, but during the test teardown phase a fixture's value may not
  508. be available.
  509. :param argname:
  510. The fixture name.
  511. :raises pytest.FixtureLookupError:
  512. If the given fixture could not be found.
  513. """
  514. fixturedef = self._get_active_fixturedef(argname)
  515. assert fixturedef.cached_result is not None, (
  516. f'The fixture value for "{argname}" is not available. '
  517. "This can happen when the fixture has already been torn down."
  518. )
  519. return fixturedef.cached_result[0]
  520. def _get_active_fixturedef(
  521. self, argname: str
  522. ) -> Union["FixtureDef[object]", PseudoFixtureDef[object]]:
  523. try:
  524. return self._fixture_defs[argname]
  525. except KeyError:
  526. try:
  527. fixturedef = self._getnextfixturedef(argname)
  528. except FixtureLookupError:
  529. if argname == "request":
  530. cached_result = (self, [0], None)
  531. return PseudoFixtureDef(cached_result, Scope.Function)
  532. raise
  533. # Remove indent to prevent the python3 exception
  534. # from leaking into the call.
  535. self._compute_fixture_value(fixturedef)
  536. self._fixture_defs[argname] = fixturedef
  537. return fixturedef
  538. def _get_fixturestack(self) -> List["FixtureDef[Any]"]:
  539. current = self
  540. values: List[FixtureDef[Any]] = []
  541. while isinstance(current, SubRequest):
  542. values.append(current._fixturedef) # type: ignore[has-type]
  543. current = current._parent_request
  544. values.reverse()
  545. return values
  546. def _compute_fixture_value(self, fixturedef: "FixtureDef[object]") -> None:
  547. """Create a SubRequest based on "self" and call the execute method
  548. of the given FixtureDef object.
  549. This will force the FixtureDef object to throw away any previous
  550. results and compute a new fixture value, which will be stored into
  551. the FixtureDef object itself.
  552. """
  553. # prepare a subrequest object before calling fixture function
  554. # (latter managed by fixturedef)
  555. argname = fixturedef.argname
  556. funcitem = self._pyfuncitem
  557. scope = fixturedef._scope
  558. try:
  559. callspec = funcitem.callspec
  560. except AttributeError:
  561. callspec = None
  562. if callspec is not None and argname in callspec.params:
  563. param = callspec.params[argname]
  564. param_index = callspec.indices[argname]
  565. # If a parametrize invocation set a scope it will override
  566. # the static scope defined with the fixture function.
  567. with suppress(KeyError):
  568. scope = callspec._arg2scope[argname]
  569. else:
  570. param = NOTSET
  571. param_index = 0
  572. has_params = fixturedef.params is not None
  573. fixtures_not_supported = getattr(funcitem, "nofuncargs", False)
  574. if has_params and fixtures_not_supported:
  575. msg = (
  576. "{name} does not support fixtures, maybe unittest.TestCase subclass?\n"
  577. "Node id: {nodeid}\n"
  578. "Function type: {typename}"
  579. ).format(
  580. name=funcitem.name,
  581. nodeid=funcitem.nodeid,
  582. typename=type(funcitem).__name__,
  583. )
  584. fail(msg, pytrace=False)
  585. if has_params:
  586. frame = inspect.stack()[3]
  587. frameinfo = inspect.getframeinfo(frame[0])
  588. source_path = absolutepath(frameinfo.filename)
  589. source_lineno = frameinfo.lineno
  590. try:
  591. source_path_str = str(
  592. source_path.relative_to(funcitem.config.rootpath)
  593. )
  594. except ValueError:
  595. source_path_str = str(source_path)
  596. msg = (
  597. "The requested fixture has no parameter defined for test:\n"
  598. " {}\n\n"
  599. "Requested fixture '{}' defined in:\n{}"
  600. "\n\nRequested here:\n{}:{}".format(
  601. funcitem.nodeid,
  602. fixturedef.argname,
  603. getlocation(fixturedef.func, funcitem.config.rootpath),
  604. source_path_str,
  605. source_lineno,
  606. )
  607. )
  608. fail(msg, pytrace=False)
  609. subrequest = SubRequest(
  610. self, scope, param, param_index, fixturedef, _ispytest=True
  611. )
  612. # Check if a higher-level scoped fixture accesses a lower level one.
  613. subrequest._check_scope(argname, self._scope, scope)
  614. try:
  615. # Call the fixture function.
  616. fixturedef.execute(request=subrequest)
  617. finally:
  618. self._schedule_finalizers(fixturedef, subrequest)
  619. def _schedule_finalizers(
  620. self, fixturedef: "FixtureDef[object]", subrequest: "SubRequest"
  621. ) -> None:
  622. # If fixture function failed it might have registered finalizers.
  623. subrequest.node.addfinalizer(lambda: fixturedef.finish(request=subrequest))
  624. def _check_scope(
  625. self,
  626. argname: str,
  627. invoking_scope: Scope,
  628. requested_scope: Scope,
  629. ) -> None:
  630. if argname == "request":
  631. return
  632. if invoking_scope > requested_scope:
  633. # Try to report something helpful.
  634. text = "\n".join(self._factorytraceback())
  635. fail(
  636. f"ScopeMismatch: You tried to access the {requested_scope.value} scoped "
  637. f"fixture {argname} with a {invoking_scope.value} scoped request object, "
  638. f"involved factories:\n{text}",
  639. pytrace=False,
  640. )
  641. def _factorytraceback(self) -> List[str]:
  642. lines = []
  643. for fixturedef in self._get_fixturestack():
  644. factory = fixturedef.func
  645. fs, lineno = getfslineno(factory)
  646. if isinstance(fs, Path):
  647. session: Session = self._pyfuncitem.session
  648. p = bestrelpath(session.path, fs)
  649. else:
  650. p = fs
  651. args = _format_args(factory)
  652. lines.append("%s:%d: def %s%s" % (p, lineno + 1, factory.__name__, args))
  653. return lines
  654. def __repr__(self) -> str:
  655. return "<FixtureRequest for %r>" % (self.node)
  656. @final
  657. class SubRequest(FixtureRequest):
  658. """A sub request for handling getting a fixture from a test function/fixture."""
  659. def __init__(
  660. self,
  661. request: "FixtureRequest",
  662. scope: Scope,
  663. param: Any,
  664. param_index: int,
  665. fixturedef: "FixtureDef[object]",
  666. *,
  667. _ispytest: bool = False,
  668. ) -> None:
  669. check_ispytest(_ispytest)
  670. self._parent_request = request
  671. self.fixturename = fixturedef.argname
  672. if param is not NOTSET:
  673. self.param = param
  674. self.param_index = param_index
  675. self._scope = scope
  676. self._fixturedef = fixturedef
  677. self._pyfuncitem = request._pyfuncitem
  678. self._fixture_defs = request._fixture_defs
  679. self._arg2fixturedefs = request._arg2fixturedefs
  680. self._arg2index = request._arg2index
  681. self._fixturemanager = request._fixturemanager
  682. def __repr__(self) -> str:
  683. return f"<SubRequest {self.fixturename!r} for {self._pyfuncitem!r}>"
  684. def addfinalizer(self, finalizer: Callable[[], object]) -> None:
  685. """Add finalizer/teardown function to be called without arguments after
  686. the last test within the requesting test context finished execution."""
  687. self._fixturedef.addfinalizer(finalizer)
  688. def _schedule_finalizers(
  689. self, fixturedef: "FixtureDef[object]", subrequest: "SubRequest"
  690. ) -> None:
  691. # If the executing fixturedef was not explicitly requested in the argument list (via
  692. # getfixturevalue inside the fixture call) then ensure this fixture def will be finished
  693. # first.
  694. if fixturedef.argname not in self.fixturenames:
  695. fixturedef.addfinalizer(
  696. functools.partial(self._fixturedef.finish, request=self)
  697. )
  698. super()._schedule_finalizers(fixturedef, subrequest)
  699. @final
  700. class FixtureLookupError(LookupError):
  701. """Could not return a requested fixture (missing or invalid)."""
  702. def __init__(
  703. self, argname: Optional[str], request: FixtureRequest, msg: Optional[str] = None
  704. ) -> None:
  705. self.argname = argname
  706. self.request = request
  707. self.fixturestack = request._get_fixturestack()
  708. self.msg = msg
  709. def formatrepr(self) -> "FixtureLookupErrorRepr":
  710. tblines: List[str] = []
  711. addline = tblines.append
  712. stack = [self.request._pyfuncitem.obj]
  713. stack.extend(map(lambda x: x.func, self.fixturestack))
  714. msg = self.msg
  715. if msg is not None:
  716. # The last fixture raise an error, let's present
  717. # it at the requesting side.
  718. stack = stack[:-1]
  719. for function in stack:
  720. fspath, lineno = getfslineno(function)
  721. try:
  722. lines, _ = inspect.getsourcelines(get_real_func(function))
  723. except (OSError, IndexError, TypeError):
  724. error_msg = "file %s, line %s: source code not available"
  725. addline(error_msg % (fspath, lineno + 1))
  726. else:
  727. addline(f"file {fspath}, line {lineno + 1}")
  728. for i, line in enumerate(lines):
  729. line = line.rstrip()
  730. addline(" " + line)
  731. if line.lstrip().startswith("def"):
  732. break
  733. if msg is None:
  734. fm = self.request._fixturemanager
  735. available = set()
  736. parentid = self.request._pyfuncitem.parent.nodeid
  737. for name, fixturedefs in fm._arg2fixturedefs.items():
  738. faclist = list(fm._matchfactories(fixturedefs, parentid))
  739. if faclist:
  740. available.add(name)
  741. if self.argname in available:
  742. msg = " recursive dependency involving fixture '{}' detected".format(
  743. self.argname
  744. )
  745. else:
  746. msg = f"fixture '{self.argname}' not found"
  747. msg += "\n available fixtures: {}".format(", ".join(sorted(available)))
  748. msg += "\n use 'pytest --fixtures [testpath]' for help on them."
  749. return FixtureLookupErrorRepr(fspath, lineno, tblines, msg, self.argname)
  750. class FixtureLookupErrorRepr(TerminalRepr):
  751. def __init__(
  752. self,
  753. filename: Union[str, "os.PathLike[str]"],
  754. firstlineno: int,
  755. tblines: Sequence[str],
  756. errorstring: str,
  757. argname: Optional[str],
  758. ) -> None:
  759. self.tblines = tblines
  760. self.errorstring = errorstring
  761. self.filename = filename
  762. self.firstlineno = firstlineno
  763. self.argname = argname
  764. def toterminal(self, tw: TerminalWriter) -> None:
  765. # tw.line("FixtureLookupError: %s" %(self.argname), red=True)
  766. for tbline in self.tblines:
  767. tw.line(tbline.rstrip())
  768. lines = self.errorstring.split("\n")
  769. if lines:
  770. tw.line(
  771. f"{FormattedExcinfo.fail_marker} {lines[0].strip()}",
  772. red=True,
  773. )
  774. for line in lines[1:]:
  775. tw.line(
  776. f"{FormattedExcinfo.flow_marker} {line.strip()}",
  777. red=True,
  778. )
  779. tw.line()
  780. tw.line("%s:%d" % (os.fspath(self.filename), self.firstlineno + 1))
  781. def fail_fixturefunc(fixturefunc, msg: str) -> NoReturn:
  782. fs, lineno = getfslineno(fixturefunc)
  783. location = f"{fs}:{lineno + 1}"
  784. source = _pytest._code.Source(fixturefunc)
  785. fail(msg + ":\n\n" + str(source.indent()) + "\n" + location, pytrace=False)
  786. def call_fixture_func(
  787. fixturefunc: "_FixtureFunc[FixtureValue]", request: FixtureRequest, kwargs
  788. ) -> FixtureValue:
  789. if is_generator(fixturefunc):
  790. fixturefunc = cast(
  791. Callable[..., Generator[FixtureValue, None, None]], fixturefunc
  792. )
  793. generator = fixturefunc(**kwargs)
  794. try:
  795. fixture_result = next(generator)
  796. except StopIteration:
  797. raise ValueError(f"{request.fixturename} did not yield a value") from None
  798. finalizer = functools.partial(_teardown_yield_fixture, fixturefunc, generator)
  799. request.addfinalizer(finalizer)
  800. else:
  801. fixturefunc = cast(Callable[..., FixtureValue], fixturefunc)
  802. fixture_result = fixturefunc(**kwargs)
  803. return fixture_result
  804. def _teardown_yield_fixture(fixturefunc, it) -> None:
  805. """Execute the teardown of a fixture function by advancing the iterator
  806. after the yield and ensure the iteration ends (if not it means there is
  807. more than one yield in the function)."""
  808. try:
  809. next(it)
  810. except StopIteration:
  811. pass
  812. else:
  813. fail_fixturefunc(fixturefunc, "fixture function has more than one 'yield'")
  814. def _eval_scope_callable(
  815. scope_callable: "Callable[[str, Config], _ScopeName]",
  816. fixture_name: str,
  817. config: Config,
  818. ) -> "_ScopeName":
  819. try:
  820. # Type ignored because there is no typing mechanism to specify
  821. # keyword arguments, currently.
  822. result = scope_callable(fixture_name=fixture_name, config=config) # type: ignore[call-arg]
  823. except Exception as e:
  824. raise TypeError(
  825. "Error evaluating {} while defining fixture '{}'.\n"
  826. "Expected a function with the signature (*, fixture_name, config)".format(
  827. scope_callable, fixture_name
  828. )
  829. ) from e
  830. if not isinstance(result, str):
  831. fail(
  832. "Expected {} to return a 'str' while defining fixture '{}', but it returned:\n"
  833. "{!r}".format(scope_callable, fixture_name, result),
  834. pytrace=False,
  835. )
  836. return result
  837. @final
  838. class FixtureDef(Generic[FixtureValue]):
  839. """A container for a fixture definition."""
  840. def __init__(
  841. self,
  842. fixturemanager: "FixtureManager",
  843. baseid: Optional[str],
  844. argname: str,
  845. func: "_FixtureFunc[FixtureValue]",
  846. scope: Union[Scope, "_ScopeName", Callable[[str, Config], "_ScopeName"], None],
  847. params: Optional[Sequence[object]],
  848. unittest: bool = False,
  849. ids: Optional[
  850. Union[Tuple[Optional[object], ...], Callable[[Any], Optional[object]]]
  851. ] = None,
  852. ) -> None:
  853. self._fixturemanager = fixturemanager
  854. # The "base" node ID for the fixture.
  855. #
  856. # This is a node ID prefix. A fixture is only available to a node (e.g.
  857. # a `Function` item) if the fixture's baseid is a parent of the node's
  858. # nodeid (see the `iterparentnodeids` function for what constitutes a
  859. # "parent" and a "prefix" in this context).
  860. #
  861. # For a fixture found in a Collector's object (e.g. a `Module`s module,
  862. # a `Class`'s class), the baseid is the Collector's nodeid.
  863. #
  864. # For a fixture found in a conftest plugin, the baseid is the conftest's
  865. # directory path relative to the rootdir.
  866. #
  867. # For other plugins, the baseid is the empty string (always matches).
  868. self.baseid = baseid or ""
  869. # Whether the fixture was found from a node or a conftest in the
  870. # collection tree. Will be false for fixtures defined in non-conftest
  871. # plugins.
  872. self.has_location = baseid is not None
  873. # The fixture factory function.
  874. self.func = func
  875. # The name by which the fixture may be requested.
  876. self.argname = argname
  877. if scope is None:
  878. scope = Scope.Function
  879. elif callable(scope):
  880. scope = _eval_scope_callable(scope, argname, fixturemanager.config)
  881. if isinstance(scope, str):
  882. scope = Scope.from_user(
  883. scope, descr=f"Fixture '{func.__name__}'", where=baseid
  884. )
  885. self._scope = scope
  886. # If the fixture is directly parametrized, the parameter values.
  887. self.params: Optional[Sequence[object]] = params
  888. # If the fixture is directly parametrized, a tuple of explicit IDs to
  889. # assign to the parameter values, or a callable to generate an ID given
  890. # a parameter value.
  891. self.ids = ids
  892. # The names requested by the fixtures.
  893. self.argnames = getfuncargnames(func, name=argname, is_method=unittest)
  894. # Whether the fixture was collected from a unittest TestCase class.
  895. # Note that it really only makes sense to define autouse fixtures in
  896. # unittest TestCases.
  897. self.unittest = unittest
  898. # If the fixture was executed, the current value of the fixture.
  899. # Can change if the fixture is executed with different parameters.
  900. self.cached_result: Optional[_FixtureCachedResult[FixtureValue]] = None
  901. self._finalizers: List[Callable[[], object]] = []
  902. @property
  903. def scope(self) -> "_ScopeName":
  904. """Scope string, one of "function", "class", "module", "package", "session"."""
  905. return self._scope.value
  906. def addfinalizer(self, finalizer: Callable[[], object]) -> None:
  907. self._finalizers.append(finalizer)
  908. def finish(self, request: SubRequest) -> None:
  909. exc = None
  910. try:
  911. while self._finalizers:
  912. try:
  913. func = self._finalizers.pop()
  914. func()
  915. except BaseException as e:
  916. # XXX Only first exception will be seen by user,
  917. # ideally all should be reported.
  918. if exc is None:
  919. exc = e
  920. if exc:
  921. raise exc
  922. finally:
  923. ihook = request.node.ihook
  924. ihook.pytest_fixture_post_finalizer(fixturedef=self, request=request)
  925. # Even if finalization fails, we invalidate the cached fixture
  926. # value and remove all finalizers because they may be bound methods
  927. # which will keep instances alive.
  928. self.cached_result = None
  929. self._finalizers = []
  930. def execute(self, request: SubRequest) -> FixtureValue:
  931. # Get required arguments and register our own finish()
  932. # with their finalization.
  933. for argname in self.argnames:
  934. fixturedef = request._get_active_fixturedef(argname)
  935. if argname != "request":
  936. # PseudoFixtureDef is only for "request".
  937. assert isinstance(fixturedef, FixtureDef)
  938. fixturedef.addfinalizer(functools.partial(self.finish, request=request))
  939. my_cache_key = self.cache_key(request)
  940. if self.cached_result is not None:
  941. # note: comparison with `==` can fail (or be expensive) for e.g.
  942. # numpy arrays (#6497).
  943. cache_key = self.cached_result[1]
  944. if my_cache_key is cache_key:
  945. if self.cached_result[2] is not None:
  946. _, val, tb = self.cached_result[2]
  947. raise val.with_traceback(tb)
  948. else:
  949. result = self.cached_result[0]
  950. return result
  951. # We have a previous but differently parametrized fixture instance
  952. # so we need to tear it down before creating a new one.
  953. self.finish(request)
  954. assert self.cached_result is None
  955. ihook = request.node.ihook
  956. result = ihook.pytest_fixture_setup(fixturedef=self, request=request)
  957. return result
  958. def cache_key(self, request: SubRequest) -> object:
  959. return request.param_index if not hasattr(request, "param") else request.param
  960. def __repr__(self) -> str:
  961. return "<FixtureDef argname={!r} scope={!r} baseid={!r}>".format(
  962. self.argname, self.scope, self.baseid
  963. )
  964. def resolve_fixture_function(
  965. fixturedef: FixtureDef[FixtureValue], request: FixtureRequest
  966. ) -> "_FixtureFunc[FixtureValue]":
  967. """Get the actual callable that can be called to obtain the fixture
  968. value, dealing with unittest-specific instances and bound methods."""
  969. fixturefunc = fixturedef.func
  970. if fixturedef.unittest:
  971. if request.instance is not None:
  972. # Bind the unbound method to the TestCase instance.
  973. fixturefunc = fixturedef.func.__get__(request.instance) # type: ignore[union-attr]
  974. else:
  975. # The fixture function needs to be bound to the actual
  976. # request.instance so that code working with "fixturedef" behaves
  977. # as expected.
  978. if request.instance is not None:
  979. # Handle the case where fixture is defined not in a test class, but some other class
  980. # (for example a plugin class with a fixture), see #2270.
  981. if hasattr(fixturefunc, "__self__") and not isinstance(
  982. request.instance, fixturefunc.__self__.__class__ # type: ignore[union-attr]
  983. ):
  984. return fixturefunc
  985. fixturefunc = getimfunc(fixturedef.func)
  986. if fixturefunc != fixturedef.func:
  987. fixturefunc = fixturefunc.__get__(request.instance) # type: ignore[union-attr]
  988. return fixturefunc
  989. def pytest_fixture_setup(
  990. fixturedef: FixtureDef[FixtureValue], request: SubRequest
  991. ) -> FixtureValue:
  992. """Execution of fixture setup."""
  993. kwargs = {}
  994. for argname in fixturedef.argnames:
  995. fixdef = request._get_active_fixturedef(argname)
  996. assert fixdef.cached_result is not None
  997. result, arg_cache_key, exc = fixdef.cached_result
  998. request._check_scope(argname, request._scope, fixdef._scope)
  999. kwargs[argname] = result
  1000. fixturefunc = resolve_fixture_function(fixturedef, request)
  1001. my_cache_key = fixturedef.cache_key(request)
  1002. try:
  1003. result = call_fixture_func(fixturefunc, request, kwargs)
  1004. except TEST_OUTCOME:
  1005. exc_info = sys.exc_info()
  1006. assert exc_info[0] is not None
  1007. if isinstance(
  1008. exc_info[1], skip.Exception
  1009. ) and not fixturefunc.__name__.startswith("xunit_setup"):
  1010. exc_info[1]._use_item_location = True # type: ignore[attr-defined]
  1011. fixturedef.cached_result = (None, my_cache_key, exc_info)
  1012. raise
  1013. fixturedef.cached_result = (result, my_cache_key, None)
  1014. return result
  1015. def _ensure_immutable_ids(
  1016. ids: Optional[Union[Sequence[Optional[object]], Callable[[Any], Optional[object]]]]
  1017. ) -> Optional[Union[Tuple[Optional[object], ...], Callable[[Any], Optional[object]]]]:
  1018. if ids is None:
  1019. return None
  1020. if callable(ids):
  1021. return ids
  1022. return tuple(ids)
  1023. def _params_converter(
  1024. params: Optional[Iterable[object]],
  1025. ) -> Optional[Tuple[object, ...]]:
  1026. return tuple(params) if params is not None else None
  1027. def wrap_function_to_error_out_if_called_directly(
  1028. function: FixtureFunction,
  1029. fixture_marker: "FixtureFunctionMarker",
  1030. ) -> FixtureFunction:
  1031. """Wrap the given fixture function so we can raise an error about it being called directly,
  1032. instead of used as an argument in a test function."""
  1033. message = (
  1034. 'Fixture "{name}" called directly. Fixtures are not meant to be called directly,\n'
  1035. "but are created automatically when test functions request them as parameters.\n"
  1036. "See https://docs.pytest.org/en/stable/explanation/fixtures.html for more information about fixtures, and\n"
  1037. "https://docs.pytest.org/en/stable/deprecations.html#calling-fixtures-directly about how to update your code."
  1038. ).format(name=fixture_marker.name or function.__name__)
  1039. @functools.wraps(function)
  1040. def result(*args, **kwargs):
  1041. fail(message, pytrace=False)
  1042. # Keep reference to the original function in our own custom attribute so we don't unwrap
  1043. # further than this point and lose useful wrappings like @mock.patch (#3774).
  1044. result.__pytest_wrapped__ = _PytestWrapper(function) # type: ignore[attr-defined]
  1045. return cast(FixtureFunction, result)
  1046. @final
  1047. @dataclasses.dataclass(frozen=True)
  1048. class FixtureFunctionMarker:
  1049. scope: "Union[_ScopeName, Callable[[str, Config], _ScopeName]]"
  1050. params: Optional[Tuple[object, ...]]
  1051. autouse: bool = False
  1052. ids: Optional[
  1053. Union[Tuple[Optional[object], ...], Callable[[Any], Optional[object]]]
  1054. ] = None
  1055. name: Optional[str] = None
  1056. _ispytest: dataclasses.InitVar[bool] = False
  1057. def __post_init__(self, _ispytest: bool) -> None:
  1058. check_ispytest(_ispytest)
  1059. def __call__(self, function: FixtureFunction) -> FixtureFunction:
  1060. if inspect.isclass(function):
  1061. raise ValueError("class fixtures not supported (maybe in the future)")
  1062. if getattr(function, "_pytestfixturefunction", False):
  1063. raise ValueError(
  1064. "fixture is being applied more than once to the same function"
  1065. )
  1066. function = wrap_function_to_error_out_if_called_directly(function, self)
  1067. name = self.name or function.__name__
  1068. if name == "request":
  1069. location = getlocation(function)
  1070. fail(
  1071. "'request' is a reserved word for fixtures, use another name:\n {}".format(
  1072. location
  1073. ),
  1074. pytrace=False,
  1075. )
  1076. # Type ignored because https://github.com/python/mypy/issues/2087.
  1077. function._pytestfixturefunction = self # type: ignore[attr-defined]
  1078. return function
  1079. @overload
  1080. def fixture(
  1081. fixture_function: FixtureFunction,
  1082. *,
  1083. scope: "Union[_ScopeName, Callable[[str, Config], _ScopeName]]" = ...,
  1084. params: Optional[Iterable[object]] = ...,
  1085. autouse: bool = ...,
  1086. ids: Optional[
  1087. Union[Sequence[Optional[object]], Callable[[Any], Optional[object]]]
  1088. ] = ...,
  1089. name: Optional[str] = ...,
  1090. ) -> FixtureFunction:
  1091. ...
  1092. @overload
  1093. def fixture( # noqa: F811
  1094. fixture_function: None = ...,
  1095. *,
  1096. scope: "Union[_ScopeName, Callable[[str, Config], _ScopeName]]" = ...,
  1097. params: Optional[Iterable[object]] = ...,
  1098. autouse: bool = ...,
  1099. ids: Optional[
  1100. Union[Sequence[Optional[object]], Callable[[Any], Optional[object]]]
  1101. ] = ...,
  1102. name: Optional[str] = None,
  1103. ) -> FixtureFunctionMarker:
  1104. ...
  1105. def fixture( # noqa: F811
  1106. fixture_function: Optional[FixtureFunction] = None,
  1107. *,
  1108. scope: "Union[_ScopeName, Callable[[str, Config], _ScopeName]]" = "function",
  1109. params: Optional[Iterable[object]] = None,
  1110. autouse: bool = False,
  1111. ids: Optional[
  1112. Union[Sequence[Optional[object]], Callable[[Any], Optional[object]]]
  1113. ] = None,
  1114. name: Optional[str] = None,
  1115. ) -> Union[FixtureFunctionMarker, FixtureFunction]:
  1116. """Decorator to mark a fixture factory function.
  1117. This decorator can be used, with or without parameters, to define a
  1118. fixture function.
  1119. The name of the fixture function can later be referenced to cause its
  1120. invocation ahead of running tests: test modules or classes can use the
  1121. ``pytest.mark.usefixtures(fixturename)`` marker.
  1122. Test functions can directly use fixture names as input arguments in which
  1123. case the fixture instance returned from the fixture function will be
  1124. injected.
  1125. Fixtures can provide their values to test functions using ``return`` or
  1126. ``yield`` statements. When using ``yield`` the code block after the
  1127. ``yield`` statement is executed as teardown code regardless of the test
  1128. outcome, and must yield exactly once.
  1129. :param scope:
  1130. The scope for which this fixture is shared; one of ``"function"``
  1131. (default), ``"class"``, ``"module"``, ``"package"`` or ``"session"``.
  1132. This parameter may also be a callable which receives ``(fixture_name, config)``
  1133. as parameters, and must return a ``str`` with one of the values mentioned above.
  1134. See :ref:`dynamic scope` in the docs for more information.
  1135. :param params:
  1136. An optional list of parameters which will cause multiple invocations
  1137. of the fixture function and all of the tests using it. The current
  1138. parameter is available in ``request.param``.
  1139. :param autouse:
  1140. If True, the fixture func is activated for all tests that can see it.
  1141. If False (the default), an explicit reference is needed to activate
  1142. the fixture.
  1143. :param ids:
  1144. Sequence of ids each corresponding to the params so that they are
  1145. part of the test id. If no ids are provided they will be generated
  1146. automatically from the params.
  1147. :param name:
  1148. The name of the fixture. This defaults to the name of the decorated
  1149. function. If a fixture is used in the same module in which it is
  1150. defined, the function name of the fixture will be shadowed by the
  1151. function arg that requests the fixture; one way to resolve this is to
  1152. name the decorated function ``fixture_<fixturename>`` and then use
  1153. ``@pytest.fixture(name='<fixturename>')``.
  1154. """
  1155. fixture_marker = FixtureFunctionMarker(
  1156. scope=scope,
  1157. params=tuple(params) if params is not None else None,
  1158. autouse=autouse,
  1159. ids=None if ids is None else ids if callable(ids) else tuple(ids),
  1160. name=name,
  1161. _ispytest=True,
  1162. )
  1163. # Direct decoration.
  1164. if fixture_function:
  1165. return fixture_marker(fixture_function)
  1166. return fixture_marker
  1167. def yield_fixture(
  1168. fixture_function=None,
  1169. *args,
  1170. scope="function",
  1171. params=None,
  1172. autouse=False,
  1173. ids=None,
  1174. name=None,
  1175. ):
  1176. """(Return a) decorator to mark a yield-fixture factory function.
  1177. .. deprecated:: 3.0
  1178. Use :py:func:`pytest.fixture` directly instead.
  1179. """
  1180. warnings.warn(YIELD_FIXTURE, stacklevel=2)
  1181. return fixture(
  1182. fixture_function,
  1183. *args,
  1184. scope=scope,
  1185. params=params,
  1186. autouse=autouse,
  1187. ids=ids,
  1188. name=name,
  1189. )
  1190. @fixture(scope="session")
  1191. def pytestconfig(request: FixtureRequest) -> Config:
  1192. """Session-scoped fixture that returns the session's :class:`pytest.Config`
  1193. object.
  1194. Example::
  1195. def test_foo(pytestconfig):
  1196. if pytestconfig.getoption("verbose") > 0:
  1197. ...
  1198. """
  1199. return request.config
  1200. def pytest_addoption(parser: Parser) -> None:
  1201. parser.addini(
  1202. "usefixtures",
  1203. type="args",
  1204. default=[],
  1205. help="List of default fixtures to be used with this project",
  1206. )
  1207. class FixtureManager:
  1208. """pytest fixture definitions and information is stored and managed
  1209. from this class.
  1210. During collection fm.parsefactories() is called multiple times to parse
  1211. fixture function definitions into FixtureDef objects and internal
  1212. data structures.
  1213. During collection of test functions, metafunc-mechanics instantiate
  1214. a FuncFixtureInfo object which is cached per node/func-name.
  1215. This FuncFixtureInfo object is later retrieved by Function nodes
  1216. which themselves offer a fixturenames attribute.
  1217. The FuncFixtureInfo object holds information about fixtures and FixtureDefs
  1218. relevant for a particular function. An initial list of fixtures is
  1219. assembled like this:
  1220. - ini-defined usefixtures
  1221. - autouse-marked fixtures along the collection chain up from the function
  1222. - usefixtures markers at module/class/function level
  1223. - test function funcargs
  1224. Subsequently the funcfixtureinfo.fixturenames attribute is computed
  1225. as the closure of the fixtures needed to setup the initial fixtures,
  1226. i.e. fixtures needed by fixture functions themselves are appended
  1227. to the fixturenames list.
  1228. Upon the test-setup phases all fixturenames are instantiated, retrieved
  1229. by a lookup of their FuncFixtureInfo.
  1230. """
  1231. FixtureLookupError = FixtureLookupError
  1232. FixtureLookupErrorRepr = FixtureLookupErrorRepr
  1233. def __init__(self, session: "Session") -> None:
  1234. self.session = session
  1235. self.config: Config = session.config
  1236. self._arg2fixturedefs: Dict[str, List[FixtureDef[Any]]] = {}
  1237. self._holderobjseen: Set[object] = set()
  1238. # A mapping from a nodeid to a list of autouse fixtures it defines.
  1239. self._nodeid_autousenames: Dict[str, List[str]] = {
  1240. "": self.config.getini("usefixtures"),
  1241. }
  1242. session.config.pluginmanager.register(self, "funcmanage")
  1243. def _get_direct_parametrize_args(self, node: nodes.Node) -> List[str]:
  1244. """Return all direct parametrization arguments of a node, so we don't
  1245. mistake them for fixtures.
  1246. Check https://github.com/pytest-dev/pytest/issues/5036.
  1247. These things are done later as well when dealing with parametrization
  1248. so this could be improved.
  1249. """
  1250. parametrize_argnames: List[str] = []
  1251. for marker in node.iter_markers(name="parametrize"):
  1252. if not marker.kwargs.get("indirect", False):
  1253. p_argnames, _ = ParameterSet._parse_parametrize_args(
  1254. *marker.args, **marker.kwargs
  1255. )
  1256. parametrize_argnames.extend(p_argnames)
  1257. return parametrize_argnames
  1258. def getfixtureinfo(
  1259. self, node: nodes.Node, func, cls, funcargs: bool = True
  1260. ) -> FuncFixtureInfo:
  1261. if funcargs and not getattr(node, "nofuncargs", False):
  1262. argnames = getfuncargnames(func, name=node.name, cls=cls)
  1263. else:
  1264. argnames = ()
  1265. usefixtures = tuple(
  1266. arg for mark in node.iter_markers(name="usefixtures") for arg in mark.args
  1267. )
  1268. initialnames = usefixtures + argnames
  1269. fm = node.session._fixturemanager
  1270. initialnames, names_closure, arg2fixturedefs = fm.getfixtureclosure(
  1271. initialnames, node, ignore_args=self._get_direct_parametrize_args(node)
  1272. )
  1273. return FuncFixtureInfo(argnames, initialnames, names_closure, arg2fixturedefs)
  1274. def pytest_plugin_registered(self, plugin: _PluggyPlugin) -> None:
  1275. nodeid = None
  1276. try:
  1277. p = absolutepath(plugin.__file__) # type: ignore[attr-defined]
  1278. except AttributeError:
  1279. pass
  1280. else:
  1281. # Construct the base nodeid which is later used to check
  1282. # what fixtures are visible for particular tests (as denoted
  1283. # by their test id).
  1284. if p.name.startswith("conftest.py"):
  1285. try:
  1286. nodeid = str(p.parent.relative_to(self.config.rootpath))
  1287. except ValueError:
  1288. nodeid = ""
  1289. if nodeid == ".":
  1290. nodeid = ""
  1291. if os.sep != nodes.SEP:
  1292. nodeid = nodeid.replace(os.sep, nodes.SEP)
  1293. self.parsefactories(plugin, nodeid)
  1294. def _getautousenames(self, nodeid: str) -> Iterator[str]:
  1295. """Return the names of autouse fixtures applicable to nodeid."""
  1296. for parentnodeid in nodes.iterparentnodeids(nodeid):
  1297. basenames = self._nodeid_autousenames.get(parentnodeid)
  1298. if basenames:
  1299. yield from basenames
  1300. def getfixtureclosure(
  1301. self,
  1302. fixturenames: Tuple[str, ...],
  1303. parentnode: nodes.Node,
  1304. ignore_args: Sequence[str] = (),
  1305. ) -> Tuple[Tuple[str, ...], List[str], Dict[str, Sequence[FixtureDef[Any]]]]:
  1306. # Collect the closure of all fixtures, starting with the given
  1307. # fixturenames as the initial set. As we have to visit all
  1308. # factory definitions anyway, we also return an arg2fixturedefs
  1309. # mapping so that the caller can reuse it and does not have
  1310. # to re-discover fixturedefs again for each fixturename
  1311. # (discovering matching fixtures for a given name/node is expensive).
  1312. parentid = parentnode.nodeid
  1313. fixturenames_closure = list(self._getautousenames(parentid))
  1314. def merge(otherlist: Iterable[str]) -> None:
  1315. for arg in otherlist:
  1316. if arg not in fixturenames_closure:
  1317. fixturenames_closure.append(arg)
  1318. merge(fixturenames)
  1319. # At this point, fixturenames_closure contains what we call "initialnames",
  1320. # which is a set of fixturenames the function immediately requests. We
  1321. # need to return it as well, so save this.
  1322. initialnames = tuple(fixturenames_closure)
  1323. arg2fixturedefs: Dict[str, Sequence[FixtureDef[Any]]] = {}
  1324. lastlen = -1
  1325. while lastlen != len(fixturenames_closure):
  1326. lastlen = len(fixturenames_closure)
  1327. for argname in fixturenames_closure:
  1328. if argname in ignore_args:
  1329. continue
  1330. if argname in arg2fixturedefs:
  1331. continue
  1332. fixturedefs = self.getfixturedefs(argname, parentid)
  1333. if fixturedefs:
  1334. arg2fixturedefs[argname] = fixturedefs
  1335. merge(fixturedefs[-1].argnames)
  1336. def sort_by_scope(arg_name: str) -> Scope:
  1337. try:
  1338. fixturedefs = arg2fixturedefs[arg_name]
  1339. except KeyError:
  1340. return Scope.Function
  1341. else:
  1342. return fixturedefs[-1]._scope
  1343. fixturenames_closure.sort(key=sort_by_scope, reverse=True)
  1344. return initialnames, fixturenames_closure, arg2fixturedefs
  1345. def pytest_generate_tests(self, metafunc: "Metafunc") -> None:
  1346. """Generate new tests based on parametrized fixtures used by the given metafunc"""
  1347. def get_parametrize_mark_argnames(mark: Mark) -> Sequence[str]:
  1348. args, _ = ParameterSet._parse_parametrize_args(*mark.args, **mark.kwargs)
  1349. return args
  1350. for argname in metafunc.fixturenames:
  1351. # Get the FixtureDefs for the argname.
  1352. fixture_defs = metafunc._arg2fixturedefs.get(argname)
  1353. if not fixture_defs:
  1354. # Will raise FixtureLookupError at setup time if not parametrized somewhere
  1355. # else (e.g @pytest.mark.parametrize)
  1356. continue
  1357. # If the test itself parametrizes using this argname, give it
  1358. # precedence.
  1359. if any(
  1360. argname in get_parametrize_mark_argnames(mark)
  1361. for mark in metafunc.definition.iter_markers("parametrize")
  1362. ):
  1363. continue
  1364. # In the common case we only look at the fixture def with the
  1365. # closest scope (last in the list). But if the fixture overrides
  1366. # another fixture, while requesting the super fixture, keep going
  1367. # in case the super fixture is parametrized (#1953).
  1368. for fixturedef in reversed(fixture_defs):
  1369. # Fixture is parametrized, apply it and stop.
  1370. if fixturedef.params is not None:
  1371. metafunc.parametrize(
  1372. argname,
  1373. fixturedef.params,
  1374. indirect=True,
  1375. scope=fixturedef.scope,
  1376. ids=fixturedef.ids,
  1377. )
  1378. break
  1379. # Not requesting the overridden super fixture, stop.
  1380. if argname not in fixturedef.argnames:
  1381. break
  1382. # Try next super fixture, if any.
  1383. def pytest_collection_modifyitems(self, items: List[nodes.Item]) -> None:
  1384. # Separate parametrized setups.
  1385. items[:] = reorder_items(items)
  1386. @overload
  1387. def parsefactories(
  1388. self,
  1389. node_or_obj: nodes.Node,
  1390. *,
  1391. unittest: bool = ...,
  1392. ) -> None:
  1393. raise NotImplementedError()
  1394. @overload
  1395. def parsefactories( # noqa: F811
  1396. self,
  1397. node_or_obj: object,
  1398. nodeid: Optional[str],
  1399. *,
  1400. unittest: bool = ...,
  1401. ) -> None:
  1402. raise NotImplementedError()
  1403. def parsefactories( # noqa: F811
  1404. self,
  1405. node_or_obj: Union[nodes.Node, object],
  1406. nodeid: Union[str, NotSetType, None] = NOTSET,
  1407. *,
  1408. unittest: bool = False,
  1409. ) -> None:
  1410. """Collect fixtures from a collection node or object.
  1411. Found fixtures are parsed into `FixtureDef`s and saved.
  1412. If `node_or_object` is a collection node (with an underlying Python
  1413. object), the node's object is traversed and the node's nodeid is used to
  1414. determine the fixtures' visibilty. `nodeid` must not be specified in
  1415. this case.
  1416. If `node_or_object` is an object (e.g. a plugin), the object is
  1417. traversed and the given `nodeid` is used to determine the fixtures'
  1418. visibility. `nodeid` must be specified in this case; None and "" mean
  1419. total visibility.
  1420. """
  1421. if nodeid is not NOTSET:
  1422. holderobj = node_or_obj
  1423. else:
  1424. assert isinstance(node_or_obj, nodes.Node)
  1425. holderobj = cast(object, node_or_obj.obj) # type: ignore[attr-defined]
  1426. assert isinstance(node_or_obj.nodeid, str)
  1427. nodeid = node_or_obj.nodeid
  1428. if holderobj in self._holderobjseen:
  1429. return
  1430. self._holderobjseen.add(holderobj)
  1431. autousenames = []
  1432. for name in dir(holderobj):
  1433. # ugly workaround for one of the fspath deprecated property of node
  1434. # todo: safely generalize
  1435. if isinstance(holderobj, nodes.Node) and name == "fspath":
  1436. continue
  1437. # The attribute can be an arbitrary descriptor, so the attribute
  1438. # access below can raise. safe_getatt() ignores such exceptions.
  1439. obj = safe_getattr(holderobj, name, None)
  1440. marker = getfixturemarker(obj)
  1441. if not isinstance(marker, FixtureFunctionMarker):
  1442. # Magic globals with __getattr__ might have got us a wrong
  1443. # fixture attribute.
  1444. continue
  1445. if marker.name:
  1446. name = marker.name
  1447. # During fixture definition we wrap the original fixture function
  1448. # to issue a warning if called directly, so here we unwrap it in
  1449. # order to not emit the warning when pytest itself calls the
  1450. # fixture function.
  1451. obj = get_real_method(obj, holderobj)
  1452. fixture_def = FixtureDef(
  1453. fixturemanager=self,
  1454. baseid=nodeid,
  1455. argname=name,
  1456. func=obj,
  1457. scope=marker.scope,
  1458. params=marker.params,
  1459. unittest=unittest,
  1460. ids=marker.ids,
  1461. )
  1462. faclist = self._arg2fixturedefs.setdefault(name, [])
  1463. if fixture_def.has_location:
  1464. faclist.append(fixture_def)
  1465. else:
  1466. # fixturedefs with no location are at the front
  1467. # so this inserts the current fixturedef after the
  1468. # existing fixturedefs from external plugins but
  1469. # before the fixturedefs provided in conftests.
  1470. i = len([f for f in faclist if not f.has_location])
  1471. faclist.insert(i, fixture_def)
  1472. if marker.autouse:
  1473. autousenames.append(name)
  1474. if autousenames:
  1475. self._nodeid_autousenames.setdefault(nodeid or "", []).extend(autousenames)
  1476. def getfixturedefs(
  1477. self, argname: str, nodeid: str
  1478. ) -> Optional[Sequence[FixtureDef[Any]]]:
  1479. """Get a list of fixtures which are applicable to the given node id.
  1480. :param str argname: Name of the fixture to search for.
  1481. :param str nodeid: Full node id of the requesting test.
  1482. :rtype: Sequence[FixtureDef]
  1483. """
  1484. try:
  1485. fixturedefs = self._arg2fixturedefs[argname]
  1486. except KeyError:
  1487. return None
  1488. return tuple(self._matchfactories(fixturedefs, nodeid))
  1489. def _matchfactories(
  1490. self, fixturedefs: Iterable[FixtureDef[Any]], nodeid: str
  1491. ) -> Iterator[FixtureDef[Any]]:
  1492. parentnodeids = set(nodes.iterparentnodeids(nodeid))
  1493. for fixturedef in fixturedefs:
  1494. if fixturedef.baseid in parentnodeids:
  1495. yield fixturedef