1 | import unittest
|
---|
2 | from django.conf import settings
|
---|
3 | from django.db.models import get_app, get_apps
|
---|
4 | from django.test import _doctest as doctest
|
---|
5 | from django.test.utils import setup_test_environment, teardown_test_environment
|
---|
6 | from django.contrib.gis.db.backend import create_spatial_db
|
---|
7 | from django.test.testcases import OutputChecker, DocTestRunner
|
---|
8 |
|
---|
9 | # The module name for tests outside models.py
|
---|
10 | TEST_MODULE = 'tests'
|
---|
11 |
|
---|
12 | doctestOutputChecker = OutputChecker()
|
---|
13 |
|
---|
14 | def get_tests(app_module):
|
---|
15 | try:
|
---|
16 | app_path = app_module.__name__.split('.')[:-1]
|
---|
17 | test_module = __import__('.'.join(app_path + [TEST_MODULE]), {}, {}, TEST_MODULE)
|
---|
18 | except ImportError, e:
|
---|
19 | import os.path
|
---|
20 | from imp import find_module
|
---|
21 | try:
|
---|
22 | mod = find_module(TEST_MODULE, [os.path.dirname(app_module.__file__)])
|
---|
23 | except ImportError:
|
---|
24 | test_module = None
|
---|
25 | else:
|
---|
26 | if mod[0]:
|
---|
27 | mod[0].close()
|
---|
28 | raise
|
---|
29 | return test_module
|
---|
30 |
|
---|
31 | def build_suite(app_module):
|
---|
32 | "Create a complete Django test suite for the provided application module"
|
---|
33 | suite = unittest.TestSuite()
|
---|
34 |
|
---|
35 | if hasattr(app_module, 'suite'):
|
---|
36 | suite.addTest(app_module.suite())
|
---|
37 | else:
|
---|
38 | suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(app_module))
|
---|
39 | try:
|
---|
40 | suite.addTest(doctest.DocTestSuite(app_module,
|
---|
41 | checker=doctestOutputChecker,
|
---|
42 | runner=DocTestRunner))
|
---|
43 | except ValueError:
|
---|
44 | pass
|
---|
45 |
|
---|
46 | test_module = get_tests(app_module)
|
---|
47 | if test_module:
|
---|
48 | if hasattr(test_module, 'suite'):
|
---|
49 | suite.addTest(test_module.suite())
|
---|
50 | else:
|
---|
51 | suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(test_module))
|
---|
52 | try:
|
---|
53 | suite.addTest(doctest.DocTestSuite(test_module,
|
---|
54 | checker=doctestOutputChecker,
|
---|
55 | runner=DocTestRunner))
|
---|
56 | except ValueError:
|
---|
57 | # No doc tests in tests.py
|
---|
58 | pass
|
---|
59 | return suite
|
---|
60 |
|
---|
61 | def build_test(label):
|
---|
62 | parts = label.split('.')
|
---|
63 | if len(parts) < 2 or len(parts) > 3:
|
---|
64 | raise ValueError("Test label '%s' should be of the form app.TestCase or app.TestCase.test_method" % label)
|
---|
65 |
|
---|
66 | app_module = get_app(parts[0])
|
---|
67 | TestClass = getattr(app_module, parts[1], None)
|
---|
68 |
|
---|
69 | # Couldn't find the test class in models.py; look in tests.py
|
---|
70 | if TestClass is None:
|
---|
71 | test_module = get_tests(app_module)
|
---|
72 | if test_module:
|
---|
73 | TestClass = getattr(test_module, parts[1], None)
|
---|
74 |
|
---|
75 | if len(parts) == 2: # label is app.TestClass
|
---|
76 | try:
|
---|
77 | return unittest.TestLoader().loadTestsFromTestCase(TestClass)
|
---|
78 | except TypeError:
|
---|
79 | raise ValueError("Test label '%s' does not refer to a test class" % label)
|
---|
80 | else: # label is app.TestClass.test_method
|
---|
81 | return TestClass(parts[2])
|
---|
82 |
|
---|
83 | def run_tests(test_labels, verbosity=2, interactive=True, extra_tests=[]):
|
---|
84 | from django.db import connection
|
---|
85 | setup_test_environment()
|
---|
86 |
|
---|
87 | settings.DEBUG = False
|
---|
88 | suite = unittest.TestSuite()
|
---|
89 |
|
---|
90 | if test_labels:
|
---|
91 | for label in test_labels:
|
---|
92 | if '.' in label:
|
---|
93 | suite.addTest(build_test(label))
|
---|
94 | else:
|
---|
95 | app = get_app(label)
|
---|
96 | suite.addTest(build_suite(app))
|
---|
97 | else:
|
---|
98 | for app in get_apps():
|
---|
99 | suite.addTest(build_suite(app))
|
---|
100 |
|
---|
101 | for test in extra_tests:
|
---|
102 | suite.addTest(test)
|
---|
103 |
|
---|
104 | print suite
|
---|
105 |
|
---|
106 | old_name = settings.DATABASE_NAME
|
---|
107 | create_spatial_db(test=True, verbosity=verbosity)
|
---|
108 | result = unittest.TextTestRunner(verbosity=verbosity).run(suite)
|
---|
109 | connection.creation.destroy_test_db(old_name, verbosity)
|
---|
110 |
|
---|
111 | teardown_test_environment()
|
---|
112 |
|
---|
113 | return len(result.failures) + len(result.errors)
|
---|
114 |
|
---|