I want to debug a Django TestCase just like I would any other Python code: Simply call pdb.set_trace()
and then drop into an interactive session. When I do that, I don't see anything, as the tests are run in a different process. I'm using django-discover-runner, but my guess is that this applies to the default Django test runner.
The question:
Is it possible to drop into a pdb
session while using django-discover-runner
a) on every error / fail, AND/OR b) only when I call pdb.set_trace()
in my test code?
Some research:
This answer explains that Django creates another process, and suggests using a call to rpdb2 debugger
, a part of winpdb
, but I don't want to use winpdb
, I'd rather use ipdb
.
This answer solves the problem for django-nose
by running the test command like this: ./manage.py test -- -s
, but that option's not available for django-discover-runner
.
This answer shows that I can do this with ipython
:
In [9]: %pdb
Automatic pdb calling has been turned ON
That seems like a potential option, but it seems a bit cumbersome to fire up ipython
every time I run tests.
Finally, this answer shows that nose
comes with a --pdb
flag that drops into pdb
on errors, which is what I want. Is my only option to switch to the django-nose
test runner?
I don't see any options for this in the built-in help for 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