How can I make Python unittest not catch exceptions?

I am working on a Django project, but I think this is a pure Python question unittest.

Usually, when you run tests, exceptions will fall into the test runner and be handled accordingly.

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

python -i manage.py test

will break into the interactive shell of Python for an exception, as usual.

How to do it?

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

+5
source share
3 answers

django-nose test runner, unittest , python manage.py test -v2 --pdb. pdb .

+4
+3

You can try something like this in a module inside your package, then use CondCatches(your exceptions )in your code:

# System Imports
import os

class NoSuchException(Exception):
    """ Null Exception will not match any exception."""
    pass

def CondCatches(conditional, *args):
    """
    Depending on conditional either returns the arguments or NoSuchException.

    Use this to check have a caught exception that is suppressed some of the
    time. e.g.:
    from DisableableExcept import CondCatches
    import os
    try:
        # Something like:
        print "Do something bad!"
        print 23/0
    except CondCatches(os.getenv('DEBUG'), Exception), e:
        #handle the exception in non DEBUG
        print 'Somthing has a problem!', e
    """
    if conditional:
        return (NoSuchException, )
    else:
        return args

if __name__ == '__main__':
    # Do SOMETHING if file is called on it own.
    try:
        print 'To Suppress Catching this exception set DEBUG=anything'
        print 1 / 0
    except CondCatches(os.getenv('DEBUG'), ValueError, ZeroDivisionError), e:
        print "Caught Exception", e
0
source

All Articles