fixtures.py 63 KB

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