简体   繁体   中英

How to debug unittests with pudb debugger?

I am having some trouble trying to debug some unit tests through the pudb debugger .

The tests run fine with python, but I had no luck runnign them with pudb .

I isolated the problem, getting to the following sample code:

class Math:
    def pow(self, x, y):
        return x ** y

import unittest

class MathTest(unittest.TestCase):
    def testPow23(self):
        self.assertEquals(8, Math().pow(2, 3))
    def testPow24(self):
        self.assertEquals(16, Math().pow(2, 4))

if __name__ == '__main__':
    unittest.main()

The tests run fine:

$ python amodule.py 
.
----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK

But if running through pudb, it gives me the output:

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

I've tried running using pudb amodule.py and also with python -m pudb.run amodule.py , but it makes no difference -- no tests are run in one or another way.

Should I be doing something different to debug unit tests using pudb?

Try placing a breakpoint on a useful line in your code:

from pudb import set_trace; set_trace()

The ways you tried to launch it might interfere with test discovery and/or not run your script with a __name__ of '__main__' .

Since this is a popular question, I feel I should also mention that most test running tools will require you to pass in a switch to prevent it from capturing the standard output and input (usually it's -s ).

So, remember to run pytest -s when using Pytest, or nosetests -s for Nose, python manage.py test -s for Django tests, or check the documentation for your test running tool.

您可以通过以下方式更轻松地设置断点:

import pudb; pu.db

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM