Skip to content

Instantly share code, notes, and snippets.

@cloverrose
Created May 19, 2012 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cloverrose/2729484 to your computer and use it in GitHub Desktop.
Save cloverrose/2729484 to your computer and use it in GitHub Desktop.
[Django] [python manage.py test] Django組み込みのテストを拡張してtest.py以外のスクリプト(mytest.pyやawesome.py)内のdoctestも実行できるようにする
# -*- coding:utf-8 -*-
from django.utils import unittest
from django.db.models import get_app, get_apps
from django.test import _doctest as doctest
from django.test.simple import DjangoTestSuiteRunner,build_suite,doctestOutputChecker,build_test,DocTestRunner,reorder_suite,TestCase,get_tests
from django.test import simple
my_test_modules=['mytest','awesome']
class MyDjangoTestSuiteRunner(DjangoTestSuiteRunner):
def build_suite(self, test_labels, extra_tests=None, **kwargs):
suite = unittest.TestSuite()
if test_labels:
for label in test_labels:
if '.' in label:
suite.addTest(build_test(label))
else:
app = get_app(label)
suite.addTest(build_suite(app))
suite.addTest(my_build_suite(app))
else:
for app in get_apps():
suite.addTest(build_suite(app))
suite.addTest(my_build_suite(app))
if extra_tests:
for test in extra_tests:
suite.addTest(test)
return reorder_suite(suite, (TestCase,))
def my_build_suite(app_module):
suite = unittest.TestSuite()
org_test_module=simple.TEST_MODULE
for n in my_test_modules:
simple.TEST_MODULE=n
test_module = get_tests(app_module)
if test_module:
# Load unit and doctests in the tests.py module. If module has
# a suite() method, use it. Otherwise build the test suite ourselves.
if hasattr(test_module, 'suite'):
suite.addTest(test_module.suite())
else:
suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(test_module))
try:
suite.addTest(doctest.DocTestSuite(test_module,
checker=doctestOutputChecker,
runner=DocTestRunner))
except ValueError:
# No doc tests in tests.py
pass
simple.TEST_MODULE=org_test_module
return suite
def run_tests(test_labels, verbosity=1, interactive=True, failfast=False, extra_tests=None):
import warnings
warnings.warn(
'The run_tests() test runner has been deprecated in favor of DjangoTestSuiteRunner.',
DeprecationWarning
)
test_runner = MyDjangoTestSuiteRunner(verbosity=verbosity, interactive=interactive, failfast=failfast)
return test_runner.run_tests(test_labels, extra_tests=extra_tests)
TEST_RUNNER='mysite.run_test.run_tests'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment