2013-05-20 05:58:06 +02:00
|
|
|
import os
|
2016-08-10 19:46:54 +02:00
|
|
|
from argparse import ArgumentParser
|
2015-01-28 13:35:27 +01:00
|
|
|
from contextlib import contextmanager
|
2013-12-31 21:47:14 +01:00
|
|
|
from unittest import TestSuite, TextTestRunner, defaultTestLoader
|
2013-05-20 05:58:06 +02:00
|
|
|
|
2018-07-12 06:14:24 +02:00
|
|
|
from django.db import connections
|
2018-11-26 20:05:02 +01:00
|
|
|
from django.test import SimpleTestCase
|
2013-05-11 05:08:45 +02:00
|
|
|
from django.test.runner import DiscoverRunner
|
2018-02-09 14:09:52 +01:00
|
|
|
from django.test.utils import captured_stdout
|
2013-05-20 07:56:23 +02:00
|
|
|
|
|
|
|
|
2013-12-31 18:41:34 +01:00
|
|
|
@contextmanager
|
|
|
|
def change_cwd(directory):
|
|
|
|
current_dir = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
new_dir = os.path.join(current_dir, directory)
|
|
|
|
old_cwd = os.getcwd()
|
|
|
|
os.chdir(new_dir)
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
os.chdir(old_cwd)
|
|
|
|
|
|
|
|
|
2018-11-26 20:05:02 +01:00
|
|
|
class DiscoverRunnerTests(SimpleTestCase):
|
2013-05-11 05:08:45 +02:00
|
|
|
|
2016-08-09 10:40:40 +02:00
|
|
|
def test_init_debug_mode(self):
|
|
|
|
runner = DiscoverRunner()
|
|
|
|
self.assertFalse(runner.debug_mode)
|
|
|
|
|
2016-08-10 19:46:54 +02:00
|
|
|
def test_add_arguments_debug_mode(self):
|
|
|
|
parser = ArgumentParser()
|
|
|
|
DiscoverRunner.add_arguments(parser)
|
|
|
|
|
|
|
|
ns = parser.parse_args([])
|
|
|
|
self.assertFalse(ns.debug_mode)
|
|
|
|
ns = parser.parse_args(["--debug-mode"])
|
|
|
|
self.assertTrue(ns.debug_mode)
|
|
|
|
|
2013-05-11 05:08:45 +02:00
|
|
|
def test_dotted_test_module(self):
|
|
|
|
count = DiscoverRunner().build_suite(
|
2018-01-04 20:41:38 +01:00
|
|
|
['test_runner_apps.sample.tests_sample'],
|
2013-05-11 05:08:45 +02:00
|
|
|
).countTestCases()
|
|
|
|
|
2018-01-04 20:41:38 +01:00
|
|
|
self.assertEqual(count, 4)
|
2013-05-11 05:08:45 +02:00
|
|
|
|
|
|
|
def test_dotted_test_class_vanilla_unittest(self):
|
|
|
|
count = DiscoverRunner().build_suite(
|
2018-01-04 20:41:38 +01:00
|
|
|
['test_runner_apps.sample.tests_sample.TestVanillaUnittest'],
|
2013-05-11 05:08:45 +02:00
|
|
|
).countTestCases()
|
|
|
|
|
|
|
|
self.assertEqual(count, 1)
|
|
|
|
|
|
|
|
def test_dotted_test_class_django_testcase(self):
|
|
|
|
count = DiscoverRunner().build_suite(
|
2018-01-04 20:41:38 +01:00
|
|
|
['test_runner_apps.sample.tests_sample.TestDjangoTestCase'],
|
2013-05-11 05:08:45 +02:00
|
|
|
).countTestCases()
|
|
|
|
|
|
|
|
self.assertEqual(count, 1)
|
|
|
|
|
|
|
|
def test_dotted_test_method_django_testcase(self):
|
|
|
|
count = DiscoverRunner().build_suite(
|
2018-01-04 20:41:38 +01:00
|
|
|
['test_runner_apps.sample.tests_sample.TestDjangoTestCase.test_sample'],
|
2013-05-11 05:08:45 +02:00
|
|
|
).countTestCases()
|
|
|
|
|
|
|
|
self.assertEqual(count, 1)
|
|
|
|
|
|
|
|
def test_pattern(self):
|
|
|
|
count = DiscoverRunner(
|
|
|
|
pattern="*_tests.py",
|
2018-01-04 20:41:38 +01:00
|
|
|
).build_suite(['test_runner_apps.sample']).countTestCases()
|
2013-05-11 05:08:45 +02:00
|
|
|
|
|
|
|
self.assertEqual(count, 1)
|
|
|
|
|
|
|
|
def test_file_path(self):
|
2013-12-31 18:41:34 +01:00
|
|
|
with change_cwd(".."):
|
2013-05-20 05:58:06 +02:00
|
|
|
count = DiscoverRunner().build_suite(
|
2018-01-04 20:41:38 +01:00
|
|
|
['test_runner_apps/sample/'],
|
2013-05-20 05:58:06 +02:00
|
|
|
).countTestCases()
|
2013-05-11 05:08:45 +02:00
|
|
|
|
2018-01-04 20:41:38 +01:00
|
|
|
self.assertEqual(count, 5)
|
2013-09-10 15:49:39 +02:00
|
|
|
|
2013-12-31 18:41:34 +01:00
|
|
|
def test_empty_label(self):
|
|
|
|
"""
|
|
|
|
If the test label is empty, discovery should happen on the current
|
|
|
|
working directory.
|
|
|
|
"""
|
|
|
|
with change_cwd("."):
|
|
|
|
suite = DiscoverRunner().build_suite([])
|
|
|
|
self.assertEqual(
|
|
|
|
suite._tests[0].id().split(".")[0],
|
|
|
|
os.path.basename(os.getcwd()),
|
|
|
|
)
|
|
|
|
|
2013-12-16 17:04:28 +01:00
|
|
|
def test_empty_test_case(self):
|
|
|
|
count = DiscoverRunner().build_suite(
|
2018-01-04 20:41:38 +01:00
|
|
|
['test_runner_apps.sample.tests_sample.EmptyTestCase'],
|
2013-12-16 17:04:28 +01:00
|
|
|
).countTestCases()
|
|
|
|
|
|
|
|
self.assertEqual(count, 0)
|
|
|
|
|
|
|
|
def test_discovery_on_package(self):
|
|
|
|
count = DiscoverRunner().build_suite(
|
2018-01-04 20:41:38 +01:00
|
|
|
['test_runner_apps.sample.tests'],
|
2013-12-16 17:04:28 +01:00
|
|
|
).countTestCases()
|
|
|
|
|
|
|
|
self.assertEqual(count, 1)
|
|
|
|
|
|
|
|
def test_ignore_adjacent(self):
|
|
|
|
"""
|
|
|
|
When given a dotted path to a module, unittest discovery searches
|
|
|
|
not just the module, but also the directory containing the module.
|
|
|
|
|
|
|
|
This results in tests from adjacent modules being run when they
|
|
|
|
should not. The discover runner avoids this behavior.
|
|
|
|
"""
|
|
|
|
count = DiscoverRunner().build_suite(
|
2018-01-04 20:41:38 +01:00
|
|
|
['test_runner_apps.sample.empty'],
|
2013-12-16 17:04:28 +01:00
|
|
|
).countTestCases()
|
|
|
|
|
|
|
|
self.assertEqual(count, 0)
|
|
|
|
|
2014-04-12 11:42:06 +02:00
|
|
|
def test_testcase_ordering(self):
|
2014-05-16 12:11:35 +02:00
|
|
|
with change_cwd(".."):
|
2018-01-04 20:41:38 +01:00
|
|
|
suite = DiscoverRunner().build_suite(['test_runner_apps/sample/'])
|
2014-05-16 12:11:35 +02:00
|
|
|
self.assertEqual(
|
|
|
|
suite._tests[0].__class__.__name__,
|
|
|
|
'TestDjangoTestCase',
|
|
|
|
msg="TestDjangoTestCase should be the first test case")
|
|
|
|
self.assertEqual(
|
|
|
|
suite._tests[1].__class__.__name__,
|
|
|
|
'TestZimpleTestCase',
|
|
|
|
msg="TestZimpleTestCase should be the second test case")
|
|
|
|
# All others can follow in unspecified order, including doctests
|
|
|
|
self.assertIn('DocTestCase', [t.__class__.__name__ for t in suite._tests[2:]])
|
2014-04-12 11:42:06 +02:00
|
|
|
|
2014-10-23 20:15:41 +02:00
|
|
|
def test_duplicates_ignored(self):
|
|
|
|
"""
|
|
|
|
Tests shouldn't be discovered twice when discovering on overlapping paths.
|
|
|
|
"""
|
2017-05-03 03:27:11 +02:00
|
|
|
base_app = 'forms_tests'
|
|
|
|
sub_app = 'forms_tests.field_tests'
|
2015-07-26 03:15:50 +02:00
|
|
|
with self.modify_settings(INSTALLED_APPS={'append': sub_app}):
|
|
|
|
single = DiscoverRunner().build_suite([base_app]).countTestCases()
|
|
|
|
dups = DiscoverRunner().build_suite([base_app, sub_app]).countTestCases()
|
2014-10-23 20:15:41 +02:00
|
|
|
self.assertEqual(single, dups)
|
|
|
|
|
2014-11-22 17:59:05 +01:00
|
|
|
def test_reverse(self):
|
|
|
|
"""
|
|
|
|
Reverse should reorder tests while maintaining the grouping specified
|
|
|
|
by ``DiscoverRunner.reorder_by``.
|
|
|
|
"""
|
|
|
|
runner = DiscoverRunner(reverse=True)
|
|
|
|
suite = runner.build_suite(
|
2018-01-04 20:41:38 +01:00
|
|
|
test_labels=('test_runner_apps.sample', 'test_runner_apps.simple'))
|
|
|
|
self.assertIn('test_runner_apps.simple', next(iter(suite)).id(),
|
2014-11-22 17:59:05 +01:00
|
|
|
msg="Test labels should be reversed.")
|
2018-01-04 20:41:38 +01:00
|
|
|
suite = runner.build_suite(test_labels=('test_runner_apps.simple',))
|
2014-11-22 17:59:05 +01:00
|
|
|
suite = tuple(suite)
|
|
|
|
self.assertIn('DjangoCase', suite[0].id(),
|
|
|
|
msg="Test groups should not be reversed.")
|
|
|
|
self.assertIn('SimpleCase', suite[4].id(),
|
|
|
|
msg="Test groups order should be preserved.")
|
|
|
|
self.assertIn('DjangoCase2', suite[0].id(),
|
|
|
|
msg="Django test cases should be reversed.")
|
|
|
|
self.assertIn('SimpleCase2', suite[4].id(),
|
|
|
|
msg="Simple test cases should be reversed.")
|
|
|
|
self.assertIn('UnittestCase2', suite[8].id(),
|
|
|
|
msg="Unittest test cases should be reversed.")
|
|
|
|
self.assertIn('test_2', suite[0].id(),
|
|
|
|
msg="Methods of Django cases should be reversed.")
|
|
|
|
self.assertIn('test_2', suite[4].id(),
|
|
|
|
msg="Methods of simple cases should be reversed.")
|
|
|
|
self.assertIn('test_2', suite[8].id(),
|
|
|
|
msg="Methods of unittest cases should be reversed.")
|
|
|
|
|
2016-08-04 19:26:21 +02:00
|
|
|
def test_overridable_get_test_runner_kwargs(self):
|
|
|
|
self.assertIsInstance(DiscoverRunner().get_test_runner_kwargs(), dict)
|
|
|
|
|
2015-12-03 00:55:50 +01:00
|
|
|
def test_overridable_test_suite(self):
|
2013-09-10 15:49:39 +02:00
|
|
|
self.assertEqual(DiscoverRunner().test_suite, TestSuite)
|
|
|
|
|
2015-12-03 00:55:50 +01:00
|
|
|
def test_overridable_test_runner(self):
|
2013-09-10 15:49:39 +02:00
|
|
|
self.assertEqual(DiscoverRunner().test_runner, TextTestRunner)
|
|
|
|
|
2015-12-03 00:55:50 +01:00
|
|
|
def test_overridable_test_loader(self):
|
2013-09-10 15:49:39 +02:00
|
|
|
self.assertEqual(DiscoverRunner().test_loader, defaultTestLoader)
|
2015-11-07 14:57:56 +01:00
|
|
|
|
|
|
|
def test_tags(self):
|
|
|
|
runner = DiscoverRunner(tags=['core'])
|
2018-01-04 20:41:38 +01:00
|
|
|
self.assertEqual(runner.build_suite(['test_runner_apps.tagged.tests']).countTestCases(), 1)
|
2015-11-07 14:57:56 +01:00
|
|
|
runner = DiscoverRunner(tags=['fast'])
|
2018-01-04 20:41:38 +01:00
|
|
|
self.assertEqual(runner.build_suite(['test_runner_apps.tagged.tests']).countTestCases(), 2)
|
2015-11-07 14:57:56 +01:00
|
|
|
runner = DiscoverRunner(tags=['slow'])
|
2018-01-04 20:41:38 +01:00
|
|
|
self.assertEqual(runner.build_suite(['test_runner_apps.tagged.tests']).countTestCases(), 2)
|
2015-11-07 14:57:56 +01:00
|
|
|
|
|
|
|
def test_exclude_tags(self):
|
|
|
|
runner = DiscoverRunner(tags=['fast'], exclude_tags=['core'])
|
2018-01-04 20:41:38 +01:00
|
|
|
self.assertEqual(runner.build_suite(['test_runner_apps.tagged.tests']).countTestCases(), 1)
|
2015-11-07 14:57:56 +01:00
|
|
|
runner = DiscoverRunner(tags=['fast'], exclude_tags=['slow'])
|
2018-01-04 20:41:38 +01:00
|
|
|
self.assertEqual(runner.build_suite(['test_runner_apps.tagged.tests']).countTestCases(), 0)
|
2015-11-07 14:57:56 +01:00
|
|
|
runner = DiscoverRunner(exclude_tags=['slow'])
|
2018-01-04 20:41:38 +01:00
|
|
|
self.assertEqual(runner.build_suite(['test_runner_apps.tagged.tests']).countTestCases(), 0)
|
2017-12-01 17:00:45 +01:00
|
|
|
|
|
|
|
def test_tag_inheritance(self):
|
|
|
|
def count_tests(**kwargs):
|
|
|
|
suite = DiscoverRunner(**kwargs).build_suite(['test_runner_apps.tagged.tests_inheritance'])
|
|
|
|
return suite.countTestCases()
|
|
|
|
|
|
|
|
self.assertEqual(count_tests(tags=['foo']), 4)
|
|
|
|
self.assertEqual(count_tests(tags=['bar']), 2)
|
|
|
|
self.assertEqual(count_tests(tags=['baz']), 2)
|
|
|
|
self.assertEqual(count_tests(tags=['foo'], exclude_tags=['bar']), 2)
|
|
|
|
self.assertEqual(count_tests(tags=['foo'], exclude_tags=['bar', 'baz']), 1)
|
|
|
|
self.assertEqual(count_tests(exclude_tags=['foo']), 0)
|
2018-02-09 14:09:52 +01:00
|
|
|
|
|
|
|
def test_included_tags_displayed(self):
|
|
|
|
runner = DiscoverRunner(tags=['foo', 'bar'], verbosity=2)
|
|
|
|
with captured_stdout() as stdout:
|
|
|
|
runner.build_suite(['test_runner_apps.tagged.tests'])
|
|
|
|
self.assertIn('Including test tag(s): bar, foo.\n', stdout.getvalue())
|
|
|
|
|
|
|
|
def test_excluded_tags_displayed(self):
|
|
|
|
runner = DiscoverRunner(exclude_tags=['foo', 'bar'], verbosity=3)
|
|
|
|
with captured_stdout() as stdout:
|
|
|
|
runner.build_suite(['test_runner_apps.tagged.tests'])
|
|
|
|
self.assertIn('Excluding test tag(s): bar, foo.\n', stdout.getvalue())
|
2018-07-12 06:14:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DiscoverRunnerGetDatabasesTests(SimpleTestCase):
|
|
|
|
runner = DiscoverRunner(verbosity=2)
|
|
|
|
skip_msg = 'Skipping setup of unused database(s): '
|
|
|
|
|
|
|
|
def get_databases(self, test_labels):
|
|
|
|
suite = self.runner.build_suite(test_labels)
|
|
|
|
with captured_stdout() as stdout:
|
|
|
|
databases = self.runner.get_databases(suite)
|
|
|
|
return databases, stdout.getvalue()
|
|
|
|
|
|
|
|
def test_mixed(self):
|
|
|
|
databases, output = self.get_databases(['test_runner_apps.databases.tests'])
|
|
|
|
self.assertEqual(databases, set(connections))
|
|
|
|
self.assertNotIn(self.skip_msg, output)
|
|
|
|
|
|
|
|
def test_all(self):
|
|
|
|
databases, output = self.get_databases(['test_runner_apps.databases.tests.AllDatabasesTests'])
|
|
|
|
self.assertEqual(databases, set(connections))
|
|
|
|
self.assertNotIn(self.skip_msg, output)
|
|
|
|
|
|
|
|
def test_default_and_other(self):
|
|
|
|
databases, output = self.get_databases([
|
|
|
|
'test_runner_apps.databases.tests.DefaultDatabaseTests',
|
|
|
|
'test_runner_apps.databases.tests.OtherDatabaseTests',
|
|
|
|
])
|
|
|
|
self.assertEqual(databases, set(connections))
|
|
|
|
self.assertNotIn(self.skip_msg, output)
|
|
|
|
|
|
|
|
def test_default_only(self):
|
|
|
|
databases, output = self.get_databases(['test_runner_apps.databases.tests.DefaultDatabaseTests'])
|
|
|
|
self.assertEqual(databases, {'default'})
|
|
|
|
self.assertIn(self.skip_msg + 'other', output)
|
|
|
|
|
|
|
|
def test_other_only(self):
|
|
|
|
databases, output = self.get_databases(['test_runner_apps.databases.tests.OtherDatabaseTests'])
|
|
|
|
self.assertEqual(databases, {'other'})
|
|
|
|
self.assertIn(self.skip_msg + 'default', output)
|
|
|
|
|
|
|
|
def test_no_databases_required(self):
|
|
|
|
databases, output = self.get_databases(['test_runner_apps.databases.tests.NoDatabaseTests'])
|
|
|
|
self.assertEqual(databases, set())
|
|
|
|
self.assertIn(self.skip_msg + 'default, other', output)
|