2013-05-11 11:08:45 +08:00
|
|
|
"""
|
|
|
|
This module is pending deprecation as of Django 1.6 and will be removed in
|
|
|
|
version 1.8.
|
|
|
|
|
|
|
|
"""
|
2013-07-29 21:50:58 +08:00
|
|
|
from importlib import import_module
|
2013-05-28 04:41:39 +08:00
|
|
|
import json
|
|
|
|
import re
|
2011-03-18 09:17:48 +08:00
|
|
|
import unittest as real_unittest
|
2013-05-11 11:08:45 +08:00
|
|
|
import warnings
|
2011-06-12 20:34:10 +08:00
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps import apps
|
2007-05-31 21:18:12 +08:00
|
|
|
from django.test import _doctest as doctest
|
2013-05-11 11:08:45 +08:00
|
|
|
from django.test import runner
|
2013-05-28 04:41:39 +08:00
|
|
|
from django.test.utils import compare_xml, strip_quotes
|
2013-07-02 03:49:11 +08:00
|
|
|
# django.utils.unittest is deprecated, but so is django.test.simple,
|
|
|
|
# and the latter will be removed before the former.
|
2010-10-11 20:55:17 +08:00
|
|
|
from django.utils import unittest
|
2014-02-27 05:48:20 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango18Warning
|
2011-06-12 20:34:10 +08:00
|
|
|
from django.utils.module_loading import module_has_submodule
|
2006-08-27 20:24:59 +08:00
|
|
|
|
2013-02-01 02:40:38 +08:00
|
|
|
__all__ = ('DjangoTestSuiteRunner',)
|
2010-12-05 08:44:34 +08:00
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
warnings.warn(
|
|
|
|
"The django.test.simple module and DjangoTestSuiteRunner are deprecated; "
|
|
|
|
"use django.test.runner.DiscoverRunner instead.",
|
2014-02-27 05:48:20 +08:00
|
|
|
RemovedInDjango18Warning)
|
2013-05-11 11:08:45 +08:00
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
# The module name for tests outside models.py
|
|
|
|
TEST_MODULE = 'tests'
|
2009-02-22 16:34:51 +08:00
|
|
|
|
2013-05-28 04:41:39 +08:00
|
|
|
|
|
|
|
normalize_long_ints = lambda s: re.sub(r'(?<![\w])(\d+)L(?![\w])', '\\1', s)
|
|
|
|
normalize_decimals = lambda s: re.sub(r"Decimal\('(\d+(\.\d*)?)'\)",
|
|
|
|
lambda m: "Decimal(\"%s\")" % m.groups()[0], s)
|
|
|
|
|
|
|
|
|
|
|
|
class OutputChecker(doctest.OutputChecker):
|
|
|
|
def check_output(self, want, got, optionflags):
|
|
|
|
"""
|
|
|
|
The entry method for doctest output checking. Defers to a sequence of
|
|
|
|
child checkers
|
|
|
|
"""
|
|
|
|
checks = (self.check_output_default,
|
|
|
|
self.check_output_numeric,
|
|
|
|
self.check_output_xml,
|
|
|
|
self.check_output_json)
|
|
|
|
for check in checks:
|
|
|
|
if check(want, got, optionflags):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def check_output_default(self, want, got, optionflags):
|
|
|
|
"""
|
|
|
|
The default comparator provided by doctest - not perfect, but good for
|
|
|
|
most purposes
|
|
|
|
"""
|
|
|
|
return doctest.OutputChecker.check_output(self, want, got, optionflags)
|
|
|
|
|
|
|
|
def check_output_numeric(self, want, got, optionflags):
|
|
|
|
"""Doctest does an exact string comparison of output, which means that
|
|
|
|
some numerically equivalent values aren't equal. This check normalizes
|
|
|
|
* long integers (22L) so that they equal normal integers. (22)
|
|
|
|
* Decimals so that they are comparable, regardless of the change
|
|
|
|
made to __repr__ in Python 2.6.
|
|
|
|
"""
|
|
|
|
return doctest.OutputChecker.check_output(self,
|
|
|
|
normalize_decimals(normalize_long_ints(want)),
|
|
|
|
normalize_decimals(normalize_long_ints(got)),
|
|
|
|
optionflags)
|
|
|
|
|
|
|
|
def check_output_xml(self, want, got, optionsflags):
|
|
|
|
try:
|
|
|
|
return compare_xml(want, got)
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def check_output_json(self, want, got, optionsflags):
|
|
|
|
"""
|
|
|
|
Tries to compare want and got as if they were JSON-encoded data
|
|
|
|
"""
|
|
|
|
want, got = strip_quotes(want, got)
|
|
|
|
try:
|
|
|
|
want_json = json.loads(want)
|
|
|
|
got_json = json.loads(got)
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
return want_json == got_json
|
|
|
|
|
|
|
|
|
|
|
|
class DocTestRunner(doctest.DocTestRunner):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
doctest.DocTestRunner.__init__(self, *args, **kwargs)
|
|
|
|
self.optionflags = doctest.ELLIPSIS
|
|
|
|
|
|
|
|
|
2006-08-27 20:24:59 +08:00
|
|
|
doctestOutputChecker = OutputChecker()
|
|
|
|
|
2012-01-30 16:27:50 +08:00
|
|
|
|
2013-12-15 03:36:22 +08:00
|
|
|
def get_tests(app_config):
|
2007-07-28 12:02:52 +08:00
|
|
|
try:
|
2013-12-15 03:36:22 +08:00
|
|
|
test_module = import_module('%s.%s' % (app_config.name, TEST_MODULE))
|
2011-06-12 20:34:10 +08:00
|
|
|
except ImportError:
|
2007-07-28 12:02:52 +08:00
|
|
|
# Couldn't import tests.py. Was it due to a missing file, or
|
|
|
|
# due to an import error in a tests.py that actually exists?
|
2013-12-27 01:40:28 +08:00
|
|
|
if not module_has_submodule(app_config.module, TEST_MODULE):
|
2007-07-28 12:02:52 +08:00
|
|
|
test_module = None
|
|
|
|
else:
|
2011-06-12 20:34:10 +08:00
|
|
|
# The module exists, so there must be an import error in the test
|
|
|
|
# module itself.
|
2007-07-28 12:02:52 +08:00
|
|
|
raise
|
|
|
|
return test_module
|
2009-02-22 16:34:51 +08:00
|
|
|
|
2012-01-30 16:27:50 +08:00
|
|
|
|
2013-04-07 01:09:44 +08:00
|
|
|
def make_doctest(module):
|
|
|
|
return doctest.DocTestSuite(module,
|
|
|
|
checker=doctestOutputChecker,
|
2013-12-09 01:20:06 +08:00
|
|
|
runner=DocTestRunner)
|
2013-04-07 01:09:44 +08:00
|
|
|
|
|
|
|
|
2013-12-15 03:36:22 +08:00
|
|
|
def build_suite(app_config):
|
2012-01-30 16:27:50 +08:00
|
|
|
"""
|
|
|
|
Create a complete Django test suite for the provided application module.
|
|
|
|
"""
|
2006-08-27 20:24:59 +08:00
|
|
|
suite = unittest.TestSuite()
|
2009-02-22 16:34:51 +08:00
|
|
|
|
2007-07-20 21:57:49 +08:00
|
|
|
# Load unit and doctests in the models.py module. If module has
|
|
|
|
# a suite() method, use it. Otherwise build the test suite ourselves.
|
2013-12-15 03:36:22 +08:00
|
|
|
models_module = app_config.models_module
|
|
|
|
if models_module:
|
|
|
|
if hasattr(models_module, 'suite'):
|
|
|
|
suite.addTest(models_module.suite())
|
|
|
|
else:
|
|
|
|
suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(
|
|
|
|
models_module))
|
|
|
|
try:
|
|
|
|
suite.addTest(make_doctest(models_module))
|
|
|
|
except ValueError:
|
|
|
|
# No doc tests in models.py
|
|
|
|
pass
|
2009-02-22 16:34:51 +08:00
|
|
|
|
|
|
|
# Check to see if a separate 'tests' module exists parallel to the
|
2006-08-27 20:24:59 +08:00
|
|
|
# models module
|
2013-12-15 03:36:22 +08:00
|
|
|
tests_module = get_tests(app_config)
|
|
|
|
if tests_module:
|
2007-07-20 21:57:49 +08:00
|
|
|
# Load unit and doctests in the tests.py module. If module has
|
|
|
|
# a suite() method, use it. Otherwise build the test suite ourselves.
|
2013-12-15 03:36:22 +08:00
|
|
|
if hasattr(tests_module, 'suite'):
|
|
|
|
suite.addTest(tests_module.suite())
|
2007-07-20 21:57:49 +08:00
|
|
|
else:
|
2012-01-30 16:27:50 +08:00
|
|
|
suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(
|
2013-12-15 03:36:22 +08:00
|
|
|
tests_module))
|
2009-02-22 16:34:51 +08:00
|
|
|
try:
|
2013-12-15 03:36:22 +08:00
|
|
|
suite.addTest(make_doctest(tests_module))
|
2007-07-20 21:57:49 +08:00
|
|
|
except ValueError:
|
|
|
|
# No doc tests in tests.py
|
|
|
|
pass
|
2006-08-27 20:24:59 +08:00
|
|
|
return suite
|
|
|
|
|
2012-01-30 16:27:50 +08:00
|
|
|
|
2007-07-28 12:02:52 +08:00
|
|
|
def build_test(label):
|
2012-01-30 16:27:50 +08:00
|
|
|
"""
|
|
|
|
Construct a test case with the specified label. Label should be of the
|
2013-12-15 03:36:22 +08:00
|
|
|
form app_label.TestClass or app_label.TestClass.test_method. Returns an
|
2010-01-19 09:14:02 +08:00
|
|
|
instantiated test or test suite corresponding to the label provided.
|
2007-07-28 12:02:52 +08:00
|
|
|
"""
|
|
|
|
parts = label.split('.')
|
|
|
|
if len(parts) < 2 or len(parts) > 3:
|
2012-01-30 16:27:50 +08:00
|
|
|
raise ValueError("Test label '%s' should be of the form app.TestCase "
|
|
|
|
"or app.TestCase.test_method" % label)
|
2009-02-22 16:34:51 +08:00
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
app_config = apps.get_app_config(parts[0])
|
2013-12-15 03:36:22 +08:00
|
|
|
models_module = app_config.models_module
|
|
|
|
tests_module = get_tests(app_config)
|
|
|
|
|
|
|
|
test_modules = []
|
|
|
|
if models_module:
|
|
|
|
test_modules.append(models_module)
|
|
|
|
if tests_module:
|
|
|
|
test_modules.append(tests_module)
|
2007-07-28 12:02:52 +08:00
|
|
|
|
2013-12-15 03:36:22 +08:00
|
|
|
TestClass = None
|
|
|
|
for module in test_modules:
|
|
|
|
TestClass = getattr(models_module, parts[1], None)
|
|
|
|
if TestClass is not None:
|
|
|
|
break
|
2007-07-28 12:02:52 +08:00
|
|
|
|
2010-02-01 21:00:00 +08:00
|
|
|
try:
|
2011-03-18 09:17:48 +08:00
|
|
|
if issubclass(TestClass, (unittest.TestCase, real_unittest.TestCase)):
|
2013-11-03 05:02:56 +08:00
|
|
|
if len(parts) == 2: # label is app.TestClass
|
2010-02-01 21:00:00 +08:00
|
|
|
try:
|
2012-01-30 16:27:50 +08:00
|
|
|
return unittest.TestLoader().loadTestsFromTestCase(
|
|
|
|
TestClass)
|
2010-02-01 21:00:00 +08:00
|
|
|
except TypeError:
|
2012-01-30 16:27:50 +08:00
|
|
|
raise ValueError(
|
|
|
|
"Test label '%s' does not refer to a test class"
|
|
|
|
% label)
|
2013-11-03 05:02:56 +08:00
|
|
|
else: # label is app.TestClass.test_method
|
2010-02-01 21:00:00 +08:00
|
|
|
return TestClass(parts[2])
|
|
|
|
except TypeError:
|
|
|
|
# TestClass isn't a TestClass - it must be a method or normal class
|
|
|
|
pass
|
|
|
|
|
|
|
|
#
|
|
|
|
# If there isn't a TestCase, look for a doctest that matches
|
|
|
|
#
|
|
|
|
tests = []
|
2013-12-15 03:36:22 +08:00
|
|
|
for module in test_modules:
|
2007-07-28 12:02:52 +08:00
|
|
|
try:
|
2013-04-07 01:09:44 +08:00
|
|
|
doctests = make_doctest(module)
|
2010-02-01 21:00:00 +08:00
|
|
|
# Now iterate over the suite, looking for doctests whose name
|
|
|
|
# matches the pattern that was given
|
|
|
|
for test in doctests:
|
|
|
|
if test._dt_test.name in (
|
|
|
|
'%s.%s' % (module.__name__, '.'.join(parts[1:])),
|
2012-01-30 16:27:50 +08:00
|
|
|
'%s.__test__.%s' % (
|
|
|
|
module.__name__, '.'.join(parts[1:]))):
|
2010-02-01 21:00:00 +08:00
|
|
|
tests.append(test)
|
|
|
|
except ValueError:
|
|
|
|
# No doctests found.
|
|
|
|
pass
|
|
|
|
|
|
|
|
# If no tests were found, then we were given a bad test label.
|
|
|
|
if not tests:
|
|
|
|
raise ValueError("Test label '%s' does not refer to a test" % label)
|
|
|
|
|
|
|
|
# Construct a suite out of the tests that matched.
|
|
|
|
return unittest.TestSuite(tests)
|
2007-07-28 12:02:52 +08:00
|
|
|
|
2012-01-30 16:27:50 +08:00
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
class DjangoTestSuiteRunner(runner.DiscoverRunner):
|
2010-01-18 23:11:01 +08:00
|
|
|
|
2010-02-22 20:34:52 +08:00
|
|
|
def build_suite(self, test_labels, extra_tests=None, **kwargs):
|
2010-01-18 23:11:01 +08:00
|
|
|
suite = unittest.TestSuite()
|
|
|
|
|
|
|
|
if test_labels:
|
|
|
|
for label in test_labels:
|
|
|
|
if '.' in label:
|
|
|
|
suite.addTest(build_test(label))
|
|
|
|
else:
|
2013-12-24 19:25:17 +08:00
|
|
|
app_config = apps.get_app_config(label)
|
2013-12-15 03:36:22 +08:00
|
|
|
suite.addTest(build_suite(app_config))
|
2010-01-18 23:11:01 +08:00
|
|
|
else:
|
2013-12-24 19:25:17 +08:00
|
|
|
for app_config in apps.get_app_configs():
|
2013-12-15 03:36:22 +08:00
|
|
|
suite.addTest(build_suite(app_config))
|
2009-02-22 16:34:51 +08:00
|
|
|
|
2010-01-18 23:11:01 +08:00
|
|
|
if extra_tests:
|
|
|
|
for test in extra_tests:
|
|
|
|
suite.addTest(test)
|
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
return runner.reorder_suite(suite, (unittest.TestCase,))
|