The Django 1.4 documentation on tests states:
For a given Django application, the test runner looks for doctests in two places:
The
models.py
file. You can define module-level doctests and/or a doctest for individual models. It's common practice to put application-level doctests in the module docstring and model-level doctests in the model docstrings.A file called
tests.py
in the application directory -- i.e., the directory that holds models.py. This file is a hook for any and all doctests you want to write that aren't necessarily related to models.
Out of curiosity I'd like to know why Django's testrunner is limited to the doctests in models.py
, but more practically I'd like to know how one could expand the testrunner's doctests to include (for example) views.py
and other modules when running manage.py test
.
I'd be grateful for any input.
Thank you.
Brian
I posted a github gist that lets you run test in any file or module in your project. Running doctests from specific modules and files
Use nosetests with plugin for django (django-sane-testing or django-nose) and use the --with-doctest flag.
This is my
tests/__init__.py
implementation, based on Jesse Shieh answer:Basically, this solution allows adding arbitrary "files" (actually, modules) to the test suite. It allows splitting the unit tests into separate files, and allows adding any module that contains doctests. Just add the module names to the appropriate list at the top of this file.
You can do this by adding/editing the suite() function in tests.py which defines what tests will be run by the django test runner.
Then just run your tests as usual and you should see your doctests in views.py run.
This is described in more detail in the django testing documentation
However, keep in mind that constructing your own test suite means that the django test runner will not automatically run any tests you have in tests.py. You'll have to add these into your suite manually, for example
Things have changed in Django 1.6:
So all I had to do myself was (in my_app/tests.py):
You could try to write your own
testrunner
and see, if you can include other files to be checked for doc tests.http://docs.djangoproject.com/en/dev/topics/testing/#defining-a-test-runner