Is there any convenient way to get an ipdb debugger on an exception, when running tests with python's unittest module?
It's convenient to debug python code using ipython --pdb my_script.py
.
However, when I use the unittest module, with
class MyTestCase(unittest.TestCase):
def runTest(self):
x = 0
y = 3/x
unittest catches the exception and exits.
nose now has an ipdb plugin. You can install it via:
pip install ipdbplugin
Then test your program by,
nosetests --ipdb <test_file>
I find it helpful to run the tests first and see if any error occurs. This helps get a holistic view of the error. For example are there more than one test that are failing, and which one should be looked at first.
After analysing that, this is my approach to test/debug cycle. In your test:
def test_foo_is_bar(self):
import ipdb
ipdb.set_trace()
self.assertEqual('foo', 'bar')
Now run the test with:
nosetests -s tests/test_example.py
-s
flag will help your to get into input mode instead of getting the exception from nose.
Sidenote: I have shortcut set to paste import ipdb as pdb; pdb.set_trace()
in IntelliJ(PyCharm) settings, so that I can insert this one line to stop wherever I want in my code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With