forked from django/django
/
test_discover_runner.py
82 lines (60 loc) · 2.54 KB
/
test_discover_runner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from contextlib import contextmanager
import os
from django.test import TestCase
from django.test.runner import DiscoverRunner
class DiscoverRunnerTest(TestCase):
def test_dotted_test_module(self):
count = DiscoverRunner().build_suite(
["test_discovery_sample.tests_sample"],
).countTestCases()
self.assertEqual(count, 3)
def test_dotted_test_class_vanilla_unittest(self):
count = DiscoverRunner().build_suite(
["test_discovery_sample.tests_sample.TestVanillaUnittest"],
).countTestCases()
self.assertEqual(count, 1)
def test_dotted_test_class_unittest2(self):
count = DiscoverRunner().build_suite(
["test_discovery_sample.tests_sample.TestUnittest2"],
).countTestCases()
self.assertEqual(count, 1)
def test_dotted_test_class_django_testcase(self):
count = DiscoverRunner().build_suite(
["test_discovery_sample.tests_sample.TestDjangoTestCase"],
).countTestCases()
self.assertEqual(count, 1)
def test_dotted_test_method_vanilla_unittest(self):
count = DiscoverRunner().build_suite(
["test_discovery_sample.tests_sample.TestVanillaUnittest.test_sample"],
).countTestCases()
self.assertEqual(count, 1)
def test_dotted_test_method_unittest2(self):
count = DiscoverRunner().build_suite(
["test_discovery_sample.tests_sample.TestUnittest2.test_sample"],
).countTestCases()
self.assertEqual(count, 1)
def test_dotted_test_method_django_testcase(self):
count = DiscoverRunner().build_suite(
["test_discovery_sample.tests_sample.TestDjangoTestCase.test_sample"],
).countTestCases()
self.assertEqual(count, 1)
def test_pattern(self):
count = DiscoverRunner(
pattern="*_tests.py",
).build_suite(["test_discovery_sample"]).countTestCases()
self.assertEqual(count, 1)
def test_file_path(self):
@contextmanager
def change_cwd_to_tests():
"""Change CWD to tests directory (one level up from this file)"""
current_dir = os.path.abspath(os.path.dirname(__file__))
tests_dir = os.path.join(current_dir, '..')
old_cwd = os.getcwd()
os.chdir(tests_dir)
yield
os.chdir(old_cwd)
with change_cwd_to_tests():
count = DiscoverRunner().build_suite(
["test_discovery_sample/"],
).countTestCases()
self.assertEqual(count, 4)