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

3

6

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!

Mishamishaan answered 31/8, 2012 at 0:49 Comment(0)
C
4

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.

Collette answered 31/8, 2012 at 1:0 Comment(6)
Thanks. I've heard nose mentioned a lot with various benefits such as this, and it's on my to-do list to learn, but for now, I'm hoping there is a way to do this without it. Do you know if it's definitely not possible with the standard test runner?Mishamishaan
It's not nice, but you can catch your exception in your code and run pdb.Collette
FYI, I'm trying to figure out how to install django-nose, which looks great. It's giving me trouble so far... See #12216020Mishamishaan
@Ghopper21, answered in your questionCollette
What if I want to just let it bubble up and be caught with PyCharm? It doesn't allow use of pdb.Denims
Apologies for only now accepting this answer. I never did get it working at the time. This morning I had another reason to use django-nose, and the installation worked on my current project (after a bit of fuss), so I was finally able to come back and test -- and accept -- your answer. Thanks again.Mishamishaan
D
3

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

Denims answered 31/8, 2012 at 4:19 Comment(1)
+1, this works nicely with the new manage.py test --pdb flag. I'm now trying to get django-nose installed so I can compare the two approaches. (Btw, I'll keep using django-pdb anyhow for it's other debugging enhancements.)Mishamishaan
R
0

You could try something like this in a module within 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's 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
Rhenium answered 26/2, 2014 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.