ipdb with python unittest module
Asked Answered
N

3

12

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.

Nedanedda answered 26/7, 2015 at 18:17 Comment(1)
nosetests --pdb <test file> is the closest I am aware of but does not use ipdb. I add import ipdb; ipdb.set_trace() to force tests to drop into ipdb personally, though I'd love a cmd line way to do it also.Selfoperating
K
2

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.

Kingsley answered 20/2, 2017 at 16:29 Comment(0)
T
1

nose now has an ipdb plugin. You can install it via:

pip install ipdbplugin

Then test your program by,

nosetests --ipdb <test_file>
Triode answered 19/9, 2019 at 7:10 Comment(0)
P
-1

There is a %pdb flag you can set in ipython.It will invoke the debugger if any exception occurs.

Philae answered 6/9, 2016 at 6:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.