| 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 | |
|---|
| 9 | from nose.plugins import Plugin |
|---|
| 10 | from nose.inspector import inspect_traceback |
|---|
| 11 | |
|---|
| 12 | class FailureDetail(Plugin): |
|---|
| 13 | """ |
|---|
| 14 | Plugin that provides extra information in tracebacks of test failures. |
|---|
| 15 | """ |
|---|
| 16 | score = 600 # before capture |
|---|
| 17 | |
|---|
| 18 | def options(self, parser, env): |
|---|
| 19 | """Register commmandline options. |
|---|
| 20 | """ |
|---|
| 21 | parser.add_option( |
|---|
| 22 | "-d", "--detailed-errors", "--failure-detail", |
|---|
| 23 | action="store_true", |
|---|
| 24 | default=env.get('NOSE_DETAILED_ERRORS'), |
|---|
| 25 | dest="detailedErrors", help="Add detail to error" |
|---|
| 26 | " output by attempting to evaluate failed" |
|---|
| 27 | " asserts [NOSE_DETAILED_ERRORS]") |
|---|
| 28 | |
|---|
| 29 | def configure(self, options, conf): |
|---|
| 30 | """Configure plugin. |
|---|
| 31 | """ |
|---|
| 32 | if not self.can_configure: |
|---|
| 33 | return |
|---|
| 34 | self.enabled = options.detailedErrors |
|---|
| 35 | self.conf = conf |
|---|
| 36 | |
|---|
| 37 | def formatFailure(self, test, err): |
|---|
| 38 | """Add detail from traceback inspection to error message of a failure. |
|---|
| 39 | """ |
|---|
| 40 | ec, ev, tb = err |
|---|
| 41 | tbinfo = inspect_traceback(tb) |
|---|
| 42 | test.tbinfo = tbinfo |
|---|
| 43 | return (ec, '\n'.join([str(ev), tbinfo]), tb) |
|---|