2009-12-14 01:08:36 +08:00
|
|
|
"""
|
|
|
|
Tests for django test runner
|
|
|
|
"""
|
|
|
|
import StringIO
|
2011-06-10 16:26:05 +08:00
|
|
|
from optparse import make_option
|
2011-04-02 21:26:39 +08:00
|
|
|
import warnings
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2010-12-05 08:44:34 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2011-06-10 16:26:05 +08:00
|
|
|
from django.core.management import call_command
|
2009-12-22 14:00:57 +08:00
|
|
|
from django.test import simple
|
2011-06-12 20:34:10 +08:00
|
|
|
from django.test.simple import get_tests
|
2011-04-02 21:26:39 +08:00
|
|
|
from django.test.utils import get_warnings_state, restore_warnings_state
|
2010-10-11 20:55:17 +08:00
|
|
|
from django.utils import unittest
|
2011-06-12 20:34:10 +08:00
|
|
|
from django.utils.importlib import import_module
|
|
|
|
|
2011-06-10 16:26:05 +08:00
|
|
|
from regressiontests.admin_scripts.tests import AdminScriptTestCase
|
2009-12-22 14:00:57 +08:00
|
|
|
|
2011-04-02 21:26:39 +08:00
|
|
|
|
2011-06-12 20:34:10 +08:00
|
|
|
TEST_APP_OK = 'regressiontests.test_runner.valid_app.models'
|
|
|
|
TEST_APP_ERROR = 'regressiontests.test_runner.invalid_app.models'
|
|
|
|
|
|
|
|
|
2009-12-22 14:00:57 +08:00
|
|
|
class DjangoTestRunnerTests(unittest.TestCase):
|
2011-04-02 21:26:39 +08:00
|
|
|
def setUp(self):
|
|
|
|
self._warnings_state = get_warnings_state()
|
|
|
|
warnings.filterwarnings('ignore', category=DeprecationWarning,
|
|
|
|
module='django.test.simple')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
restore_warnings_state(self._warnings_state)
|
2009-12-14 01:08:36 +08:00
|
|
|
|
|
|
|
def test_failfast(self):
|
2009-12-22 14:00:57 +08:00
|
|
|
class MockTestOne(unittest.TestCase):
|
2009-12-14 01:08:36 +08:00
|
|
|
def runTest(self):
|
|
|
|
assert False
|
2009-12-22 14:00:57 +08:00
|
|
|
class MockTestTwo(unittest.TestCase):
|
2009-12-14 01:08:36 +08:00
|
|
|
def runTest(self):
|
|
|
|
assert False
|
2009-12-22 14:00:57 +08:00
|
|
|
|
2009-12-14 01:08:36 +08:00
|
|
|
suite = unittest.TestSuite([MockTestOne(), MockTestTwo()])
|
|
|
|
mock_stream = StringIO.StringIO()
|
|
|
|
dtr = simple.DjangoTestRunner(verbosity=0, failfast=False, stream=mock_stream)
|
|
|
|
result = dtr.run(suite)
|
|
|
|
self.assertEqual(2, result.testsRun)
|
|
|
|
self.assertEqual(2, len(result.failures))
|
2009-12-22 14:00:57 +08:00
|
|
|
|
2009-12-14 01:08:36 +08:00
|
|
|
dtr = simple.DjangoTestRunner(verbosity=0, failfast=True, stream=mock_stream)
|
|
|
|
result = dtr.run(suite)
|
|
|
|
self.assertEqual(1, result.testsRun)
|
|
|
|
self.assertEqual(1, len(result.failures))
|
2010-12-05 08:44:34 +08:00
|
|
|
|
|
|
|
class DependencyOrderingTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_simple_dependencies(self):
|
|
|
|
raw = [
|
2011-02-02 22:02:14 +08:00
|
|
|
('s1', ('s1_db', ['alpha'])),
|
|
|
|
('s2', ('s2_db', ['bravo'])),
|
|
|
|
('s3', ('s3_db', ['charlie'])),
|
2010-12-05 08:44:34 +08:00
|
|
|
]
|
|
|
|
dependencies = {
|
|
|
|
'alpha': ['charlie'],
|
|
|
|
'bravo': ['charlie'],
|
|
|
|
}
|
|
|
|
|
|
|
|
ordered = simple.dependency_ordered(raw, dependencies=dependencies)
|
2011-02-02 22:02:14 +08:00
|
|
|
ordered_sigs = [sig for sig,value in ordered]
|
2010-12-05 08:44:34 +08:00
|
|
|
|
|
|
|
self.assertIn('s1', ordered_sigs)
|
|
|
|
self.assertIn('s2', ordered_sigs)
|
|
|
|
self.assertIn('s3', ordered_sigs)
|
|
|
|
self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s1'))
|
|
|
|
self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s2'))
|
|
|
|
|
|
|
|
def test_chained_dependencies(self):
|
|
|
|
raw = [
|
2011-02-02 22:02:14 +08:00
|
|
|
('s1', ('s1_db', ['alpha'])),
|
|
|
|
('s2', ('s2_db', ['bravo'])),
|
|
|
|
('s3', ('s3_db', ['charlie'])),
|
2010-12-05 08:44:34 +08:00
|
|
|
]
|
|
|
|
dependencies = {
|
|
|
|
'alpha': ['bravo'],
|
|
|
|
'bravo': ['charlie'],
|
|
|
|
}
|
|
|
|
|
|
|
|
ordered = simple.dependency_ordered(raw, dependencies=dependencies)
|
2011-02-02 22:02:14 +08:00
|
|
|
ordered_sigs = [sig for sig,value in ordered]
|
2010-12-05 08:44:34 +08:00
|
|
|
|
|
|
|
self.assertIn('s1', ordered_sigs)
|
|
|
|
self.assertIn('s2', ordered_sigs)
|
|
|
|
self.assertIn('s3', ordered_sigs)
|
|
|
|
|
|
|
|
# Explicit dependencies
|
|
|
|
self.assertLess(ordered_sigs.index('s2'), ordered_sigs.index('s1'))
|
|
|
|
self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s2'))
|
|
|
|
|
|
|
|
# Implied dependencies
|
|
|
|
self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s1'))
|
|
|
|
|
|
|
|
def test_multiple_dependencies(self):
|
|
|
|
raw = [
|
2011-02-02 22:02:14 +08:00
|
|
|
('s1', ('s1_db', ['alpha'])),
|
|
|
|
('s2', ('s2_db', ['bravo'])),
|
|
|
|
('s3', ('s3_db', ['charlie'])),
|
|
|
|
('s4', ('s4_db', ['delta'])),
|
2010-12-05 08:44:34 +08:00
|
|
|
]
|
|
|
|
dependencies = {
|
|
|
|
'alpha': ['bravo','delta'],
|
|
|
|
'bravo': ['charlie'],
|
|
|
|
'delta': ['charlie'],
|
|
|
|
}
|
|
|
|
|
|
|
|
ordered = simple.dependency_ordered(raw, dependencies=dependencies)
|
|
|
|
ordered_sigs = [sig for sig,aliases in ordered]
|
|
|
|
|
|
|
|
self.assertIn('s1', ordered_sigs)
|
|
|
|
self.assertIn('s2', ordered_sigs)
|
|
|
|
self.assertIn('s3', ordered_sigs)
|
|
|
|
self.assertIn('s4', ordered_sigs)
|
|
|
|
|
|
|
|
# Explicit dependencies
|
|
|
|
self.assertLess(ordered_sigs.index('s2'), ordered_sigs.index('s1'))
|
|
|
|
self.assertLess(ordered_sigs.index('s4'), ordered_sigs.index('s1'))
|
|
|
|
self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s2'))
|
|
|
|
self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s4'))
|
|
|
|
|
|
|
|
# Implicit dependencies
|
|
|
|
self.assertLess(ordered_sigs.index('s3'), ordered_sigs.index('s1'))
|
|
|
|
|
|
|
|
def test_circular_dependencies(self):
|
|
|
|
raw = [
|
2011-02-02 22:02:14 +08:00
|
|
|
('s1', ('s1_db', ['alpha'])),
|
|
|
|
('s2', ('s2_db', ['bravo'])),
|
2010-12-05 08:44:34 +08:00
|
|
|
]
|
|
|
|
dependencies = {
|
|
|
|
'bravo': ['alpha'],
|
|
|
|
'alpha': ['bravo'],
|
|
|
|
}
|
|
|
|
|
|
|
|
self.assertRaises(ImproperlyConfigured, simple.dependency_ordered, raw, dependencies=dependencies)
|
|
|
|
|
2011-06-10 16:26:05 +08:00
|
|
|
|
|
|
|
class MockTestRunner(object):
|
|
|
|
invoked = False
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def run_tests(self, test_labels, extra_tests=None, **kwargs):
|
|
|
|
MockTestRunner.invoked = True
|
|
|
|
|
|
|
|
|
|
|
|
class ManageCommandTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_custom_test_runner(self):
|
|
|
|
call_command('test', 'sites',
|
|
|
|
testrunner='regressiontests.test_runner.tests.MockTestRunner')
|
|
|
|
self.assertTrue(MockTestRunner.invoked,
|
|
|
|
"The custom test runner has not been invoked")
|
|
|
|
|
|
|
|
|
|
|
|
class CustomOptionsTestRunner(simple.DjangoTestSuiteRunner):
|
|
|
|
option_list = (
|
|
|
|
make_option('--option_a','-a', action='store', dest='option_a', default='1'),
|
|
|
|
make_option('--option_b','-b', action='store', dest='option_b', default='2'),
|
|
|
|
make_option('--option_c','-c', action='store', dest='option_c', default='3'),
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, verbosity=1, interactive=True, failfast=True, option_a=None, option_b=None, option_c=None, **kwargs):
|
|
|
|
super(CustomOptionsTestRunner, self).__init__(verbosity=verbosity, interactive=interactive,
|
|
|
|
failfast=failfast)
|
|
|
|
self.option_a = option_a
|
|
|
|
self.option_b = option_b
|
|
|
|
self.option_c = option_c
|
|
|
|
|
|
|
|
def run_tests(self, test_labels, extra_tests=None, **kwargs):
|
|
|
|
print "%s:%s:%s" % (self.option_a, self.option_b, self.option_c)
|
|
|
|
|
|
|
|
|
|
|
|
class CustomTestRunnerOptionsTests(AdminScriptTestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
settings = {
|
|
|
|
'TEST_RUNNER': '\'regressiontests.test_runner.tests.CustomOptionsTestRunner\'',
|
|
|
|
}
|
|
|
|
self.write_settings('settings.py', sdict=settings)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.remove_settings('settings.py')
|
|
|
|
|
|
|
|
def test_default_options(self):
|
|
|
|
args = ['test', '--settings=settings']
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertOutput(out, '1:2:3')
|
|
|
|
|
|
|
|
def test_default_and_given_options(self):
|
|
|
|
args = ['test', '--settings=settings', '--option_b=foo']
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertOutput(out, '1:foo:3')
|
|
|
|
|
|
|
|
def test_option_name_and_value_separated(self):
|
|
|
|
args = ['test', '--settings=settings', '--option_b', 'foo']
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertOutput(out, '1:foo:3')
|
|
|
|
|
|
|
|
def test_all_options_given(self):
|
|
|
|
args = ['test', '--settings=settings', '--option_a=bar', '--option_b=foo', '--option_c=31337']
|
|
|
|
out, err = self.run_django_admin(args)
|
|
|
|
self.assertNoOutput(err)
|
|
|
|
self.assertOutput(out, 'bar:foo:31337')
|
2011-06-12 20:34:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ModulesTestsPackages(unittest.TestCase):
|
|
|
|
def test_get_tests(self):
|
|
|
|
"Check that the get_tests helper function can find tests in a directory"
|
|
|
|
module = import_module(TEST_APP_OK)
|
|
|
|
tests = get_tests(module)
|
|
|
|
self.assertIsInstance(tests, type(module))
|
|
|
|
|
|
|
|
def test_import_error(self):
|
|
|
|
"Test for #12658 - Tests with ImportError's shouldn't fail silently"
|
|
|
|
module = import_module(TEST_APP_ERROR)
|
|
|
|
self.assertRaises(ImportError, get_tests, module)
|