-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_parameterizabletests.py
155 lines (137 loc) · 5.44 KB
/
test_parameterizabletests.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import unittest
from parameterizabletests import parameterizable, parameters, C
class TestParaterizableTests(unittest.TestCase):
def runTest(self, testcase, testname):
res = unittest.TestResult()
testcase(methodName=testname).run(res)
self.assertEqual(1, res.testsRun)
self.assertEqual([], res.failures)
self.assertEqual([], res.errors)
self.assertTrue(res.wasSuccessful())
def test_normal_tests_run(self):
res = []
@parameterizable
class Test(unittest.TestCase):
def test_normal_tests_run(self):
res.append(1)
@parameters()
def test_foo(self):
raise Exception("This should not be run in this test")
self.runTest(Test, 'test_normal_tests_run')
def test_parameterizable(self):
@parameterizable
class Test(unittest.TestCase):
@parameters()
def test_foo(self):
raise Exception("This should not be run in this test")
with self.assertRaises(ValueError):
Test(methodName='test_foo').run()
def test_single_arg_parameters(self):
res = []
@parameterizable
class Test(unittest.TestCase):
@parameters(C(1), C(2))
def test_foo(self, arg):
res.append(arg)
self.runTest(Test, 'test_foo_1')
self.assertEqual([1], res)
self.runTest(Test, 'test_foo_2')
self.assertEqual([1, 2], res)
with self.assertRaises(ValueError):
Test(methodName='test_foo_3').run()
def test_multiple_arg_parameters(self):
res = []
@parameterizable
class Test(unittest.TestCase):
@parameters(C(1, 7), C(2, 3))
def test_foo(self, *args):
res.append(args)
self.runTest(Test, 'test_foo_1_7')
self.assertEqual([(1, 7)], res)
self.runTest(Test, 'test_foo_2_3')
self.assertEqual([(1, 7), (2, 3)], res)
def test_list_of_Cs(self):
res = []
@parameterizable
class Test(unittest.TestCase):
@parameters(C(1, b=7), C(2, c=3), C(a=1, b=2, c=3), C(4, 5, 6))
def test_foo(self, a, b=100, c=200):
res.append((a, b, c))
self.runTest(Test, 'test_foo_1__b_7')
self.runTest(Test, 'test_foo_2__c_3')
self.runTest(Test, 'test_foo_4_5_6')
self.assertEqual([(1, 7, 200), (2, 100, 3), (4, 5, 6)], res)
# XXX This will not pass consistently on 3.3-3.5, so don't run it.
#self.runTest(Test, 'test_foo_a_1__b_2__c_3')
#self.assertEqual([(1, 7, 200), (2, 100, 3), (4, 5, 6), (1, 2, 3)], res)
def test_keyword_parameters(self):
res = []
@parameterizable
class Test(unittest.TestCase):
@parameters(foo=C(a=1, b=2), bar=C(b=7))
def test_foo(self, a=None, b=None):
res.append((a, b))
self.runTest(Test, 'test_foo_foo')
self.runTest(Test, 'test_foo_bar')
self.assertEqual([(1, 2), (None, 7)], res)
def test_dict_parameters_as_single_arg(self):
res = []
@parameterizable
class Test(unittest.TestCase):
@parameters(dict(foo=C(a=1, b=2), bar=C(b=7)))
def test_foo(self, a=None, b=None):
res.append((a, b))
self.runTest(Test, 'test_foo_foo')
self.runTest(Test, 'test_foo_bar')
self.assertEqual([(1, 2), (None, 7)], res)
def test__include_key(self):
res = []
@parameterizable
class Test(unittest.TestCase):
@parameters(a=C(1, 7), b=C(2, 8), _include_key=True)
def test_bar(self, *args):
res.append(args)
self.runTest(Test, 'test_bar_a')
self.runTest(Test, 'test_bar_b')
self.assertEqual([('a', 1, 7), ('b', 2, 8)], res)
def test_mixed_positionals_and_keywords(self):
res = []
@parameterizable
class Test(unittest.TestCase):
@parameters(C(1), b=C(1, k=3))
def test_bar(self, z, k=None):
res.append((z, k))
self.runTest(Test, 'test_bar_1')
self.runTest(Test, 'test_bar_b')
self.assertEqual([(1, None), (1, 3)], res)
def test_invalid_setting_raises(self):
with self.assertRaises(TypeError):
@parameterizable
class Test(unittest.TestCase):
@parameters(a=1, b=2, _nosuch_setting=True)
def test_bar(self, *args):
pass
def test_duplicate_test_names_raises(self):
with self.assertRaises(NameError):
@parameterizable
class Test(unittest.TestCase):
# Both of these would be named 'test_a'
@parameters(C('a'), a=C(1))
def test_bar(self, *args):
pass
def test_using_include_key_multiple_times_works(self):
res = []
@parameterizable
class Test(unittest.TestCase):
params = dict(a=C(1), b=C(2))
@parameters(params, _include_key=True)
def test_foo(self, name, val):
res.append(('foo', name, val))
@parameters(params, _include_key=True)
def test_bar(self, name, val):
res.append(('bar', name, val))
self.runTest(Test, 'test_foo_a')
self.runTest(Test, 'test_bar_b')
self.assertEqual([('foo', 'a', 1), ('bar', 'b', 2)], res)
if __name__=='__main__':
unittest.main()