14

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?

Charles
  • 50,943
  • 13
  • 104
  • 142
Elias Dorneles
  • 22,556
  • 11
  • 85
  • 107

3 Answers3

9

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__'.

Lukas Graf
  • 30,317
  • 8
  • 77
  • 92
  • And what about the postmorten debug ? I did use to call the program like `pudb myprogram params` and it stops in the first error. Can we do the same with `pudb` and `unittests` ? I mean stop at the first error and get the postmorten debug ? – yucer Apr 19 '17 at 09:46
  • @yucer that will depend of your test runner. In case you use pytest, you can install this plugin https://pypi.org/project/pytest-pudb/ and then run `pytest --pudb` – Elias Dorneles Oct 15 '18 at 11:17
2

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.

Elias Dorneles
  • 22,556
  • 11
  • 85
  • 107
  • the question is about unittest not pytest – G M Nov 16 '22 at 12:46
  • @GM I think I know that, it was me who asked the question. Read again my answer, it's not specific to pytest. – Elias Dorneles Nov 17 '22 at 10:25
  • Regardless of the fact that you asked the question, I didn't find an answer to your question in your answer :-). So I have upvoted your question but downvoted your answer which is more comment in my opinion. – G M Nov 17 '22 at 11:45
  • My question was "how to debug unit tests with pudb", my answer is right on topic. Question for you: do you believe my answer is not useful? Because clearly at least two other people found it useful, probably because they had the same problem I had, which made me come back to add this answer years after i had originally wrote the question. – Elias Dorneles Nov 17 '22 at 12:46
2

You can set a breakpoint even easier by:

import pudb; pu.db

Sławomir Lenart
  • 7,543
  • 4
  • 45
  • 61