1 | """ |
---|
2 | This plugin provides ``--pdb`` and ``--pdb-failures`` options. The ``--pdb`` |
---|
3 | option will drop the test runner into pdb when it encounters an error. To |
---|
4 | drop into pdb on failure, use ``--pdb-failures``. |
---|
5 | """ |
---|
6 | |
---|
7 | import pdb |
---|
8 | from nose.plugins.base import Plugin |
---|
9 | |
---|
10 | class Pdb(Plugin): |
---|
11 | """ |
---|
12 | Provides --pdb and --pdb-failures options that cause the test runner to |
---|
13 | drop into pdb if it encounters an error or failure, respectively. |
---|
14 | """ |
---|
15 | enabled_for_errors = False |
---|
16 | enabled_for_failures = False |
---|
17 | score = 5 # run last, among builtins |
---|
18 | |
---|
19 | def options(self, parser, env): |
---|
20 | """Register commandline options. |
---|
21 | """ |
---|
22 | parser.add_option( |
---|
23 | "--pdb", action="store_true", dest="debugErrors", |
---|
24 | default=env.get('NOSE_PDB', False), |
---|
25 | help="Drop into debugger on errors") |
---|
26 | parser.add_option( |
---|
27 | "--pdb-failures", action="store_true", |
---|
28 | dest="debugFailures", |
---|
29 | default=env.get('NOSE_PDB_FAILURES', False), |
---|
30 | help="Drop into debugger on failures") |
---|
31 | |
---|
32 | def configure(self, options, conf): |
---|
33 | """Configure which kinds of exceptions trigger plugin. |
---|
34 | """ |
---|
35 | self.conf = conf |
---|
36 | self.enabled = options.debugErrors or options.debugFailures |
---|
37 | self.enabled_for_errors = options.debugErrors |
---|
38 | self.enabled_for_failures = options.debugFailures |
---|
39 | |
---|
40 | def addError(self, test, err): |
---|
41 | """Enter pdb if configured to debug errors. |
---|
42 | """ |
---|
43 | if not self.enabled_for_errors: |
---|
44 | return |
---|
45 | self.debug(err) |
---|
46 | |
---|
47 | def addFailure(self, test, err): |
---|
48 | """Enter pdb if configured to debug failures. |
---|
49 | """ |
---|
50 | if not self.enabled_for_failures: |
---|
51 | return |
---|
52 | self.debug(err) |
---|
53 | |
---|
54 | def debug(self, err): |
---|
55 | import sys # FIXME why is this import here? |
---|
56 | ec, ev, tb = err |
---|
57 | stdout = sys.stdout |
---|
58 | sys.stdout = sys.__stdout__ |
---|
59 | try: |
---|
60 | pdb.post_mortem(tb) |
---|
61 | finally: |
---|
62 | sys.stdout = stdout |
---|