PageRenderTime 112ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/test_runner/tests.py

https://github.com/Gautier/django_old
Python | 264 lines | 204 code | 52 blank | 8 comment | 5 complexity | 299ab86a1f3ee2a4d141d5425a8f7f99 MD5 | raw file
  1. """
  2. Tests for django test runner
  3. """
  4. from __future__ import absolute_import
  5. import StringIO
  6. from optparse import make_option
  7. import warnings
  8. from django.core.exceptions import ImproperlyConfigured
  9. from django.core.management import call_command
  10. from django.test import simple
  11. from django.test.simple import DjangoTestSuiteRunner, get_tests
  12. from django.test.utils import get_warnings_state, restore_warnings_state
  13. from django.utils import unittest
  14. from django.utils.importlib import import_module
  15. from ..admin_scripts.tests import AdminScriptTestCase
  16. TEST_APP_OK = 'regressiontests.test_runner.valid_app.models'
  17. TEST_APP_ERROR = 'regressiontests.test_runner.invalid_app.models'
  18. class DjangoTestRunnerTests(unittest.TestCase):
  19. def setUp(self):
  20. self._warnings_state = get_warnings_state()
  21. warnings.filterwarnings('ignore', category=DeprecationWarning,
  22. module='django.test.simple')
  23. def tearDown(self):
  24. restore_warnings_state(self._warnings_state)
  25. def test_failfast(self):
  26. class MockTestOne(unittest.TestCase):
  27. def runTest(self):
  28. assert False
  29. class MockTestTwo(unittest.TestCase):
  30. def runTest(self):
  31. assert False
  32. suite = unittest.TestSuite([MockTestOne(), MockTestTwo()])
  33. mock_stream = StringIO.StringIO()
  34. dtr = simple.DjangoTestRunner(verbosity=0, failfast=False, stream=mock_stream)
  35. result = dtr.run(suite)
  36. self.assertEqual(2, result.testsRun)
  37. self.assertEqual(2, len(result.failures))
  38. dtr = simple.DjangoTestRunner(verbosity=0, failfast=True, stream=mock_stream)
  39. result = dtr.run(suite)
  40. self.assertEqual(1, result.testsRun)
  41. self.assertEqual(1, len(result.failures))
  42. class DependencyOrderingTests(unittest.TestCase):
  43. def test_simple_dependencies(self):
  44. raw = [
  45. ('s1', ('s1_db', ['alpha'])),
  46. ('s2', ('s2_db', ['bravo'])),
  47. ('s3', ('s3_db', ['charlie'])),
  48. ]
  49. dependencies = {
  50. 'alpha': ['charlie'],
  51. 'bravo': ['charlie'],
  52. }
  53. ordered = simple.dependency_ordered(raw, dependencies=dependencies)
  54. ordered_sigs = [sig for sig,value in ordered]
  55. self.assertIn('s1', ordered_sigs)
  56. self.assertIn('s2', ordered_sigs)
  57. self.assertIn('s3', ordered_sigs)
  58. self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s1'))
  59. self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s2'))
  60. def test_chained_dependencies(self):
  61. raw = [
  62. ('s1', ('s1_db', ['alpha'])),
  63. ('s2', ('s2_db', ['bravo'])),
  64. ('s3', ('s3_db', ['charlie'])),
  65. ]
  66. dependencies = {
  67. 'alpha': ['bravo'],
  68. 'bravo': ['charlie'],
  69. }
  70. ordered = simple.dependency_ordered(raw, dependencies=dependencies)
  71. ordered_sigs = [sig for sig,value in ordered]
  72. self.assertIn('s1', ordered_sigs)
  73. self.assertIn('s2', ordered_sigs)
  74. self.assertIn('s3', ordered_sigs)
  75. # Explicit dependencies
  76. self.assertLess(ordered_sigs.index('s2'), ordered_sigs.index('s1'))
  77. self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s2'))
  78. # Implied dependencies
  79. self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s1'))
  80. def test_multiple_dependencies(self):
  81. raw = [
  82. ('s1', ('s1_db', ['alpha'])),
  83. ('s2', ('s2_db', ['bravo'])),
  84. ('s3', ('s3_db', ['charlie'])),
  85. ('s4', ('s4_db', ['delta'])),
  86. ]
  87. dependencies = {
  88. 'alpha': ['bravo','delta'],
  89. 'bravo': ['charlie'],
  90. 'delta': ['charlie'],
  91. }
  92. ordered = simple.dependency_ordered(raw, dependencies=dependencies)
  93. ordered_sigs = [sig for sig,aliases in ordered]
  94. self.assertIn('s1', ordered_sigs)
  95. self.assertIn('s2', ordered_sigs)
  96. self.assertIn('s3', ordered_sigs)
  97. self.assertIn('s4', ordered_sigs)
  98. # Explicit dependencies
  99. self.assertLess(ordered_sigs.index('s2'), ordered_sigs.index('s1'))
  100. self.assertLess(ordered_sigs.index('s4'), ordered_sigs.index('s1'))
  101. self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s2'))
  102. self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s4'))
  103. # Implicit dependencies
  104. self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s1'))
  105. def test_circular_dependencies(self):
  106. raw = [
  107. ('s1', ('s1_db', ['alpha'])),
  108. ('s2', ('s2_db', ['bravo'])),
  109. ]
  110. dependencies = {
  111. 'bravo': ['alpha'],
  112. 'alpha': ['bravo'],
  113. }
  114. self.assertRaises(ImproperlyConfigured, simple.dependency_ordered, raw, dependencies=dependencies)
  115. class MockTestRunner(object):
  116. invoked = False
  117. def __init__(self, *args, **kwargs):
  118. pass
  119. def run_tests(self, test_labels, extra_tests=None, **kwargs):
  120. MockTestRunner.invoked = True
  121. class ManageCommandTests(unittest.TestCase):
  122. def test_custom_test_runner(self):
  123. call_command('test', 'sites',
  124. testrunner='regressiontests.test_runner.tests.MockTestRunner')
  125. self.assertTrue(MockTestRunner.invoked,
  126. "The custom test runner has not been invoked")
  127. class CustomOptionsTestRunner(simple.DjangoTestSuiteRunner):
  128. option_list = (
  129. make_option('--option_a','-a', action='store', dest='option_a', default='1'),
  130. make_option('--option_b','-b', action='store', dest='option_b', default='2'),
  131. make_option('--option_c','-c', action='store', dest='option_c', default='3'),
  132. )
  133. def __init__(self, verbosity=1, interactive=True, failfast=True, option_a=None, option_b=None, option_c=None, **kwargs):
  134. super(CustomOptionsTestRunner, self).__init__(verbosity=verbosity, interactive=interactive,
  135. failfast=failfast)
  136. self.option_a = option_a
  137. self.option_b = option_b
  138. self.option_c = option_c
  139. def run_tests(self, test_labels, extra_tests=None, **kwargs):
  140. print "%s:%s:%s" % (self.option_a, self.option_b, self.option_c)
  141. class CustomTestRunnerOptionsTests(AdminScriptTestCase):
  142. def setUp(self):
  143. settings = {
  144. 'TEST_RUNNER': '\'regressiontests.test_runner.tests.CustomOptionsTestRunner\'',
  145. }
  146. self.write_settings('settings.py', sdict=settings)
  147. def tearDown(self):
  148. self.remove_settings('settings.py')
  149. def test_default_options(self):
  150. args = ['test', '--settings=regressiontests.settings']
  151. out, err = self.run_django_admin(args)
  152. self.assertNoOutput(err)
  153. self.assertOutput(out, '1:2:3')
  154. def test_default_and_given_options(self):
  155. args = ['test', '--settings=regressiontests.settings', '--option_b=foo']
  156. out, err = self.run_django_admin(args)
  157. self.assertNoOutput(err)
  158. self.assertOutput(out, '1:foo:3')
  159. def test_option_name_and_value_separated(self):
  160. args = ['test', '--settings=regressiontests.settings', '--option_b', 'foo']
  161. out, err = self.run_django_admin(args)
  162. self.assertNoOutput(err)
  163. self.assertOutput(out, '1:foo:3')
  164. def test_all_options_given(self):
  165. args = ['test', '--settings=regressiontests.settings', '--option_a=bar', '--option_b=foo', '--option_c=31337']
  166. out, err = self.run_django_admin(args)
  167. self.assertNoOutput(err)
  168. self.assertOutput(out, 'bar:foo:31337')
  169. class Ticket16885RegressionTests(unittest.TestCase):
  170. def test_ticket_16885(self):
  171. """Features are also confirmed on mirrored databases."""
  172. from django import db
  173. old_db_connections = db.connections
  174. try:
  175. db.connections = db.ConnectionHandler({
  176. 'default': {
  177. 'ENGINE': 'django.db.backends.sqlite3',
  178. },
  179. 'slave': {
  180. 'ENGINE': 'django.db.backends.sqlite3',
  181. 'TEST_MIRROR': 'default',
  182. },
  183. })
  184. slave = db.connections['slave']
  185. self.assertEqual(slave.features.supports_transactions, None)
  186. DjangoTestSuiteRunner(verbosity=0).setup_databases()
  187. self.assertNotEqual(slave.features.supports_transactions, None)
  188. finally:
  189. db.connections = old_db_connections
  190. class Ticket17477RegressionTests(AdminScriptTestCase):
  191. def setUp(self):
  192. self.write_settings('settings.py')
  193. def tearDown(self):
  194. self.remove_settings('settings.py')
  195. def test_ticket_17477(self):
  196. """'manage.py help test' works after r16352."""
  197. args = ['help', 'test']
  198. out, err = self.run_manage(args)
  199. self.assertNoOutput(err)
  200. class ModulesTestsPackages(unittest.TestCase):
  201. def test_get_tests(self):
  202. "Check that the get_tests helper function can find tests in a directory"
  203. module = import_module(TEST_APP_OK)
  204. tests = get_tests(module)
  205. self.assertIsInstance(tests, type(module))
  206. def test_import_error(self):
  207. "Test for #12658 - Tests with ImportError's shouldn't fail silently"
  208. module = import_module(TEST_APP_ERROR)
  209. self.assertRaises(ImportError, get_tests, module)