__init__.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # -*- coding: utf-8 -*-
  2. """
  3. support for presenting detailed information in failing assertions.
  4. """
  5. from __future__ import absolute_import
  6. from __future__ import division
  7. from __future__ import print_function
  8. import sys
  9. import six
  10. from _pytest.assertion import rewrite
  11. from _pytest.assertion import truncate
  12. from _pytest.assertion import util
  13. def pytest_addoption(parser):
  14. group = parser.getgroup("debugconfig")
  15. group.addoption(
  16. "--assert",
  17. action="store",
  18. dest="assertmode",
  19. choices=("rewrite", "plain"),
  20. default="rewrite",
  21. metavar="MODE",
  22. help="""Control assertion debugging tools. 'plain'
  23. performs no assertion debugging. 'rewrite'
  24. (the default) rewrites assert statements in
  25. test modules on import to provide assert
  26. expression information.""",
  27. )
  28. def register_assert_rewrite(*names):
  29. """Register one or more module names to be rewritten on import.
  30. This function will make sure that this module or all modules inside
  31. the package will get their assert statements rewritten.
  32. Thus you should make sure to call this before the module is
  33. actually imported, usually in your __init__.py if you are a plugin
  34. using a package.
  35. :raise TypeError: if the given module names are not strings.
  36. """
  37. for name in names:
  38. if not isinstance(name, str):
  39. msg = "expected module names as *args, got {0} instead"
  40. raise TypeError(msg.format(repr(names)))
  41. for hook in sys.meta_path:
  42. if isinstance(hook, rewrite.AssertionRewritingHook):
  43. importhook = hook
  44. break
  45. else:
  46. importhook = DummyRewriteHook()
  47. importhook.mark_rewrite(*names)
  48. class DummyRewriteHook(object):
  49. """A no-op import hook for when rewriting is disabled."""
  50. def mark_rewrite(self, *names):
  51. pass
  52. class AssertionState(object):
  53. """State for the assertion plugin."""
  54. def __init__(self, config, mode):
  55. self.mode = mode
  56. self.trace = config.trace.root.get("assertion")
  57. self.hook = None
  58. def install_importhook(config):
  59. """Try to install the rewrite hook, raise SystemError if it fails."""
  60. # Jython has an AST bug that make the assertion rewriting hook malfunction.
  61. if sys.platform.startswith("java"):
  62. raise SystemError("rewrite not supported")
  63. config._assertstate = AssertionState(config, "rewrite")
  64. config._assertstate.hook = hook = rewrite.AssertionRewritingHook(config)
  65. sys.meta_path.insert(0, hook)
  66. config._assertstate.trace("installed rewrite import hook")
  67. def undo():
  68. hook = config._assertstate.hook
  69. if hook is not None and hook in sys.meta_path:
  70. sys.meta_path.remove(hook)
  71. config.add_cleanup(undo)
  72. return hook
  73. def pytest_collection(session):
  74. # this hook is only called when test modules are collected
  75. # so for example not in the master process of pytest-xdist
  76. # (which does not collect test modules)
  77. assertstate = getattr(session.config, "_assertstate", None)
  78. if assertstate:
  79. if assertstate.hook is not None:
  80. assertstate.hook.set_session(session)
  81. def pytest_runtest_setup(item):
  82. """Setup the pytest_assertrepr_compare hook
  83. The newinterpret and rewrite modules will use util._reprcompare if
  84. it exists to use custom reporting via the
  85. pytest_assertrepr_compare hook. This sets up this custom
  86. comparison for the test.
  87. """
  88. def callbinrepr(op, left, right):
  89. """Call the pytest_assertrepr_compare hook and prepare the result
  90. This uses the first result from the hook and then ensures the
  91. following:
  92. * Overly verbose explanations are truncated unless configured otherwise
  93. (eg. if running in verbose mode).
  94. * Embedded newlines are escaped to help util.format_explanation()
  95. later.
  96. * If the rewrite mode is used embedded %-characters are replaced
  97. to protect later % formatting.
  98. The result can be formatted by util.format_explanation() for
  99. pretty printing.
  100. """
  101. hook_result = item.ihook.pytest_assertrepr_compare(
  102. config=item.config, op=op, left=left, right=right
  103. )
  104. for new_expl in hook_result:
  105. if new_expl:
  106. new_expl = truncate.truncate_if_required(new_expl, item)
  107. new_expl = [line.replace("\n", "\\n") for line in new_expl]
  108. res = six.text_type("\n~").join(new_expl)
  109. if item.config.getvalue("assertmode") == "rewrite":
  110. res = res.replace("%", "%%")
  111. return res
  112. util._reprcompare = callbinrepr
  113. def pytest_runtest_teardown(item):
  114. util._reprcompare = None
  115. def pytest_sessionfinish(session):
  116. assertstate = getattr(session.config, "_assertstate", None)
  117. if assertstate:
  118. if assertstate.hook is not None:
  119. assertstate.hook.set_session(None)
  120. # Expose this plugin's implementation for the pytest_assertrepr_compare hook
  121. pytest_assertrepr_compare = util.assertrepr_compare