Refactored old test runner to handle apps without a models module.
This commit is contained in:
parent
5ba743e262
commit
30bdad1c47
|
@ -96,22 +96,13 @@ class DocTestRunner(doctest.DocTestRunner):
|
||||||
doctestOutputChecker = OutputChecker()
|
doctestOutputChecker = OutputChecker()
|
||||||
|
|
||||||
|
|
||||||
def get_tests(app_module):
|
def get_tests(app_config):
|
||||||
parts = app_module.__name__.split('.')
|
|
||||||
prefix, last = parts[:-1], parts[-1]
|
|
||||||
try:
|
try:
|
||||||
test_module = import_module('.'.join(prefix + [TEST_MODULE]))
|
test_module = import_module('%s.%s' % (app_config.name, TEST_MODULE))
|
||||||
except ImportError:
|
except ImportError:
|
||||||
# Couldn't import tests.py. Was it due to a missing file, or
|
# 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?
|
# due to an import error in a tests.py that actually exists?
|
||||||
# app_module either points to a models.py file, or models/__init__.py
|
if not module_has_submodule(app_config.app_module, TEST_MODULE):
|
||||||
# Tests are therefore either in same directory, or one level up
|
|
||||||
if last == 'models':
|
|
||||||
app_root = import_module('.'.join(prefix))
|
|
||||||
else:
|
|
||||||
app_root = app_module
|
|
||||||
|
|
||||||
if not module_has_submodule(app_root, TEST_MODULE):
|
|
||||||
test_module = None
|
test_module = None
|
||||||
else:
|
else:
|
||||||
# The module exists, so there must be an import error in the test
|
# The module exists, so there must be an import error in the test
|
||||||
|
@ -126,7 +117,7 @@ def make_doctest(module):
|
||||||
runner=DocTestRunner)
|
runner=DocTestRunner)
|
||||||
|
|
||||||
|
|
||||||
def build_suite(app_module):
|
def build_suite(app_config):
|
||||||
"""
|
"""
|
||||||
Create a complete Django test suite for the provided application module.
|
Create a complete Django test suite for the provided application module.
|
||||||
"""
|
"""
|
||||||
|
@ -134,30 +125,32 @@ def build_suite(app_module):
|
||||||
|
|
||||||
# Load unit and doctests in the models.py module. If module has
|
# Load unit and doctests in the models.py module. If module has
|
||||||
# a suite() method, use it. Otherwise build the test suite ourselves.
|
# a suite() method, use it. Otherwise build the test suite ourselves.
|
||||||
if hasattr(app_module, 'suite'):
|
models_module = app_config.models_module
|
||||||
suite.addTest(app_module.suite())
|
if models_module:
|
||||||
else:
|
if hasattr(models_module, 'suite'):
|
||||||
suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(
|
suite.addTest(models_module.suite())
|
||||||
app_module))
|
else:
|
||||||
try:
|
suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(
|
||||||
suite.addTest(make_doctest(app_module))
|
models_module))
|
||||||
except ValueError:
|
try:
|
||||||
# No doc tests in models.py
|
suite.addTest(make_doctest(models_module))
|
||||||
pass
|
except ValueError:
|
||||||
|
# No doc tests in models.py
|
||||||
|
pass
|
||||||
|
|
||||||
# Check to see if a separate 'tests' module exists parallel to the
|
# Check to see if a separate 'tests' module exists parallel to the
|
||||||
# models module
|
# models module
|
||||||
test_module = get_tests(app_module)
|
tests_module = get_tests(app_config)
|
||||||
if test_module:
|
if tests_module:
|
||||||
# Load unit and doctests in the tests.py module. If module has
|
# Load unit and doctests in the tests.py module. If module has
|
||||||
# a suite() method, use it. Otherwise build the test suite ourselves.
|
# a suite() method, use it. Otherwise build the test suite ourselves.
|
||||||
if hasattr(test_module, 'suite'):
|
if hasattr(tests_module, 'suite'):
|
||||||
suite.addTest(test_module.suite())
|
suite.addTest(tests_module.suite())
|
||||||
else:
|
else:
|
||||||
suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(
|
suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(
|
||||||
test_module))
|
tests_module))
|
||||||
try:
|
try:
|
||||||
suite.addTest(make_doctest(test_module))
|
suite.addTest(make_doctest(tests_module))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# No doc tests in tests.py
|
# No doc tests in tests.py
|
||||||
pass
|
pass
|
||||||
|
@ -167,26 +160,29 @@ def build_suite(app_module):
|
||||||
def build_test(label):
|
def build_test(label):
|
||||||
"""
|
"""
|
||||||
Construct a test case with the specified label. Label should be of the
|
Construct a test case with the specified label. Label should be of the
|
||||||
form model.TestClass or model.TestClass.test_method. Returns an
|
form app_label.TestClass or app_label.TestClass.test_method. Returns an
|
||||||
instantiated test or test suite corresponding to the label provided.
|
instantiated test or test suite corresponding to the label provided.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
parts = label.split('.')
|
parts = label.split('.')
|
||||||
if len(parts) < 2 or len(parts) > 3:
|
if len(parts) < 2 or len(parts) > 3:
|
||||||
raise ValueError("Test label '%s' should be of the form app.TestCase "
|
raise ValueError("Test label '%s' should be of the form app.TestCase "
|
||||||
"or app.TestCase.test_method" % label)
|
"or app.TestCase.test_method" % label)
|
||||||
|
|
||||||
#
|
app_config = app_cache.get_app_config(parts[0])
|
||||||
# First, look for TestCase instances with a name that matches
|
models_module = app_config.models_module
|
||||||
#
|
tests_module = get_tests(app_config)
|
||||||
app_module = app_cache.get_app_config(parts[0]).models_module
|
|
||||||
test_module = get_tests(app_module)
|
|
||||||
TestClass = getattr(app_module, parts[1], None)
|
|
||||||
|
|
||||||
# Couldn't find the test class in models.py; look in tests.py
|
test_modules = []
|
||||||
if TestClass is None:
|
if models_module:
|
||||||
if test_module:
|
test_modules.append(models_module)
|
||||||
TestClass = getattr(test_module, parts[1], None)
|
if tests_module:
|
||||||
|
test_modules.append(tests_module)
|
||||||
|
|
||||||
|
TestClass = None
|
||||||
|
for module in test_modules:
|
||||||
|
TestClass = getattr(models_module, parts[1], None)
|
||||||
|
if TestClass is not None:
|
||||||
|
break
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if issubclass(TestClass, (unittest.TestCase, real_unittest.TestCase)):
|
if issubclass(TestClass, (unittest.TestCase, real_unittest.TestCase)):
|
||||||
|
@ -208,7 +204,7 @@ def build_test(label):
|
||||||
# If there isn't a TestCase, look for a doctest that matches
|
# If there isn't a TestCase, look for a doctest that matches
|
||||||
#
|
#
|
||||||
tests = []
|
tests = []
|
||||||
for module in app_module, test_module:
|
for module in test_modules:
|
||||||
try:
|
try:
|
||||||
doctests = make_doctest(module)
|
doctests = make_doctest(module)
|
||||||
# Now iterate over the suite, looking for doctests whose name
|
# Now iterate over the suite, looking for doctests whose name
|
||||||
|
@ -241,11 +237,11 @@ class DjangoTestSuiteRunner(runner.DiscoverRunner):
|
||||||
if '.' in label:
|
if '.' in label:
|
||||||
suite.addTest(build_test(label))
|
suite.addTest(build_test(label))
|
||||||
else:
|
else:
|
||||||
app = app_cache.get_app_config(label).models_module
|
app_config = app_cache.get_app_config(label)
|
||||||
suite.addTest(build_suite(app))
|
suite.addTest(build_suite(app_config))
|
||||||
else:
|
else:
|
||||||
for app_config in app_cache.get_app_configs():
|
for app_config in app_cache.get_app_configs():
|
||||||
suite.addTest(build_suite(app_config.models_module))
|
suite.addTest(build_suite(app_config))
|
||||||
|
|
||||||
if extra_tests:
|
if extra_tests:
|
||||||
for test in extra_tests:
|
for test in extra_tests:
|
||||||
|
|
|
@ -5,6 +5,7 @@ from __future__ import unicode_literals
|
||||||
|
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
from optparse import make_option
|
from optparse import make_option
|
||||||
|
import types
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
|
@ -18,10 +19,6 @@ from admin_scripts.tests import AdminScriptTestCase
|
||||||
from .models import Person
|
from .models import Person
|
||||||
|
|
||||||
|
|
||||||
TEST_APP_OK = 'test_runner.valid_app.models'
|
|
||||||
TEST_APP_ERROR = 'test_runner_invalid_app.models'
|
|
||||||
|
|
||||||
|
|
||||||
class DependencyOrderingTests(unittest.TestCase):
|
class DependencyOrderingTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_simple_dependencies(self):
|
def test_simple_dependencies(self):
|
||||||
|
@ -228,16 +225,24 @@ class ModulesTestsPackages(IgnoreAllDeprecationWarningsMixin, unittest.TestCase)
|
||||||
|
|
||||||
def test_get_tests(self):
|
def test_get_tests(self):
|
||||||
"Check that the get_tests helper function can find tests in a directory"
|
"Check that the get_tests helper function can find tests in a directory"
|
||||||
|
from django.apps.base import AppConfig
|
||||||
from django.test.simple import get_tests
|
from django.test.simple import get_tests
|
||||||
module = import_module(TEST_APP_OK)
|
app_config = AppConfig(
|
||||||
tests = get_tests(module)
|
'test_runner.valid_app',
|
||||||
self.assertIsInstance(tests, type(module))
|
import_module('test_runner.valid_app'),
|
||||||
|
import_module('test_runner.valid_app.models'))
|
||||||
|
tests = get_tests(app_config)
|
||||||
|
self.assertIsInstance(tests, types.ModuleType)
|
||||||
|
|
||||||
def test_import_error(self):
|
def test_import_error(self):
|
||||||
"Test for #12658 - Tests with ImportError's shouldn't fail silently"
|
"Test for #12658 - Tests with ImportError's shouldn't fail silently"
|
||||||
|
from django.apps.base import AppConfig
|
||||||
from django.test.simple import get_tests
|
from django.test.simple import get_tests
|
||||||
module = import_module(TEST_APP_ERROR)
|
app_config = AppConfig(
|
||||||
self.assertRaises(ImportError, get_tests, module)
|
'test_runner_invalid_app',
|
||||||
|
import_module('test_runner_invalid_app'),
|
||||||
|
import_module('test_runner_invalid_app.models'))
|
||||||
|
self.assertRaises(ImportError, get_tests, app_config)
|
||||||
|
|
||||||
|
|
||||||
class Sqlite3InMemoryTestDbs(TestCase):
|
class Sqlite3InMemoryTestDbs(TestCase):
|
||||||
|
|
|
@ -20,8 +20,8 @@ class SuiteOverrideTest(IgnoreAllDeprecationWarningsMixin, unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django.test.simple import build_suite
|
from django.test.simple import build_suite
|
||||||
app = app_cache.get_app_config("test_suite_override").models_module
|
app_config = app_cache.get_app_config("test_suite_override")
|
||||||
suite = build_suite(app)
|
suite = build_suite(app_config)
|
||||||
self.assertEqual(suite.countTestCases(), 1)
|
self.assertEqual(suite.countTestCases(), 1)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue