reports.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. # -*- coding: utf-8 -*-
  2. from pprint import pprint
  3. import py
  4. import six
  5. from _pytest._code.code import ExceptionInfo
  6. from _pytest._code.code import ReprEntry
  7. from _pytest._code.code import ReprEntryNative
  8. from _pytest._code.code import ReprExceptionInfo
  9. from _pytest._code.code import ReprFileLocation
  10. from _pytest._code.code import ReprFuncArgs
  11. from _pytest._code.code import ReprLocals
  12. from _pytest._code.code import ReprTraceback
  13. from _pytest._code.code import TerminalRepr
  14. from _pytest.outcomes import skip
  15. from _pytest.pathlib import Path
  16. def getslaveinfoline(node):
  17. try:
  18. return node._slaveinfocache
  19. except AttributeError:
  20. d = node.slaveinfo
  21. ver = "%s.%s.%s" % d["version_info"][:3]
  22. node._slaveinfocache = s = "[%s] %s -- Python %s %s" % (
  23. d["id"],
  24. d["sysplatform"],
  25. ver,
  26. d["executable"],
  27. )
  28. return s
  29. class BaseReport(object):
  30. when = None
  31. location = None
  32. def __init__(self, **kw):
  33. self.__dict__.update(kw)
  34. def toterminal(self, out):
  35. if hasattr(self, "node"):
  36. out.line(getslaveinfoline(self.node))
  37. longrepr = self.longrepr
  38. if longrepr is None:
  39. return
  40. if hasattr(longrepr, "toterminal"):
  41. longrepr.toterminal(out)
  42. else:
  43. try:
  44. out.line(longrepr)
  45. except UnicodeEncodeError:
  46. out.line("<unprintable longrepr>")
  47. def get_sections(self, prefix):
  48. for name, content in self.sections:
  49. if name.startswith(prefix):
  50. yield prefix, content
  51. @property
  52. def longreprtext(self):
  53. """
  54. Read-only property that returns the full string representation
  55. of ``longrepr``.
  56. .. versionadded:: 3.0
  57. """
  58. tw = py.io.TerminalWriter(stringio=True)
  59. tw.hasmarkup = False
  60. self.toterminal(tw)
  61. exc = tw.stringio.getvalue()
  62. return exc.strip()
  63. @property
  64. def caplog(self):
  65. """Return captured log lines, if log capturing is enabled
  66. .. versionadded:: 3.5
  67. """
  68. return "\n".join(
  69. content for (prefix, content) in self.get_sections("Captured log")
  70. )
  71. @property
  72. def capstdout(self):
  73. """Return captured text from stdout, if capturing is enabled
  74. .. versionadded:: 3.0
  75. """
  76. return "".join(
  77. content for (prefix, content) in self.get_sections("Captured stdout")
  78. )
  79. @property
  80. def capstderr(self):
  81. """Return captured text from stderr, if capturing is enabled
  82. .. versionadded:: 3.0
  83. """
  84. return "".join(
  85. content for (prefix, content) in self.get_sections("Captured stderr")
  86. )
  87. passed = property(lambda x: x.outcome == "passed")
  88. failed = property(lambda x: x.outcome == "failed")
  89. skipped = property(lambda x: x.outcome == "skipped")
  90. @property
  91. def fspath(self):
  92. return self.nodeid.split("::")[0]
  93. @property
  94. def count_towards_summary(self):
  95. """
  96. **Experimental**
  97. Returns True if this report should be counted towards the totals shown at the end of the
  98. test session: "1 passed, 1 failure, etc".
  99. .. note::
  100. This function is considered **experimental**, so beware that it is subject to changes
  101. even in patch releases.
  102. """
  103. return True
  104. @property
  105. def head_line(self):
  106. """
  107. **Experimental**
  108. Returns the head line shown with longrepr output for this report, more commonly during
  109. traceback representation during failures::
  110. ________ Test.foo ________
  111. In the example above, the head_line is "Test.foo".
  112. .. note::
  113. This function is considered **experimental**, so beware that it is subject to changes
  114. even in patch releases.
  115. """
  116. if self.location is not None:
  117. fspath, lineno, domain = self.location
  118. return domain
  119. def _get_verbose_word(self, config):
  120. _category, _short, verbose = config.hook.pytest_report_teststatus(
  121. report=self, config=config
  122. )
  123. return verbose
  124. def _to_json(self):
  125. """
  126. This was originally the serialize_report() function from xdist (ca03269).
  127. Returns the contents of this report as a dict of builtin entries, suitable for
  128. serialization.
  129. Experimental method.
  130. """
  131. def disassembled_report(rep):
  132. reprtraceback = rep.longrepr.reprtraceback.__dict__.copy()
  133. reprcrash = rep.longrepr.reprcrash.__dict__.copy()
  134. new_entries = []
  135. for entry in reprtraceback["reprentries"]:
  136. entry_data = {
  137. "type": type(entry).__name__,
  138. "data": entry.__dict__.copy(),
  139. }
  140. for key, value in entry_data["data"].items():
  141. if hasattr(value, "__dict__"):
  142. entry_data["data"][key] = value.__dict__.copy()
  143. new_entries.append(entry_data)
  144. reprtraceback["reprentries"] = new_entries
  145. return {
  146. "reprcrash": reprcrash,
  147. "reprtraceback": reprtraceback,
  148. "sections": rep.longrepr.sections,
  149. }
  150. d = self.__dict__.copy()
  151. if hasattr(self.longrepr, "toterminal"):
  152. if hasattr(self.longrepr, "reprtraceback") and hasattr(
  153. self.longrepr, "reprcrash"
  154. ):
  155. d["longrepr"] = disassembled_report(self)
  156. else:
  157. d["longrepr"] = six.text_type(self.longrepr)
  158. else:
  159. d["longrepr"] = self.longrepr
  160. for name in d:
  161. if isinstance(d[name], (py.path.local, Path)):
  162. d[name] = str(d[name])
  163. elif name == "result":
  164. d[name] = None # for now
  165. return d
  166. @classmethod
  167. def _from_json(cls, reportdict):
  168. """
  169. This was originally the serialize_report() function from xdist (ca03269).
  170. Factory method that returns either a TestReport or CollectReport, depending on the calling
  171. class. It's the callers responsibility to know which class to pass here.
  172. Experimental method.
  173. """
  174. if reportdict["longrepr"]:
  175. if (
  176. "reprcrash" in reportdict["longrepr"]
  177. and "reprtraceback" in reportdict["longrepr"]
  178. ):
  179. reprtraceback = reportdict["longrepr"]["reprtraceback"]
  180. reprcrash = reportdict["longrepr"]["reprcrash"]
  181. unserialized_entries = []
  182. reprentry = None
  183. for entry_data in reprtraceback["reprentries"]:
  184. data = entry_data["data"]
  185. entry_type = entry_data["type"]
  186. if entry_type == "ReprEntry":
  187. reprfuncargs = None
  188. reprfileloc = None
  189. reprlocals = None
  190. if data["reprfuncargs"]:
  191. reprfuncargs = ReprFuncArgs(**data["reprfuncargs"])
  192. if data["reprfileloc"]:
  193. reprfileloc = ReprFileLocation(**data["reprfileloc"])
  194. if data["reprlocals"]:
  195. reprlocals = ReprLocals(data["reprlocals"]["lines"])
  196. reprentry = ReprEntry(
  197. lines=data["lines"],
  198. reprfuncargs=reprfuncargs,
  199. reprlocals=reprlocals,
  200. filelocrepr=reprfileloc,
  201. style=data["style"],
  202. )
  203. elif entry_type == "ReprEntryNative":
  204. reprentry = ReprEntryNative(data["lines"])
  205. else:
  206. _report_unserialization_failure(entry_type, cls, reportdict)
  207. unserialized_entries.append(reprentry)
  208. reprtraceback["reprentries"] = unserialized_entries
  209. exception_info = ReprExceptionInfo(
  210. reprtraceback=ReprTraceback(**reprtraceback),
  211. reprcrash=ReprFileLocation(**reprcrash),
  212. )
  213. for section in reportdict["longrepr"]["sections"]:
  214. exception_info.addsection(*section)
  215. reportdict["longrepr"] = exception_info
  216. return cls(**reportdict)
  217. def _report_unserialization_failure(type_name, report_class, reportdict):
  218. url = "https://github.com/pytest-dev/pytest/issues"
  219. stream = py.io.TextIO()
  220. pprint("-" * 100, stream=stream)
  221. pprint("INTERNALERROR: Unknown entry type returned: %s" % type_name, stream=stream)
  222. pprint("report_name: %s" % report_class, stream=stream)
  223. pprint(reportdict, stream=stream)
  224. pprint("Please report this bug at %s" % url, stream=stream)
  225. pprint("-" * 100, stream=stream)
  226. raise RuntimeError(stream.getvalue())
  227. class TestReport(BaseReport):
  228. """ Basic test report object (also used for setup and teardown calls if
  229. they fail).
  230. """
  231. __test__ = False
  232. def __init__(
  233. self,
  234. nodeid,
  235. location,
  236. keywords,
  237. outcome,
  238. longrepr,
  239. when,
  240. sections=(),
  241. duration=0,
  242. user_properties=None,
  243. **extra
  244. ):
  245. #: normalized collection node id
  246. self.nodeid = nodeid
  247. #: a (filesystempath, lineno, domaininfo) tuple indicating the
  248. #: actual location of a test item - it might be different from the
  249. #: collected one e.g. if a method is inherited from a different module.
  250. self.location = location
  251. #: a name -> value dictionary containing all keywords and
  252. #: markers associated with a test invocation.
  253. self.keywords = keywords
  254. #: test outcome, always one of "passed", "failed", "skipped".
  255. self.outcome = outcome
  256. #: None or a failure representation.
  257. self.longrepr = longrepr
  258. #: one of 'setup', 'call', 'teardown' to indicate runtest phase.
  259. self.when = when
  260. #: user properties is a list of tuples (name, value) that holds user
  261. #: defined properties of the test
  262. self.user_properties = list(user_properties or [])
  263. #: list of pairs ``(str, str)`` of extra information which needs to
  264. #: marshallable. Used by pytest to add captured text
  265. #: from ``stdout`` and ``stderr``, but may be used by other plugins
  266. #: to add arbitrary information to reports.
  267. self.sections = list(sections)
  268. #: time it took to run just the test
  269. self.duration = duration
  270. self.__dict__.update(extra)
  271. def __repr__(self):
  272. return "<%s %r when=%r outcome=%r>" % (
  273. self.__class__.__name__,
  274. self.nodeid,
  275. self.when,
  276. self.outcome,
  277. )
  278. @classmethod
  279. def from_item_and_call(cls, item, call):
  280. """
  281. Factory method to create and fill a TestReport with standard item and call info.
  282. """
  283. when = call.when
  284. duration = call.stop - call.start
  285. keywords = {x: 1 for x in item.keywords}
  286. excinfo = call.excinfo
  287. sections = []
  288. if not call.excinfo:
  289. outcome = "passed"
  290. longrepr = None
  291. else:
  292. if not isinstance(excinfo, ExceptionInfo):
  293. outcome = "failed"
  294. longrepr = excinfo
  295. elif excinfo.errisinstance(skip.Exception):
  296. outcome = "skipped"
  297. r = excinfo._getreprcrash()
  298. longrepr = (str(r.path), r.lineno, r.message)
  299. else:
  300. outcome = "failed"
  301. if call.when == "call":
  302. longrepr = item.repr_failure(excinfo)
  303. else: # exception in setup or teardown
  304. longrepr = item._repr_failure_py(
  305. excinfo, style=item.config.getoption("tbstyle", "auto")
  306. )
  307. for rwhen, key, content in item._report_sections:
  308. sections.append(("Captured %s %s" % (key, rwhen), content))
  309. return cls(
  310. item.nodeid,
  311. item.location,
  312. keywords,
  313. outcome,
  314. longrepr,
  315. when,
  316. sections,
  317. duration,
  318. user_properties=item.user_properties,
  319. )
  320. class CollectReport(BaseReport):
  321. when = "collect"
  322. def __init__(self, nodeid, outcome, longrepr, result, sections=(), **extra):
  323. self.nodeid = nodeid
  324. self.outcome = outcome
  325. self.longrepr = longrepr
  326. self.result = result or []
  327. self.sections = list(sections)
  328. self.__dict__.update(extra)
  329. @property
  330. def location(self):
  331. return (self.fspath, None, self.fspath)
  332. def __repr__(self):
  333. return "<CollectReport %r lenresult=%s outcome=%r>" % (
  334. self.nodeid,
  335. len(self.result),
  336. self.outcome,
  337. )
  338. class CollectErrorRepr(TerminalRepr):
  339. def __init__(self, msg):
  340. self.longrepr = msg
  341. def toterminal(self, out):
  342. out.line(self.longrepr, red=True)
  343. def pytest_report_to_serializable(report):
  344. if isinstance(report, (TestReport, CollectReport)):
  345. data = report._to_json()
  346. data["_report_type"] = report.__class__.__name__
  347. return data
  348. def pytest_report_from_serializable(data):
  349. if "_report_type" in data:
  350. if data["_report_type"] == "TestReport":
  351. return TestReport._from_json(data)
  352. elif data["_report_type"] == "CollectReport":
  353. return CollectReport._from_json(data)
  354. assert False, "Unknown report_type unserialize data: {}".format(
  355. data["_report_type"]
  356. )