Debugging django/unittest?

北城余情 提交于 2019-12-23 13:05:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!