failuredetail.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """
  2. This plugin provides assert introspection. When the plugin is enabled
  3. and a test failure occurs, the traceback is displayed with extra context
  4. around the line in which the exception was raised. Simple variable
  5. substitution is also performed in the context output to provide more
  6. debugging information.
  7. """
  8. from nose.plugins import Plugin
  9. from nose.pyversion import exc_to_unicode, force_unicode
  10. from nose.inspector import inspect_traceback
  11. class FailureDetail(Plugin):
  12. """
  13. Plugin that provides extra information in tracebacks of test failures.
  14. """
  15. score = 1600 # before capture
  16. def options(self, parser, env):
  17. """Register commmandline options.
  18. """
  19. parser.add_option(
  20. "-d", "--detailed-errors", "--failure-detail",
  21. action="store_true",
  22. default=env.get('NOSE_DETAILED_ERRORS'),
  23. dest="detailedErrors", help="Add detail to error"
  24. " output by attempting to evaluate failed"
  25. " asserts [NOSE_DETAILED_ERRORS]")
  26. def configure(self, options, conf):
  27. """Configure plugin.
  28. """
  29. if not self.can_configure:
  30. return
  31. self.enabled = options.detailedErrors
  32. self.conf = conf
  33. def formatFailure(self, test, err):
  34. """Add detail from traceback inspection to error message of a failure.
  35. """
  36. ec, ev, tb = err
  37. tbinfo, str_ev = None, exc_to_unicode(ev)
  38. if tb:
  39. tbinfo = force_unicode(inspect_traceback(tb))
  40. str_ev = '\n'.join([str_ev, tbinfo])
  41. test.tbinfo = tbinfo
  42. return (ec, str_ev, tb)