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.

592 lines
19KB

  1. import os
  2. import warnings
  3. from pathlib import Path
  4. from typing import Callable
  5. from typing import Iterable
  6. from typing import Iterator
  7. from typing import List
  8. from typing import Optional
  9. from typing import overload
  10. from typing import Set
  11. from typing import Tuple
  12. from typing import Type
  13. from typing import TYPE_CHECKING
  14. from typing import TypeVar
  15. from typing import Union
  16. import py
  17. import _pytest._code
  18. from _pytest._code import getfslineno
  19. from _pytest._code.code import ExceptionInfo
  20. from _pytest._code.code import TerminalRepr
  21. from _pytest.compat import cached_property
  22. from _pytest.config import Config
  23. from _pytest.config import ConftestImportFailure
  24. from _pytest.deprecated import FSCOLLECTOR_GETHOOKPROXY_ISINITPATH
  25. from _pytest.mark.structures import Mark
  26. from _pytest.mark.structures import MarkDecorator
  27. from _pytest.mark.structures import NodeKeywords
  28. from _pytest.outcomes import fail
  29. from _pytest.pathlib import absolutepath
  30. from _pytest.store import Store
  31. if TYPE_CHECKING:
  32. # Imported here due to circular import.
  33. from _pytest.main import Session
  34. from _pytest._code.code import _TracebackStyle
  35. SEP = "/"
  36. tracebackcutdir = py.path.local(_pytest.__file__).dirpath()
  37. def iterparentnodeids(nodeid: str) -> Iterator[str]:
  38. """Return the parent node IDs of a given node ID, inclusive.
  39. For the node ID
  40. "testing/code/test_excinfo.py::TestFormattedExcinfo::test_repr_source"
  41. the result would be
  42. ""
  43. "testing"
  44. "testing/code"
  45. "testing/code/test_excinfo.py"
  46. "testing/code/test_excinfo.py::TestFormattedExcinfo"
  47. "testing/code/test_excinfo.py::TestFormattedExcinfo::test_repr_source"
  48. Note that :: parts are only considered at the last / component.
  49. """
  50. pos = 0
  51. sep = SEP
  52. yield ""
  53. while True:
  54. at = nodeid.find(sep, pos)
  55. if at == -1 and sep == SEP:
  56. sep = "::"
  57. elif at == -1:
  58. if nodeid:
  59. yield nodeid
  60. break
  61. else:
  62. if at:
  63. yield nodeid[:at]
  64. pos = at + len(sep)
  65. _NodeType = TypeVar("_NodeType", bound="Node")
  66. class NodeMeta(type):
  67. def __call__(self, *k, **kw):
  68. msg = (
  69. "Direct construction of {name} has been deprecated, please use {name}.from_parent.\n"
  70. "See "
  71. "https://docs.pytest.org/en/stable/deprecations.html#node-construction-changed-to-node-from-parent"
  72. " for more details."
  73. ).format(name=self.__name__)
  74. fail(msg, pytrace=False)
  75. def _create(self, *k, **kw):
  76. return super().__call__(*k, **kw)
  77. class Node(metaclass=NodeMeta):
  78. """Base class for Collector and Item, the components of the test
  79. collection tree.
  80. Collector subclasses have children; Items are leaf nodes.
  81. """
  82. # Use __slots__ to make attribute access faster.
  83. # Note that __dict__ is still available.
  84. __slots__ = (
  85. "name",
  86. "parent",
  87. "config",
  88. "session",
  89. "fspath",
  90. "_nodeid",
  91. "_store",
  92. "__dict__",
  93. )
  94. def __init__(
  95. self,
  96. name: str,
  97. parent: "Optional[Node]" = None,
  98. config: Optional[Config] = None,
  99. session: "Optional[Session]" = None,
  100. fspath: Optional[py.path.local] = None,
  101. nodeid: Optional[str] = None,
  102. ) -> None:
  103. #: A unique name within the scope of the parent node.
  104. self.name = name
  105. #: The parent collector node.
  106. self.parent = parent
  107. #: The pytest config object.
  108. if config:
  109. self.config: Config = config
  110. else:
  111. if not parent:
  112. raise TypeError("config or parent must be provided")
  113. self.config = parent.config
  114. #: The pytest session this node is part of.
  115. if session:
  116. self.session = session
  117. else:
  118. if not parent:
  119. raise TypeError("session or parent must be provided")
  120. self.session = parent.session
  121. #: Filesystem path where this node was collected from (can be None).
  122. self.fspath = fspath or getattr(parent, "fspath", None)
  123. #: Keywords/markers collected from all scopes.
  124. self.keywords = NodeKeywords(self)
  125. #: The marker objects belonging to this node.
  126. self.own_markers: List[Mark] = []
  127. #: Allow adding of extra keywords to use for matching.
  128. self.extra_keyword_matches: Set[str] = set()
  129. if nodeid is not None:
  130. assert "::()" not in nodeid
  131. self._nodeid = nodeid
  132. else:
  133. if not self.parent:
  134. raise TypeError("nodeid or parent must be provided")
  135. self._nodeid = self.parent.nodeid
  136. if self.name != "()":
  137. self._nodeid += "::" + self.name
  138. # A place where plugins can store information on the node for their
  139. # own use. Currently only intended for internal plugins.
  140. self._store = Store()
  141. @classmethod
  142. def from_parent(cls, parent: "Node", **kw):
  143. """Public constructor for Nodes.
  144. This indirection got introduced in order to enable removing
  145. the fragile logic from the node constructors.
  146. Subclasses can use ``super().from_parent(...)`` when overriding the
  147. construction.
  148. :param parent: The parent node of this Node.
  149. """
  150. if "config" in kw:
  151. raise TypeError("config is not a valid argument for from_parent")
  152. if "session" in kw:
  153. raise TypeError("session is not a valid argument for from_parent")
  154. return cls._create(parent=parent, **kw)
  155. @property
  156. def ihook(self):
  157. """fspath-sensitive hook proxy used to call pytest hooks."""
  158. return self.session.gethookproxy(self.fspath)
  159. def __repr__(self) -> str:
  160. return "<{} {}>".format(self.__class__.__name__, getattr(self, "name", None))
  161. def warn(self, warning: Warning) -> None:
  162. """Issue a warning for this Node.
  163. Warnings will be displayed after the test session, unless explicitly suppressed.
  164. :param Warning warning:
  165. The warning instance to issue.
  166. :raises ValueError: If ``warning`` instance is not a subclass of Warning.
  167. Example usage:
  168. .. code-block:: python
  169. node.warn(PytestWarning("some message"))
  170. node.warn(UserWarning("some message"))
  171. .. versionchanged:: 6.2
  172. Any subclass of :class:`Warning` is now accepted, rather than only
  173. :class:`PytestWarning <pytest.PytestWarning>` subclasses.
  174. """
  175. # enforce type checks here to avoid getting a generic type error later otherwise.
  176. if not isinstance(warning, Warning):
  177. raise ValueError(
  178. "warning must be an instance of Warning or subclass, got {!r}".format(
  179. warning
  180. )
  181. )
  182. path, lineno = get_fslocation_from_item(self)
  183. assert lineno is not None
  184. warnings.warn_explicit(
  185. warning, category=None, filename=str(path), lineno=lineno + 1,
  186. )
  187. # Methods for ordering nodes.
  188. @property
  189. def nodeid(self) -> str:
  190. """A ::-separated string denoting its collection tree address."""
  191. return self._nodeid
  192. def __hash__(self) -> int:
  193. return hash(self._nodeid)
  194. def setup(self) -> None:
  195. pass
  196. def teardown(self) -> None:
  197. pass
  198. def listchain(self) -> List["Node"]:
  199. """Return list of all parent collectors up to self, starting from
  200. the root of collection tree."""
  201. chain = []
  202. item: Optional[Node] = self
  203. while item is not None:
  204. chain.append(item)
  205. item = item.parent
  206. chain.reverse()
  207. return chain
  208. def add_marker(
  209. self, marker: Union[str, MarkDecorator], append: bool = True
  210. ) -> None:
  211. """Dynamically add a marker object to the node.
  212. :param append:
  213. Whether to append the marker, or prepend it.
  214. """
  215. from _pytest.mark import MARK_GEN
  216. if isinstance(marker, MarkDecorator):
  217. marker_ = marker
  218. elif isinstance(marker, str):
  219. marker_ = getattr(MARK_GEN, marker)
  220. else:
  221. raise ValueError("is not a string or pytest.mark.* Marker")
  222. self.keywords[marker_.name] = marker_
  223. if append:
  224. self.own_markers.append(marker_.mark)
  225. else:
  226. self.own_markers.insert(0, marker_.mark)
  227. def iter_markers(self, name: Optional[str] = None) -> Iterator[Mark]:
  228. """Iterate over all markers of the node.
  229. :param name: If given, filter the results by the name attribute.
  230. """
  231. return (x[1] for x in self.iter_markers_with_node(name=name))
  232. def iter_markers_with_node(
  233. self, name: Optional[str] = None
  234. ) -> Iterator[Tuple["Node", Mark]]:
  235. """Iterate over all markers of the node.
  236. :param name: If given, filter the results by the name attribute.
  237. :returns: An iterator of (node, mark) tuples.
  238. """
  239. for node in reversed(self.listchain()):
  240. for mark in node.own_markers:
  241. if name is None or getattr(mark, "name", None) == name:
  242. yield node, mark
  243. @overload
  244. def get_closest_marker(self, name: str) -> Optional[Mark]:
  245. ...
  246. @overload
  247. def get_closest_marker(self, name: str, default: Mark) -> Mark:
  248. ...
  249. def get_closest_marker(
  250. self, name: str, default: Optional[Mark] = None
  251. ) -> Optional[Mark]:
  252. """Return the first marker matching the name, from closest (for
  253. example function) to farther level (for example module level).
  254. :param default: Fallback return value if no marker was found.
  255. :param name: Name to filter by.
  256. """
  257. return next(self.iter_markers(name=name), default)
  258. def listextrakeywords(self) -> Set[str]:
  259. """Return a set of all extra keywords in self and any parents."""
  260. extra_keywords: Set[str] = set()
  261. for item in self.listchain():
  262. extra_keywords.update(item.extra_keyword_matches)
  263. return extra_keywords
  264. def listnames(self) -> List[str]:
  265. return [x.name for x in self.listchain()]
  266. def addfinalizer(self, fin: Callable[[], object]) -> None:
  267. """Register a function to be called when this node is finalized.
  268. This method can only be called when this node is active
  269. in a setup chain, for example during self.setup().
  270. """
  271. self.session._setupstate.addfinalizer(fin, self)
  272. def getparent(self, cls: Type[_NodeType]) -> Optional[_NodeType]:
  273. """Get the next parent node (including self) which is an instance of
  274. the given class."""
  275. current: Optional[Node] = self
  276. while current and not isinstance(current, cls):
  277. current = current.parent
  278. assert current is None or isinstance(current, cls)
  279. return current
  280. def _prunetraceback(self, excinfo: ExceptionInfo[BaseException]) -> None:
  281. pass
  282. def _repr_failure_py(
  283. self,
  284. excinfo: ExceptionInfo[BaseException],
  285. style: "Optional[_TracebackStyle]" = None,
  286. ) -> TerminalRepr:
  287. from _pytest.fixtures import FixtureLookupError
  288. if isinstance(excinfo.value, ConftestImportFailure):
  289. excinfo = ExceptionInfo(excinfo.value.excinfo)
  290. if isinstance(excinfo.value, fail.Exception):
  291. if not excinfo.value.pytrace:
  292. style = "value"
  293. if isinstance(excinfo.value, FixtureLookupError):
  294. return excinfo.value.formatrepr()
  295. if self.config.getoption("fulltrace", False):
  296. style = "long"
  297. else:
  298. tb = _pytest._code.Traceback([excinfo.traceback[-1]])
  299. self._prunetraceback(excinfo)
  300. if len(excinfo.traceback) == 0:
  301. excinfo.traceback = tb
  302. if style == "auto":
  303. style = "long"
  304. # XXX should excinfo.getrepr record all data and toterminal() process it?
  305. if style is None:
  306. if self.config.getoption("tbstyle", "auto") == "short":
  307. style = "short"
  308. else:
  309. style = "long"
  310. if self.config.getoption("verbose", 0) > 1:
  311. truncate_locals = False
  312. else:
  313. truncate_locals = True
  314. # excinfo.getrepr() formats paths relative to the CWD if `abspath` is False.
  315. # It is possible for a fixture/test to change the CWD while this code runs, which
  316. # would then result in the user seeing confusing paths in the failure message.
  317. # To fix this, if the CWD changed, always display the full absolute path.
  318. # It will be better to just always display paths relative to invocation_dir, but
  319. # this requires a lot of plumbing (#6428).
  320. try:
  321. abspath = Path(os.getcwd()) != self.config.invocation_params.dir
  322. except OSError:
  323. abspath = True
  324. return excinfo.getrepr(
  325. funcargs=True,
  326. abspath=abspath,
  327. showlocals=self.config.getoption("showlocals", False),
  328. style=style,
  329. tbfilter=False, # pruned already, or in --fulltrace mode.
  330. truncate_locals=truncate_locals,
  331. )
  332. def repr_failure(
  333. self,
  334. excinfo: ExceptionInfo[BaseException],
  335. style: "Optional[_TracebackStyle]" = None,
  336. ) -> Union[str, TerminalRepr]:
  337. """Return a representation of a collection or test failure.
  338. :param excinfo: Exception information for the failure.
  339. """
  340. return self._repr_failure_py(excinfo, style)
  341. def get_fslocation_from_item(
  342. node: "Node",
  343. ) -> Tuple[Union[str, py.path.local], Optional[int]]:
  344. """Try to extract the actual location from a node, depending on available attributes:
  345. * "location": a pair (path, lineno)
  346. * "obj": a Python object that the node wraps.
  347. * "fspath": just a path
  348. :rtype: A tuple of (str|py.path.local, int) with filename and line number.
  349. """
  350. # See Item.location.
  351. location: Optional[Tuple[str, Optional[int], str]] = getattr(node, "location", None)
  352. if location is not None:
  353. return location[:2]
  354. obj = getattr(node, "obj", None)
  355. if obj is not None:
  356. return getfslineno(obj)
  357. return getattr(node, "fspath", "unknown location"), -1
  358. class Collector(Node):
  359. """Collector instances create children through collect() and thus
  360. iteratively build a tree."""
  361. class CollectError(Exception):
  362. """An error during collection, contains a custom message."""
  363. def collect(self) -> Iterable[Union["Item", "Collector"]]:
  364. """Return a list of children (items and collectors) for this
  365. collection node."""
  366. raise NotImplementedError("abstract")
  367. # TODO: This omits the style= parameter which breaks Liskov Substitution.
  368. def repr_failure( # type: ignore[override]
  369. self, excinfo: ExceptionInfo[BaseException]
  370. ) -> Union[str, TerminalRepr]:
  371. """Return a representation of a collection failure.
  372. :param excinfo: Exception information for the failure.
  373. """
  374. if isinstance(excinfo.value, self.CollectError) and not self.config.getoption(
  375. "fulltrace", False
  376. ):
  377. exc = excinfo.value
  378. return str(exc.args[0])
  379. # Respect explicit tbstyle option, but default to "short"
  380. # (_repr_failure_py uses "long" with "fulltrace" option always).
  381. tbstyle = self.config.getoption("tbstyle", "auto")
  382. if tbstyle == "auto":
  383. tbstyle = "short"
  384. return self._repr_failure_py(excinfo, style=tbstyle)
  385. def _prunetraceback(self, excinfo: ExceptionInfo[BaseException]) -> None:
  386. if hasattr(self, "fspath"):
  387. traceback = excinfo.traceback
  388. ntraceback = traceback.cut(path=self.fspath)
  389. if ntraceback == traceback:
  390. ntraceback = ntraceback.cut(excludepath=tracebackcutdir)
  391. excinfo.traceback = ntraceback.filter()
  392. def _check_initialpaths_for_relpath(session, fspath):
  393. for initial_path in session._initialpaths:
  394. if fspath.common(initial_path) == initial_path:
  395. return fspath.relto(initial_path)
  396. class FSCollector(Collector):
  397. def __init__(
  398. self,
  399. fspath: py.path.local,
  400. parent=None,
  401. config: Optional[Config] = None,
  402. session: Optional["Session"] = None,
  403. nodeid: Optional[str] = None,
  404. ) -> None:
  405. name = fspath.basename
  406. if parent is not None:
  407. rel = fspath.relto(parent.fspath)
  408. if rel:
  409. name = rel
  410. name = name.replace(os.sep, SEP)
  411. self.fspath = fspath
  412. session = session or parent.session
  413. if nodeid is None:
  414. nodeid = self.fspath.relto(session.config.rootdir)
  415. if not nodeid:
  416. nodeid = _check_initialpaths_for_relpath(session, fspath)
  417. if nodeid and os.sep != SEP:
  418. nodeid = nodeid.replace(os.sep, SEP)
  419. super().__init__(name, parent, config, session, nodeid=nodeid, fspath=fspath)
  420. @classmethod
  421. def from_parent(cls, parent, *, fspath, **kw):
  422. """The public constructor."""
  423. return super().from_parent(parent=parent, fspath=fspath, **kw)
  424. def gethookproxy(self, fspath: py.path.local):
  425. warnings.warn(FSCOLLECTOR_GETHOOKPROXY_ISINITPATH, stacklevel=2)
  426. return self.session.gethookproxy(fspath)
  427. def isinitpath(self, path: py.path.local) -> bool:
  428. warnings.warn(FSCOLLECTOR_GETHOOKPROXY_ISINITPATH, stacklevel=2)
  429. return self.session.isinitpath(path)
  430. class File(FSCollector):
  431. """Base class for collecting tests from a file.
  432. :ref:`non-python tests`.
  433. """
  434. class Item(Node):
  435. """A basic test invocation item.
  436. Note that for a single function there might be multiple test invocation items.
  437. """
  438. nextitem = None
  439. def __init__(
  440. self,
  441. name,
  442. parent=None,
  443. config: Optional[Config] = None,
  444. session: Optional["Session"] = None,
  445. nodeid: Optional[str] = None,
  446. ) -> None:
  447. super().__init__(name, parent, config, session, nodeid=nodeid)
  448. self._report_sections: List[Tuple[str, str, str]] = []
  449. #: A list of tuples (name, value) that holds user defined properties
  450. #: for this test.
  451. self.user_properties: List[Tuple[str, object]] = []
  452. def runtest(self) -> None:
  453. raise NotImplementedError("runtest must be implemented by Item subclass")
  454. def add_report_section(self, when: str, key: str, content: str) -> None:
  455. """Add a new report section, similar to what's done internally to add
  456. stdout and stderr captured output::
  457. item.add_report_section("call", "stdout", "report section contents")
  458. :param str when:
  459. One of the possible capture states, ``"setup"``, ``"call"``, ``"teardown"``.
  460. :param str key:
  461. Name of the section, can be customized at will. Pytest uses ``"stdout"`` and
  462. ``"stderr"`` internally.
  463. :param str content:
  464. The full contents as a string.
  465. """
  466. if content:
  467. self._report_sections.append((when, key, content))
  468. def reportinfo(self) -> Tuple[Union[py.path.local, str], Optional[int], str]:
  469. return self.fspath, None, ""
  470. @cached_property
  471. def location(self) -> Tuple[str, Optional[int], str]:
  472. location = self.reportinfo()
  473. fspath = absolutepath(str(location[0]))
  474. relfspath = self.session._node_location_to_relpath(fspath)
  475. assert type(location[2]) is str
  476. return (relfspath, location[1], location[2])