nodes.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import
  3. from __future__ import division
  4. from __future__ import print_function
  5. import os
  6. import warnings
  7. import py
  8. import six
  9. import _pytest._code
  10. from _pytest.compat import getfslineno
  11. from _pytest.mark.structures import NodeKeywords
  12. from _pytest.outcomes import fail
  13. SEP = "/"
  14. tracebackcutdir = py.path.local(_pytest.__file__).dirpath()
  15. def _splitnode(nodeid):
  16. """Split a nodeid into constituent 'parts'.
  17. Node IDs are strings, and can be things like:
  18. ''
  19. 'testing/code'
  20. 'testing/code/test_excinfo.py'
  21. 'testing/code/test_excinfo.py::TestFormattedExcinfo'
  22. Return values are lists e.g.
  23. []
  24. ['testing', 'code']
  25. ['testing', 'code', 'test_excinfo.py']
  26. ['testing', 'code', 'test_excinfo.py', 'TestFormattedExcinfo', '()']
  27. """
  28. if nodeid == "":
  29. # If there is no root node at all, return an empty list so the caller's logic can remain sane
  30. return []
  31. parts = nodeid.split(SEP)
  32. # Replace single last element 'test_foo.py::Bar' with multiple elements 'test_foo.py', 'Bar'
  33. parts[-1:] = parts[-1].split("::")
  34. return parts
  35. def ischildnode(baseid, nodeid):
  36. """Return True if the nodeid is a child node of the baseid.
  37. E.g. 'foo/bar::Baz' is a child of 'foo', 'foo/bar' and 'foo/bar::Baz', but not of 'foo/blorp'
  38. """
  39. base_parts = _splitnode(baseid)
  40. node_parts = _splitnode(nodeid)
  41. if len(node_parts) < len(base_parts):
  42. return False
  43. return node_parts[: len(base_parts)] == base_parts
  44. class Node(object):
  45. """ base class for Collector and Item the test collection tree.
  46. Collector subclasses have children, Items are terminal nodes."""
  47. def __init__(
  48. self, name, parent=None, config=None, session=None, fspath=None, nodeid=None
  49. ):
  50. #: a unique name within the scope of the parent node
  51. self.name = name
  52. #: the parent collector node.
  53. self.parent = parent
  54. #: the pytest config object
  55. self.config = config or parent.config
  56. #: the session this node is part of
  57. self.session = session or parent.session
  58. #: filesystem path where this node was collected from (can be None)
  59. self.fspath = fspath or getattr(parent, "fspath", None)
  60. #: keywords/markers collected from all scopes
  61. self.keywords = NodeKeywords(self)
  62. #: the marker objects belonging to this node
  63. self.own_markers = []
  64. #: allow adding of extra keywords to use for matching
  65. self.extra_keyword_matches = set()
  66. # used for storing artificial fixturedefs for direct parametrization
  67. self._name2pseudofixturedef = {}
  68. if nodeid is not None:
  69. assert "::()" not in nodeid
  70. self._nodeid = nodeid
  71. else:
  72. self._nodeid = self.parent.nodeid
  73. if self.name != "()":
  74. self._nodeid += "::" + self.name
  75. @property
  76. def ihook(self):
  77. """ fspath sensitive hook proxy used to call pytest hooks"""
  78. return self.session.gethookproxy(self.fspath)
  79. def __repr__(self):
  80. return "<%s %s>" % (self.__class__.__name__, getattr(self, "name", None))
  81. def warn(self, warning):
  82. """Issue a warning for this item.
  83. Warnings will be displayed after the test session, unless explicitly suppressed
  84. :param Warning warning: the warning instance to issue. Must be a subclass of PytestWarning.
  85. :raise ValueError: if ``warning`` instance is not a subclass of PytestWarning.
  86. Example usage:
  87. .. code-block:: python
  88. node.warn(PytestWarning("some message"))
  89. """
  90. from _pytest.warning_types import PytestWarning
  91. if not isinstance(warning, PytestWarning):
  92. raise ValueError(
  93. "warning must be an instance of PytestWarning or subclass, got {!r}".format(
  94. warning
  95. )
  96. )
  97. path, lineno = get_fslocation_from_item(self)
  98. warnings.warn_explicit(
  99. warning,
  100. category=None,
  101. filename=str(path),
  102. lineno=lineno + 1 if lineno is not None else None,
  103. )
  104. # methods for ordering nodes
  105. @property
  106. def nodeid(self):
  107. """ a ::-separated string denoting its collection tree address. """
  108. return self._nodeid
  109. def __hash__(self):
  110. return hash(self.nodeid)
  111. def setup(self):
  112. pass
  113. def teardown(self):
  114. pass
  115. def listchain(self):
  116. """ return list of all parent collectors up to self,
  117. starting from root of collection tree. """
  118. chain = []
  119. item = self
  120. while item is not None:
  121. chain.append(item)
  122. item = item.parent
  123. chain.reverse()
  124. return chain
  125. def add_marker(self, marker, append=True):
  126. """dynamically add a marker object to the node.
  127. :type marker: ``str`` or ``pytest.mark.*`` object
  128. :param marker:
  129. ``append=True`` whether to append the marker,
  130. if ``False`` insert at position ``0``.
  131. """
  132. from _pytest.mark import MarkDecorator, MARK_GEN
  133. if isinstance(marker, six.string_types):
  134. marker = getattr(MARK_GEN, marker)
  135. elif not isinstance(marker, MarkDecorator):
  136. raise ValueError("is not a string or pytest.mark.* Marker")
  137. self.keywords[marker.name] = marker
  138. if append:
  139. self.own_markers.append(marker.mark)
  140. else:
  141. self.own_markers.insert(0, marker.mark)
  142. def iter_markers(self, name=None):
  143. """
  144. :param name: if given, filter the results by the name attribute
  145. iterate over all markers of the node
  146. """
  147. return (x[1] for x in self.iter_markers_with_node(name=name))
  148. def iter_markers_with_node(self, name=None):
  149. """
  150. :param name: if given, filter the results by the name attribute
  151. iterate over all markers of the node
  152. returns sequence of tuples (node, mark)
  153. """
  154. for node in reversed(self.listchain()):
  155. for mark in node.own_markers:
  156. if name is None or getattr(mark, "name", None) == name:
  157. yield node, mark
  158. def get_closest_marker(self, name, default=None):
  159. """return the first marker matching the name, from closest (for example function) to farther level (for example
  160. module level).
  161. :param default: fallback return value of no marker was found
  162. :param name: name to filter by
  163. """
  164. return next(self.iter_markers(name=name), default)
  165. def listextrakeywords(self):
  166. """ Return a set of all extra keywords in self and any parents."""
  167. extra_keywords = set()
  168. for item in self.listchain():
  169. extra_keywords.update(item.extra_keyword_matches)
  170. return extra_keywords
  171. def listnames(self):
  172. return [x.name for x in self.listchain()]
  173. def addfinalizer(self, fin):
  174. """ register a function to be called when this node is finalized.
  175. This method can only be called when this node is active
  176. in a setup chain, for example during self.setup().
  177. """
  178. self.session._setupstate.addfinalizer(fin, self)
  179. def getparent(self, cls):
  180. """ get the next parent node (including ourself)
  181. which is an instance of the given class"""
  182. current = self
  183. while current and not isinstance(current, cls):
  184. current = current.parent
  185. return current
  186. def _prunetraceback(self, excinfo):
  187. pass
  188. def _repr_failure_py(self, excinfo, style=None):
  189. if excinfo.errisinstance(fail.Exception):
  190. if not excinfo.value.pytrace:
  191. return six.text_type(excinfo.value)
  192. fm = self.session._fixturemanager
  193. if excinfo.errisinstance(fm.FixtureLookupError):
  194. return excinfo.value.formatrepr()
  195. tbfilter = True
  196. if self.config.getoption("fulltrace", False):
  197. style = "long"
  198. else:
  199. tb = _pytest._code.Traceback([excinfo.traceback[-1]])
  200. self._prunetraceback(excinfo)
  201. if len(excinfo.traceback) == 0:
  202. excinfo.traceback = tb
  203. tbfilter = False # prunetraceback already does it
  204. if style == "auto":
  205. style = "long"
  206. # XXX should excinfo.getrepr record all data and toterminal() process it?
  207. if style is None:
  208. if self.config.getoption("tbstyle", "auto") == "short":
  209. style = "short"
  210. else:
  211. style = "long"
  212. if self.config.getoption("verbose", 0) > 1:
  213. truncate_locals = False
  214. else:
  215. truncate_locals = True
  216. try:
  217. os.getcwd()
  218. abspath = False
  219. except OSError:
  220. abspath = True
  221. return excinfo.getrepr(
  222. funcargs=True,
  223. abspath=abspath,
  224. showlocals=self.config.getoption("showlocals", False),
  225. style=style,
  226. tbfilter=tbfilter,
  227. truncate_locals=truncate_locals,
  228. )
  229. repr_failure = _repr_failure_py
  230. def get_fslocation_from_item(item):
  231. """Tries to extract the actual location from an item, depending on available attributes:
  232. * "fslocation": a pair (path, lineno)
  233. * "obj": a Python object that the item wraps.
  234. * "fspath": just a path
  235. :rtype: a tuple of (str|LocalPath, int) with filename and line number.
  236. """
  237. result = getattr(item, "location", None)
  238. if result is not None:
  239. return result[:2]
  240. obj = getattr(item, "obj", None)
  241. if obj is not None:
  242. return getfslineno(obj)
  243. return getattr(item, "fspath", "unknown location"), -1
  244. class Collector(Node):
  245. """ Collector instances create children through collect()
  246. and thus iteratively build a tree.
  247. """
  248. class CollectError(Exception):
  249. """ an error during collection, contains a custom message. """
  250. def collect(self):
  251. """ returns a list of children (items and collectors)
  252. for this collection node.
  253. """
  254. raise NotImplementedError("abstract")
  255. def repr_failure(self, excinfo):
  256. """ represent a collection failure. """
  257. if excinfo.errisinstance(self.CollectError):
  258. exc = excinfo.value
  259. return str(exc.args[0])
  260. # Respect explicit tbstyle option, but default to "short"
  261. # (None._repr_failure_py defaults to "long" without "fulltrace" option).
  262. tbstyle = self.config.getoption("tbstyle", "auto")
  263. if tbstyle == "auto":
  264. tbstyle = "short"
  265. return self._repr_failure_py(excinfo, style=tbstyle)
  266. def _prunetraceback(self, excinfo):
  267. if hasattr(self, "fspath"):
  268. traceback = excinfo.traceback
  269. ntraceback = traceback.cut(path=self.fspath)
  270. if ntraceback == traceback:
  271. ntraceback = ntraceback.cut(excludepath=tracebackcutdir)
  272. excinfo.traceback = ntraceback.filter()
  273. def _check_initialpaths_for_relpath(session, fspath):
  274. for initial_path in session._initialpaths:
  275. if fspath.common(initial_path) == initial_path:
  276. return fspath.relto(initial_path)
  277. class FSCollector(Collector):
  278. def __init__(self, fspath, parent=None, config=None, session=None, nodeid=None):
  279. fspath = py.path.local(fspath) # xxx only for test_resultlog.py?
  280. name = fspath.basename
  281. if parent is not None:
  282. rel = fspath.relto(parent.fspath)
  283. if rel:
  284. name = rel
  285. name = name.replace(os.sep, SEP)
  286. self.fspath = fspath
  287. session = session or parent.session
  288. if nodeid is None:
  289. nodeid = self.fspath.relto(session.config.rootdir)
  290. if not nodeid:
  291. nodeid = _check_initialpaths_for_relpath(session, fspath)
  292. if nodeid and os.sep != SEP:
  293. nodeid = nodeid.replace(os.sep, SEP)
  294. super(FSCollector, self).__init__(
  295. name, parent, config, session, nodeid=nodeid, fspath=fspath
  296. )
  297. class File(FSCollector):
  298. """ base class for collecting tests from a file. """
  299. class Item(Node):
  300. """ a basic test invocation item. Note that for a single function
  301. there might be multiple test invocation items.
  302. """
  303. nextitem = None
  304. def __init__(self, name, parent=None, config=None, session=None, nodeid=None):
  305. super(Item, self).__init__(name, parent, config, session, nodeid=nodeid)
  306. self._report_sections = []
  307. #: user properties is a list of tuples (name, value) that holds user
  308. #: defined properties for this test.
  309. self.user_properties = []
  310. def add_report_section(self, when, key, content):
  311. """
  312. Adds a new report section, similar to what's done internally to add stdout and
  313. stderr captured output::
  314. item.add_report_section("call", "stdout", "report section contents")
  315. :param str when:
  316. One of the possible capture states, ``"setup"``, ``"call"``, ``"teardown"``.
  317. :param str key:
  318. Name of the section, can be customized at will. Pytest uses ``"stdout"`` and
  319. ``"stderr"`` internally.
  320. :param str content:
  321. The full contents as a string.
  322. """
  323. if content:
  324. self._report_sections.append((when, key, content))
  325. def reportinfo(self):
  326. return self.fspath, None, ""
  327. @property
  328. def location(self):
  329. try:
  330. return self._location
  331. except AttributeError:
  332. location = self.reportinfo()
  333. fspath = self.session._node_location_to_relpath(location[0])
  334. location = (fspath, location[1], str(location[2]))
  335. self._location = location
  336. return location