You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1681 lines
64KB

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