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!

Ghopper21
  • 10,287
  • 10
  • 63
  • 92

3 Answers3

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.

Fedor Gogolev
  • 10,391
  • 4
  • 30
  • 36
  • 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? – Ghopper21 Aug 31 '12 at 01:03
  • It's not nice, but you can catch your exception in your code and run pdb. – Fedor Gogolev Aug 31 '12 at 01:04
  • FYI, I'm trying to figure out how to install django-nose, which looks great. It's giving me trouble so far... See http://stackoverflow.com/questions/12215520/how-to-get-django-nose-installed-correctly – Ghopper21 Aug 31 '12 at 12:36
  • @Ghopper21, answered in your question – Fedor Gogolev Aug 31 '12 at 12:56
  • What if I want to just let it bubble up and be caught with PyCharm? It doesn't allow use of pdb. – Lincoln B Jan 23 '13 at 21:11
  • 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. – Ghopper21 Apr 01 '15 at 13:48
3

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

Lincoln B
  • 2,184
  • 1
  • 13
  • 12
  • +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.) – Ghopper21 Aug 31 '12 at 13:06
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
Steve Barnes
  • 27,618
  • 6
  • 63
  • 73