Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Python's unittest to not catch exceptions?

I'm working on a Django project but I think this is a pure Python unittest question.

Normally, when you run tests, exceptions will be caught by the test runner and handled accordingly.

For debugging purposes, I want to disable this behavior, i.e. so that:

python -i manage.py test

will break into the interactive Python shell on an exception, as normal.

How to do that?

EDIT: based on the answers so far, it seems like this is more of a Django-specific question than I realized!

like image 590
Ghopper21 Avatar asked Aug 31 '12 00:08

Ghopper21


People also ask

How do you handle exceptions in unit test Python?

assertRaises(exception, callable, *args, **kwds) Test that an exception (first argument) is raised when a function is called with any positional or keyword arguments. The test passes if the expected exception is raised, is an error if another exception is raised, or fails if no exception is raised.

How do I skip a test in Unittest?

Alternate syntax for skipping test is using instance method skipTest() inside the test function.

Which item in Python will stop a unit test abruptly?

An exception object is created when a Python script raises an exception. If the script explicitly doesn't handle the exception, the program will be forced to terminate abruptly.

Is pytest better than Unittest?

Which is better – pytest or unittest? Although both the frameworks are great for performing testing in python, pytest is easier to work with. The code in pytest is simple, compact, and efficient. For unittest, we will have to import modules, create a class and define the testing functions within that class.


2 Answers

You can use django-nose test runner, it works with unittest tests, and run your tests like python manage.py test -v2 --pdb. And nose will run pdb for you.

like image 104
Fedor Gogolev Avatar answered Oct 04 '22 05:10

Fedor Gogolev


A new app django-pdb makes this nicer, supporting a mode for breaking on test failures or uncaught exceptions in regular code.

like image 20
Lincoln B Avatar answered Oct 04 '22 07:10

Lincoln B