nodes.py 14 KB

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