0

This is a bit of a newbie question about basic python workflow. I have a library of python files, and a collection of associated unit tests. One of the tests is failing and I'd like to inject pdb at the spot of the failure.

My workflow uses elpy and emacs, which I otherwise quite like and would prefer to stick with. I load the test file in emacs, start a python shell, and then run the test code in the python shell. Something throws an exception, which causes the test to fail. The python process ends and prints a stack trace.

If the process were still alive I'd call pdb.pm() to start the debugger at the source of the error so that I could investigate the stack interactively, and that is what I'm trying to achieve. The basic structure of the unit test file is

import unittest
import other_stuff

class MyTest(unittest.TestCase):
    def test_other_stuff(self):
        self.assertTrue(other_stuff.function_call() == expected_result)

    def test_other_other_stuff(self):
        pass # you get the idea

if __name__ == "__main__":
    unittest.main(verbosity=2)

Steven Scott
  • 481
  • 3
  • 14
  • Hi! I think you shoul wrap the parts that you think may fail inside a try-except block. Here you have a very simple explanation: https://www.w3schools.com/python/python_try_except.asp – Kalma Jan 09 '20 at 18:58
  • I suspect that your test isn't actually failing but that your code is crashing in one of the functions that your tests are calling. In this case, I believe your program just fails and control doesn't come back to your test. Can you provide the text of the error message you're receiving? (BTW, the suggestion about try-except might help but, if you're not careful, it might prevent you from seeing where the error actually is). – Ben Jan 09 '20 at 19:04
  • One thing worth trying: Use `pytest` instead of plain `unittest`, and run it with the `--pdb` option. Then it'll automatically drop into the debugger on a failure. – cadolphs Jan 09 '20 at 19:11
  • 1
    Does this answer your question? [Python Unit Testing: Automatically Running the Debugger when a test fails](https://stackoverflow.com/questions/4398967/python-unit-testing-automatically-running-the-debugger-when-a-test-fails) – Tadeusz Sznuk Jan 09 '20 at 19:14

0 Answers0