我想像调试任何其他 Python 代码一样调试 Django TestCase:只需调用pdb.set_trace()
,然后进入交互式会话。当我这样做时,我什么也看不到,因为测试是在不同的过程中运行的。我正在使用django-discover-runner,但我的猜测是这适用于默认的 Django 测试运行器。
问题:
是否可以在每次错误/失败pdb
时使用django-discover-runner
a) 和/或 b) 仅在我调用pdb.set_trace()
测试代码时才进入会话?
有些研究:
这个答案rpdb2 debugger
解释了 Django 创建了另一个进程,并建议使用对的一部分的调用winpdb
,但我不想使用winpdb
,我宁愿使用ipdb
.
此答案django-nose
通过运行如下测试命令解决了问题: ./manage.py test -- -s
,但该选项不适用于django-discover-runner
.
这个答案表明我可以这样做ipython
:
In [9]: %pdb
Automatic pdb calling has been turned ON
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