24π
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.
import unittest
import doctest
from project import views
def suite():
suite = unittest.TestSuite()
suite.addTest(doctest.DocTestSuite(views))
return suite
Then just run your tests as usual and you should see your doctests in views.py run.
$ python manage.py test project
This is described in more detail in the django testing documentation
When you run your tests, the default behavior of the test utility is to find all the test cases (that is, subclasses of unittest.TestCase) in models.py and tests.py, automatically build a test suite out of those test cases, and run that suite.
There is a second way to define the test suite for a module: if you define a function called suite() in either models.py or tests.py, the Django test runner will use that function to construct the test suite for that module. This follows the suggested organization for unit tests. See the Python documentation for more details on how to construct a complex test suite.
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
import unittest
import doctest
from project import views
class FooTestCase(unittest.TestCase):
def testFoo(self):
self.assertEquals('foo', 'bar')
def suite():
suite = unittest.TestSuite()
suite.addTest(doctest.DocTestSuite(views))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(FooTestCase))
return suite
19π
Things have changed in Django 1.6:
Doctests will no longer be automatically discovered. To integrate
doctests in your test suite, follow the recommendations in the Python
documentation.
So all I had to do myself was (in my_app/tests.py):
import unittest
import doctest
from my_app import views
def load_tests(loader, tests, ignore):
tests.addTests(doctest.DocTestSuite(views))
return tests
- [Django]-How to use Django's assertJSONEqual to verify response of view returning JsonResponse
- [Django]-Filtering using viewsets in django rest framework
- [Django]-Django DB Settings 'Improperly Configured' Error
8π
This is my tests/__init__.py
implementation, based on Jesse Shieh answer:
import doctest
import unittest
list_of_doctests = [
'myapp.views.myview',
'myapp.forms.myform',
]
list_of_unittests = [
'sometestshere', # This file is myapp/tests/sometestshere.py
'moretestshere', # This file is myapp/tests/moretestshere.py
'myapp.tests.othertest', # Absolute paths also work.
]
def suite():
suite = unittest.TestSuite()
for t in list_of_doctests:
suite.addTest(doctest.DocTestSuite(
__import__(t, globals(), locals(), fromlist=["*"])
))
for t in list_of_unittests:
suite.addTest(unittest.TestLoader().loadTestsFromModule(
__import__(t, globals(), locals(), fromlist=["*"])
))
return suite
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.
- [Django]-Django, name parameter in urlpatterns
- [Django]-How to return custom JSON in Django REST Framework
- [Django]-Django User model, adding function
5π
Use nosetests with plugin for django (django-sane-testing or django-nose) and use the βwith-doctest flag.
- [Django]-What's the best solution for OpenID with Django?
- [Django]-Django, filter by specified month and year in date range
- [Django]-How to get GET request values in Django Views?
2π
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
- [Django]-Django urls without a trailing slash do not redirect
- [Django]-Matching query does not exist Error in Django
- [Django]-Django fix Admin plural
1π
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
- [Django]-Django CSRF Cookie Not Set
- [Django]-Django: get unique object list from QuerySet
- [Django]-How to use schemas in Django?
1π
Djangoβs native testing system is based on unittest
package. So it is not as powerful as it can be.
I recommend you using nose that is backward-compatible unittest
on steroids. Use it along with Django test runner that uses nose. You can customize nose in many ways including pointing it to custom test locations using -m
flag.
- [Django]-Django Rest Framework: Disable field update after object is created
- [Django]-Where did Django form wizard go in django 1.8?
- [Django]-Redis Python β how to delete all keys according to a specific pattern In python, without python iterating