سؤال

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.

You can set a breakpoint even easier by:

import pudb; pu.db

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top