Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Django to find all doctests in all modules?

If I run the following command:

>python manage.py test

Django looks at tests.py in my application, and runs any doctests or unit tests in that file. It also looks at the __ test __ dictionary for extra tests to run. So I can link doctests from other modules like so:

#tests.py
from myapp.module1 import _function1, _function2

__test__ = {
    "_function1": _function1,
    "_function2": _function2
}

If I want to include more doctests, is there an easier way than enumerating them all in this dictionary? Ideally, I just want to have Django find all doctests in all modules in the myapp application.

Is there some kind of reflection hack that would get me where I want to be?

like image 843
Chase Seibert Avatar asked Oct 23 '09 19:10

Chase Seibert


2 Answers

I solved this for myself a while ago:

apps = settings.INSTALLED_APPS

for app in apps:
    try:
        a = app + '.test'
        __import__(a)
        m = sys.modules[a]
    except ImportError: #no test jobs for this module, continue to next one
        continue
    #run your test using the imported module m

This allowed me to put per-module tests in their own test.py file, so they didn't get mixed up with the rest of my application code. It would be easy to modify this to just look for doc tests in each of your modules and run them if it found them.

like image 69
Paul McMillan Avatar answered Sep 30 '22 10:09

Paul McMillan


Use django-nose since nose automatically find all tests recursivelly.

like image 45
e-satis Avatar answered Sep 30 '22 09:09

e-satis