Last active
September 5, 2017 17:48
-
-
Save jpeyret/ca2269d84e4dfeadf813821e64655d3e to your computer and use it in GitHub Desktop.
conditional debugging on unitttests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
import sys | |
import pdb | |
#################################### | |
def ppdb(e=None): | |
"""conditional debugging | |
use with: `if ppdb(): pdb.set_trace()` | |
""" | |
return ppdb.enabled | |
ppdb.enabled = False | |
################################### | |
class SomeTest(unittest.TestCase): | |
def test_success(self): | |
try: | |
pass | |
except Exception, e: | |
if ppdb(): pdb.set_trace() | |
raise | |
def test_fail(self): | |
try: | |
1/0 | |
except Exception, e: | |
if ppdb(): pdb.set_trace() | |
raise | |
if __name__ == '__main__': | |
#conditional debugging, but not in nosetests | |
if "--pdb" in sys.argv: | |
ppdb.enabled = not sys.argv[0].endswith("nosetests") | |
sys.argv.remove("--pdb") | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample usage:
Running the unit test normally:
which gives:
Request debugging:
which gives:
requesting debugging via nosetests
and, we stop, but not because of ppdb(), but just because of nosetests' builtin handling of
--pdb
flag.unittest sees nothing of the
--pdb
trick:output: