Debugging django/unittest?

2019-06-25 06:34发布

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.

4条回答
2楼-- · 2019-06-25 07:02

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.

查看更多
成全新的幸福
3楼-- · 2019-06-25 07:03

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).

查看更多
看我几分像从前
4楼-- · 2019-06-25 07:03

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

查看更多
ら.Afraid
5楼-- · 2019-06-25 07:20

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/

查看更多
登录 后发表回答