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, ie 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!
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.
一个新的应用程序django-pdb使这更好,支持打破测试失败或常规代码中未捕获的异常的模式。
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.