Configure Django to find all doctests in all modules?

后端 未结 5 1904
终归单人心
终归单人心 2021-02-14 09:27

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 th

5条回答
  •  情话喂你
    2021-02-14 10:21

    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.

提交回复
热议问题