Bri*_*ant 21 django debugging django-testing pdb
我想像调试任何其他Python代码一样调试Django TestCase:只需调用pdb.set_trace()
然后进入交互式会话即可.当我这样做时,我没有看到任何东西,因为测试是在不同的过程中运行的.我正在使用django-discover-runner,但我的猜测是这适用于默认的Django测试运行器.
是否可以在每次错误/失败pdb
时使用django-discover-runner
a)进入会话,和/或b)只有在我调用pdb.set_trace()
我的测试代码时?
这个答案解释了Django创建了另一个进程,并建议使用rpdb2 debugger
一部分调用winpdb
,但我不想使用winpdb
,我宁愿使用ipdb
.
这个答案django-nose
通过运行如下所示的测试命令解决了问题:./manage.py test -- -s
但该选项不可用django-discover-runner
.
这个答案显示我可以这样做ipython
:
In [9]: %pdb
Automatic pdb calling has been turned ON
Run Code Online (Sandbox Code Playgroud)
这似乎是一个潜在的选择,但ipython
每次运行测试时启动它似乎有点麻烦.
最后,这个答案表明nose
带有一个--pdb
标志,它会pdb
出现错误,这就是我想要的.我唯一的选择是切换到django-nose
测试跑步者?
我在内置帮助中没有看到任何选项django-discover-runner
:
$ python manage.py help test --settings=settings.test
Usage: manage.py test [options] [appname ...]
Runs the test suite for the specified applications, or the entire site if no apps are specified.
Options:
-v VERBOSITY, --verbosity=VERBOSITY
Verbosity level; 0=minimal output, 1=normal output,
2=verbose output, 3=very verbose output
--settings=SETTINGS The Python path to a settings module, e.g.
"myproject.settings.main". If this isn't provided, the
DJANGO_SETTINGS_MODULE environment variable will be
used.
--pythonpath=PYTHONPATH
A directory to add to the Python path, e.g.
"/home/djangoprojects/myproject".
--traceback Print traceback on exception
--noinput Tells Django to NOT prompt the user for input of any
kind.
--failfast Tells Django to stop running the test suite after
first failed test.
--testrunner=TESTRUNNER
Tells Django to use specified test runner class
instead of the one specified by the TEST_RUNNER
setting.
--liveserver=LIVESERVER
Overrides the default address where the live server
(used with LiveServerTestCase) is expected to run
from. The default value is localhost:8081.
-t TOP_LEVEL, --top-level-directory=TOP_LEVEL
Top level of project for unittest discovery.
-p PATTERN, --pattern=PATTERN
The test matching pattern. Defaults to test*.py.
--version show program's version number and exit
-h, --help show this help message and exit
Run Code Online (Sandbox Code Playgroud)
Car*_*yer 20
Django不会在单独的进程中运行测试; 声称它确实存在的相关答案完全是错误的.(最接近的是LiveServerTestCase
Selenium测试,它启动一个单独的线程来运行开发服务器,但这仍然不是一个单独的过程,并且它不会阻止使用pdb).您应该能够import pdb; pdb.set_trace()
在测试中(或在测试代码中)的任何位置插入并获得可用的pdb提示.我从未遇到过这方面的麻烦,我刚刚在Django 1.5.1和django-discover-runner 1.0的新项目中再次验证了它.如果这不适合你,那是因为项目中的其他内容,而不是由于Django或django-discover-runner.
Nose默认捕获所有输出,中断import pdb; pdb.set_trace()
.该-s
选项关闭输出捕获.对于股票Django测试运行器或django-discover-runner来说,这不是必需的,因为他们都没有开始输出捕获.
--pdb
如果您正在使用django-discover-runner,我不知道有任何等同于nose的选项.有一个django-pdb项目提供了这个,但是快速浏览它的代码告诉我它不能很好地与django-discover-runner合作; 但是它的代码可能会为你自己实现这个提供一些线索.
FWIW,我个人使用py.test和pytest-django而不是django-discover-runner或django-nose.即使py.test提供--pdb
像鼻子这样的选项,我也不会使用它; 为了在错误之前逐步执行,我经常想要比实际的错误点更早打破,所以我通常只是插入import pytest; pytest.set_trace()
(set_trace
从中导入pytest
相当于nose的-s
选项;它在运行pdb之前关闭py.test的输出捕获)我希望它在代码中,然后在我完成后删除它.我觉得这很麻烦; 因人而异.
Gle*_*leb 11
尝试使用ipdb而不是pdb -
import ipdb;ipdb.set_trace()
Run Code Online (Sandbox Code Playgroud)
或(在鼻子测试跑步者的情况下工作)
from nose.tools import set_trace;set_trace()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9350 次 |
最近记录: |