问题
I'm wondering if anybody has a hint on how to debug a unittest, or any other piece of code in django, for that matter, using a debugger like winpdb? I'm trying to to a
winpdb manage.py test photo
which runs my unittest for my photo app, but winpdb crashes. Are there alternatives? What is the best way to do this?
I'm running linux, ubuntu 10.10.
回答1:
You can use pdb to debug your program.
import pdb
def some_function():
pdb.set_trace()
some_other_computation()
When the program hits the set_trace method, execution will pause, and you will be put into an interactive shell. You can then examine variables, and step through your code.
回答2:
Look at pudb, it is a full-screen, console-based visual debugger for Python. Very nice for debugging with good console UI.
import pudb
def some_function():
pudb.set_trace()
some_other_computation()
You'll need to pass the -s option (eg: python manage.py test -s
), to turn off output capturing (which prevents the debugger from starting).
回答3:
Add following lines to your code:
import rpdb2;
rpdb2.start_embedded_debugger_interactive_password()
You can find more information here: http://winpdb.org/docs/embedded-debugging/
回答4:
The problem is that django creates another process in which it runs the application under test. So you can not just use winpdb on your main django process.
You should put a call to rpdb2 debugger (winpdb internal debugger) just before the place you want test and attach with winpdb to that running debugger.
See a tutorial here: https://code.djangoproject.com/wiki/DebuggingDjangoWithWinpdb
来源:https://stackoverflow.com/questions/5723656/debugging-django-unittest