Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Unit Testing: Automatically Running the Debugger when a test fails

Is there a way to automatically start the debugger at the point at which a unittest fails?

Right now I am just using pdb.set_trace() manually, but this is very tedious as I need to add it each time and take it out at the end.

For Example:

import unittest  class tests(unittest.TestCase):      def setUp(self):         pass      def test_trigger_pdb(self):         #this is the way I do it now         try:             assert 1==0         except AssertionError:             import pdb             pdb.set_trace()      def test_no_trigger(self):         #this is the way I would like to do it:         a=1         b=2         assert a==b         #magically, pdb would start here         #so that I could inspect the values of a and b  if __name__=='__main__':     #In the documentation the unittest.TestCase has a debug() method     #but I don't understand how to use it     #A=tests()     #A.debug(A)      unittest.main() 
like image 752
tjb Avatar asked Dec 09 '10 13:12

tjb


People also ask

How do you debug a unit test case in python?

Debugging python unit test is a lot like debugging regular python code. The only difference is that you need to figure out how to run a individual test, then go and figure out where in the code you would put your set_trace() statement, and then it is normal debugging.

How does unit test work in python?

The unit test framework in Python is called unittest , which comes packaged with Python. Unit testing makes your code future proof since you anticipate the cases where your code could potentially fail or produce a bug. Though you cannot predict all of the cases, you still address most of them.

Which function in unit test will run all of your tests in python?

TestCase is used to create test cases by subclassing it. The last block of the code at the bottom allows us to run all the tests just by running the file.


2 Answers

I think what you are looking for is nose. It works like a test runner for unittest.

You can drop into the debugger on errors, with the following command:

nosetests --pdb 
like image 58
cmcginty Avatar answered Oct 14 '22 02:10

cmcginty


import unittest import sys import pdb import functools import traceback def debug_on(*exceptions):     if not exceptions:         exceptions = (AssertionError, )     def decorator(f):         @functools.wraps(f)         def wrapper(*args, **kwargs):             try:                 return f(*args, **kwargs)             except exceptions:                 info = sys.exc_info()                 traceback.print_exception(*info)                  pdb.post_mortem(info[2])         return wrapper     return decorator  class tests(unittest.TestCase):     @debug_on()     def test_trigger_pdb(self):         assert 1 == 0 

I corrected the code to call post_mortem on the exception instead of set_trace.

like image 25
Rosh Oxymoron Avatar answered Oct 14 '22 02:10

Rosh Oxymoron