Skip to content

Instantly share code, notes, and snippets.

@cloverrose
Created May 19, 2012 04:31
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/2729119 to your computer and use it in GitHub Desktop.
Save cloverrose/2729119 to your computer and use it in GitHub Desktop.
[Django] [python manage.py test] this is error program!! Django組み込みのテストを拡張してtest.py以外のスクリプト(mytest.pyやawesome.py)内のdoctestも実行できるようにする
# -*- coding:utf-8 -*-
"""
this is error program
これは間違いを示すためのプログラムです。
(1)と(2)のforループはmodels.pyに記述された
doctestを複数回テストスイーツに追加してしまいます。
その結果、予期しないテストが実行されてしまいます。
例えば、doctest内でデータベースに値を追加するコードがあったとすると、
複数回同じ値を追加することになります。
"""
from django.utils import unittest
from django.db.models import get_app, get_apps
from django.test import _doctest as doctest
from django.test import simple
from django.test.simple import DjangoTestSuiteRunner,build_suite,doctestOutputChecker,build_test,DocTestRunner,reorder_suite,TestCase,get_tests
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))
org_test_module=simple.TEST_MODULE
for n in my_test_modules: # (1)
simple.TEST_MODULE=n
suite.addTest(build_suite(app))
simple.TEST_MODULE=org_test_module
else:
for app in get_apps():
suite.addTest(build_suite(app))
org_test_module=simple.TEST_MODULE
for n in my_test_modules: # (2)
simple.TEST_MODULE=n
suite.addTest(build_suite(app))
simple.TEST_MODULE=org_test_module
if extra_tests:
for test in extra_tests:
suite.addTest(test)
return reorder_suite(suite, (TestCase,))
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_error.run_tests'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment