1 | n/a | import sys |
---|
2 | n/a | import types |
---|
3 | n/a | import warnings |
---|
4 | n/a | |
---|
5 | n/a | import unittest |
---|
6 | n/a | |
---|
7 | n/a | # Decorator used in the deprecation tests to reset the warning registry for |
---|
8 | n/a | # test isolation and reproducibility. |
---|
9 | n/a | def warningregistry(func): |
---|
10 | n/a | def wrapper(*args, **kws): |
---|
11 | n/a | missing = [] |
---|
12 | n/a | saved = getattr(warnings, '__warningregistry__', missing).copy() |
---|
13 | n/a | try: |
---|
14 | n/a | return func(*args, **kws) |
---|
15 | n/a | finally: |
---|
16 | n/a | if saved is missing: |
---|
17 | n/a | try: |
---|
18 | n/a | del warnings.__warningregistry__ |
---|
19 | n/a | except AttributeError: |
---|
20 | n/a | pass |
---|
21 | n/a | else: |
---|
22 | n/a | warnings.__warningregistry__ = saved |
---|
23 | n/a | return wrapper |
---|
24 | n/a | |
---|
25 | n/a | |
---|
26 | n/a | class Test_TestLoader(unittest.TestCase): |
---|
27 | n/a | |
---|
28 | n/a | ### Basic object tests |
---|
29 | n/a | ################################################################ |
---|
30 | n/a | |
---|
31 | n/a | def test___init__(self): |
---|
32 | n/a | loader = unittest.TestLoader() |
---|
33 | n/a | self.assertEqual([], loader.errors) |
---|
34 | n/a | |
---|
35 | n/a | ### Tests for TestLoader.loadTestsFromTestCase |
---|
36 | n/a | ################################################################ |
---|
37 | n/a | |
---|
38 | n/a | # "Return a suite of all test cases contained in the TestCase-derived |
---|
39 | n/a | # class testCaseClass" |
---|
40 | n/a | def test_loadTestsFromTestCase(self): |
---|
41 | n/a | class Foo(unittest.TestCase): |
---|
42 | n/a | def test_1(self): pass |
---|
43 | n/a | def test_2(self): pass |
---|
44 | n/a | def foo_bar(self): pass |
---|
45 | n/a | |
---|
46 | n/a | tests = unittest.TestSuite([Foo('test_1'), Foo('test_2')]) |
---|
47 | n/a | |
---|
48 | n/a | loader = unittest.TestLoader() |
---|
49 | n/a | self.assertEqual(loader.loadTestsFromTestCase(Foo), tests) |
---|
50 | n/a | |
---|
51 | n/a | # "Return a suite of all test cases contained in the TestCase-derived |
---|
52 | n/a | # class testCaseClass" |
---|
53 | n/a | # |
---|
54 | n/a | # Make sure it does the right thing even if no tests were found |
---|
55 | n/a | def test_loadTestsFromTestCase__no_matches(self): |
---|
56 | n/a | class Foo(unittest.TestCase): |
---|
57 | n/a | def foo_bar(self): pass |
---|
58 | n/a | |
---|
59 | n/a | empty_suite = unittest.TestSuite() |
---|
60 | n/a | |
---|
61 | n/a | loader = unittest.TestLoader() |
---|
62 | n/a | self.assertEqual(loader.loadTestsFromTestCase(Foo), empty_suite) |
---|
63 | n/a | |
---|
64 | n/a | # "Return a suite of all test cases contained in the TestCase-derived |
---|
65 | n/a | # class testCaseClass" |
---|
66 | n/a | # |
---|
67 | n/a | # What happens if loadTestsFromTestCase() is given an object |
---|
68 | n/a | # that isn't a subclass of TestCase? Specifically, what happens |
---|
69 | n/a | # if testCaseClass is a subclass of TestSuite? |
---|
70 | n/a | # |
---|
71 | n/a | # This is checked for specifically in the code, so we better add a |
---|
72 | n/a | # test for it. |
---|
73 | n/a | def test_loadTestsFromTestCase__TestSuite_subclass(self): |
---|
74 | n/a | class NotATestCase(unittest.TestSuite): |
---|
75 | n/a | pass |
---|
76 | n/a | |
---|
77 | n/a | loader = unittest.TestLoader() |
---|
78 | n/a | try: |
---|
79 | n/a | loader.loadTestsFromTestCase(NotATestCase) |
---|
80 | n/a | except TypeError: |
---|
81 | n/a | pass |
---|
82 | n/a | else: |
---|
83 | n/a | self.fail('Should raise TypeError') |
---|
84 | n/a | |
---|
85 | n/a | # "Return a suite of all test cases contained in the TestCase-derived |
---|
86 | n/a | # class testCaseClass" |
---|
87 | n/a | # |
---|
88 | n/a | # Make sure loadTestsFromTestCase() picks up the default test method |
---|
89 | n/a | # name (as specified by TestCase), even though the method name does |
---|
90 | n/a | # not match the default TestLoader.testMethodPrefix string |
---|
91 | n/a | def test_loadTestsFromTestCase__default_method_name(self): |
---|
92 | n/a | class Foo(unittest.TestCase): |
---|
93 | n/a | def runTest(self): |
---|
94 | n/a | pass |
---|
95 | n/a | |
---|
96 | n/a | loader = unittest.TestLoader() |
---|
97 | n/a | # This has to be false for the test to succeed |
---|
98 | n/a | self.assertFalse('runTest'.startswith(loader.testMethodPrefix)) |
---|
99 | n/a | |
---|
100 | n/a | suite = loader.loadTestsFromTestCase(Foo) |
---|
101 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
102 | n/a | self.assertEqual(list(suite), [Foo('runTest')]) |
---|
103 | n/a | |
---|
104 | n/a | ################################################################ |
---|
105 | n/a | ### /Tests for TestLoader.loadTestsFromTestCase |
---|
106 | n/a | |
---|
107 | n/a | ### Tests for TestLoader.loadTestsFromModule |
---|
108 | n/a | ################################################################ |
---|
109 | n/a | |
---|
110 | n/a | # "This method searches `module` for classes derived from TestCase" |
---|
111 | n/a | def test_loadTestsFromModule__TestCase_subclass(self): |
---|
112 | n/a | m = types.ModuleType('m') |
---|
113 | n/a | class MyTestCase(unittest.TestCase): |
---|
114 | n/a | def test(self): |
---|
115 | n/a | pass |
---|
116 | n/a | m.testcase_1 = MyTestCase |
---|
117 | n/a | |
---|
118 | n/a | loader = unittest.TestLoader() |
---|
119 | n/a | suite = loader.loadTestsFromModule(m) |
---|
120 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
121 | n/a | |
---|
122 | n/a | expected = [loader.suiteClass([MyTestCase('test')])] |
---|
123 | n/a | self.assertEqual(list(suite), expected) |
---|
124 | n/a | |
---|
125 | n/a | # "This method searches `module` for classes derived from TestCase" |
---|
126 | n/a | # |
---|
127 | n/a | # What happens if no tests are found (no TestCase instances)? |
---|
128 | n/a | def test_loadTestsFromModule__no_TestCase_instances(self): |
---|
129 | n/a | m = types.ModuleType('m') |
---|
130 | n/a | |
---|
131 | n/a | loader = unittest.TestLoader() |
---|
132 | n/a | suite = loader.loadTestsFromModule(m) |
---|
133 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
134 | n/a | self.assertEqual(list(suite), []) |
---|
135 | n/a | |
---|
136 | n/a | # "This method searches `module` for classes derived from TestCase" |
---|
137 | n/a | # |
---|
138 | n/a | # What happens if no tests are found (TestCases instances, but no tests)? |
---|
139 | n/a | def test_loadTestsFromModule__no_TestCase_tests(self): |
---|
140 | n/a | m = types.ModuleType('m') |
---|
141 | n/a | class MyTestCase(unittest.TestCase): |
---|
142 | n/a | pass |
---|
143 | n/a | m.testcase_1 = MyTestCase |
---|
144 | n/a | |
---|
145 | n/a | loader = unittest.TestLoader() |
---|
146 | n/a | suite = loader.loadTestsFromModule(m) |
---|
147 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
148 | n/a | |
---|
149 | n/a | self.assertEqual(list(suite), [loader.suiteClass()]) |
---|
150 | n/a | |
---|
151 | n/a | # "This method searches `module` for classes derived from TestCase"s |
---|
152 | n/a | # |
---|
153 | n/a | # What happens if loadTestsFromModule() is given something other |
---|
154 | n/a | # than a module? |
---|
155 | n/a | # |
---|
156 | n/a | # XXX Currently, it succeeds anyway. This flexibility |
---|
157 | n/a | # should either be documented or loadTestsFromModule() should |
---|
158 | n/a | # raise a TypeError |
---|
159 | n/a | # |
---|
160 | n/a | # XXX Certain people are using this behaviour. We'll add a test for it |
---|
161 | n/a | def test_loadTestsFromModule__not_a_module(self): |
---|
162 | n/a | class MyTestCase(unittest.TestCase): |
---|
163 | n/a | def test(self): |
---|
164 | n/a | pass |
---|
165 | n/a | |
---|
166 | n/a | class NotAModule(object): |
---|
167 | n/a | test_2 = MyTestCase |
---|
168 | n/a | |
---|
169 | n/a | loader = unittest.TestLoader() |
---|
170 | n/a | suite = loader.loadTestsFromModule(NotAModule) |
---|
171 | n/a | |
---|
172 | n/a | reference = [unittest.TestSuite([MyTestCase('test')])] |
---|
173 | n/a | self.assertEqual(list(suite), reference) |
---|
174 | n/a | |
---|
175 | n/a | |
---|
176 | n/a | # Check that loadTestsFromModule honors (or not) a module |
---|
177 | n/a | # with a load_tests function. |
---|
178 | n/a | @warningregistry |
---|
179 | n/a | def test_loadTestsFromModule__load_tests(self): |
---|
180 | n/a | m = types.ModuleType('m') |
---|
181 | n/a | class MyTestCase(unittest.TestCase): |
---|
182 | n/a | def test(self): |
---|
183 | n/a | pass |
---|
184 | n/a | m.testcase_1 = MyTestCase |
---|
185 | n/a | |
---|
186 | n/a | load_tests_args = [] |
---|
187 | n/a | def load_tests(loader, tests, pattern): |
---|
188 | n/a | self.assertIsInstance(tests, unittest.TestSuite) |
---|
189 | n/a | load_tests_args.extend((loader, tests, pattern)) |
---|
190 | n/a | return tests |
---|
191 | n/a | m.load_tests = load_tests |
---|
192 | n/a | |
---|
193 | n/a | loader = unittest.TestLoader() |
---|
194 | n/a | suite = loader.loadTestsFromModule(m) |
---|
195 | n/a | self.assertIsInstance(suite, unittest.TestSuite) |
---|
196 | n/a | self.assertEqual(load_tests_args, [loader, suite, None]) |
---|
197 | n/a | # With Python 3.5, the undocumented and unofficial use_load_tests is |
---|
198 | n/a | # ignored (and deprecated). |
---|
199 | n/a | load_tests_args = [] |
---|
200 | n/a | with warnings.catch_warnings(record=False): |
---|
201 | n/a | warnings.simplefilter('ignore') |
---|
202 | n/a | suite = loader.loadTestsFromModule(m, use_load_tests=False) |
---|
203 | n/a | self.assertEqual(load_tests_args, [loader, suite, None]) |
---|
204 | n/a | |
---|
205 | n/a | @warningregistry |
---|
206 | n/a | def test_loadTestsFromModule__use_load_tests_deprecated_positional(self): |
---|
207 | n/a | m = types.ModuleType('m') |
---|
208 | n/a | class MyTestCase(unittest.TestCase): |
---|
209 | n/a | def test(self): |
---|
210 | n/a | pass |
---|
211 | n/a | m.testcase_1 = MyTestCase |
---|
212 | n/a | |
---|
213 | n/a | load_tests_args = [] |
---|
214 | n/a | def load_tests(loader, tests, pattern): |
---|
215 | n/a | self.assertIsInstance(tests, unittest.TestSuite) |
---|
216 | n/a | load_tests_args.extend((loader, tests, pattern)) |
---|
217 | n/a | return tests |
---|
218 | n/a | m.load_tests = load_tests |
---|
219 | n/a | # The method still works. |
---|
220 | n/a | loader = unittest.TestLoader() |
---|
221 | n/a | # use_load_tests=True as a positional argument. |
---|
222 | n/a | with warnings.catch_warnings(record=True) as w: |
---|
223 | n/a | warnings.simplefilter('always') |
---|
224 | n/a | suite = loader.loadTestsFromModule(m, False) |
---|
225 | n/a | self.assertIsInstance(suite, unittest.TestSuite) |
---|
226 | n/a | # load_tests was still called because use_load_tests is deprecated |
---|
227 | n/a | # and ignored. |
---|
228 | n/a | self.assertEqual(load_tests_args, [loader, suite, None]) |
---|
229 | n/a | # We got a warning. |
---|
230 | n/a | self.assertIs(w[-1].category, DeprecationWarning) |
---|
231 | n/a | self.assertEqual(str(w[-1].message), |
---|
232 | n/a | 'use_load_tests is deprecated and ignored') |
---|
233 | n/a | |
---|
234 | n/a | @warningregistry |
---|
235 | n/a | def test_loadTestsFromModule__use_load_tests_deprecated_keyword(self): |
---|
236 | n/a | m = types.ModuleType('m') |
---|
237 | n/a | class MyTestCase(unittest.TestCase): |
---|
238 | n/a | def test(self): |
---|
239 | n/a | pass |
---|
240 | n/a | m.testcase_1 = MyTestCase |
---|
241 | n/a | |
---|
242 | n/a | load_tests_args = [] |
---|
243 | n/a | def load_tests(loader, tests, pattern): |
---|
244 | n/a | self.assertIsInstance(tests, unittest.TestSuite) |
---|
245 | n/a | load_tests_args.extend((loader, tests, pattern)) |
---|
246 | n/a | return tests |
---|
247 | n/a | m.load_tests = load_tests |
---|
248 | n/a | # The method still works. |
---|
249 | n/a | loader = unittest.TestLoader() |
---|
250 | n/a | with warnings.catch_warnings(record=True) as w: |
---|
251 | n/a | warnings.simplefilter('always') |
---|
252 | n/a | suite = loader.loadTestsFromModule(m, use_load_tests=False) |
---|
253 | n/a | self.assertIsInstance(suite, unittest.TestSuite) |
---|
254 | n/a | # load_tests was still called because use_load_tests is deprecated |
---|
255 | n/a | # and ignored. |
---|
256 | n/a | self.assertEqual(load_tests_args, [loader, suite, None]) |
---|
257 | n/a | # We got a warning. |
---|
258 | n/a | self.assertIs(w[-1].category, DeprecationWarning) |
---|
259 | n/a | self.assertEqual(str(w[-1].message), |
---|
260 | n/a | 'use_load_tests is deprecated and ignored') |
---|
261 | n/a | |
---|
262 | n/a | @warningregistry |
---|
263 | n/a | def test_loadTestsFromModule__too_many_positional_args(self): |
---|
264 | n/a | m = types.ModuleType('m') |
---|
265 | n/a | class MyTestCase(unittest.TestCase): |
---|
266 | n/a | def test(self): |
---|
267 | n/a | pass |
---|
268 | n/a | m.testcase_1 = MyTestCase |
---|
269 | n/a | |
---|
270 | n/a | load_tests_args = [] |
---|
271 | n/a | def load_tests(loader, tests, pattern): |
---|
272 | n/a | self.assertIsInstance(tests, unittest.TestSuite) |
---|
273 | n/a | load_tests_args.extend((loader, tests, pattern)) |
---|
274 | n/a | return tests |
---|
275 | n/a | m.load_tests = load_tests |
---|
276 | n/a | loader = unittest.TestLoader() |
---|
277 | n/a | with self.assertRaises(TypeError) as cm, \ |
---|
278 | n/a | warnings.catch_warnings(record=True) as w: |
---|
279 | n/a | warnings.simplefilter('always') |
---|
280 | n/a | loader.loadTestsFromModule(m, False, 'testme.*') |
---|
281 | n/a | # We still got the deprecation warning. |
---|
282 | n/a | self.assertIs(w[-1].category, DeprecationWarning) |
---|
283 | n/a | self.assertEqual(str(w[-1].message), |
---|
284 | n/a | 'use_load_tests is deprecated and ignored') |
---|
285 | n/a | # We also got a TypeError for too many positional arguments. |
---|
286 | n/a | self.assertEqual(type(cm.exception), TypeError) |
---|
287 | n/a | self.assertEqual( |
---|
288 | n/a | str(cm.exception), |
---|
289 | n/a | 'loadTestsFromModule() takes 1 positional argument but 3 were given') |
---|
290 | n/a | |
---|
291 | n/a | @warningregistry |
---|
292 | n/a | def test_loadTestsFromModule__use_load_tests_other_bad_keyword(self): |
---|
293 | n/a | m = types.ModuleType('m') |
---|
294 | n/a | class MyTestCase(unittest.TestCase): |
---|
295 | n/a | def test(self): |
---|
296 | n/a | pass |
---|
297 | n/a | m.testcase_1 = MyTestCase |
---|
298 | n/a | |
---|
299 | n/a | load_tests_args = [] |
---|
300 | n/a | def load_tests(loader, tests, pattern): |
---|
301 | n/a | self.assertIsInstance(tests, unittest.TestSuite) |
---|
302 | n/a | load_tests_args.extend((loader, tests, pattern)) |
---|
303 | n/a | return tests |
---|
304 | n/a | m.load_tests = load_tests |
---|
305 | n/a | loader = unittest.TestLoader() |
---|
306 | n/a | with warnings.catch_warnings(): |
---|
307 | n/a | warnings.simplefilter('ignore') |
---|
308 | n/a | with self.assertRaises(TypeError) as cm: |
---|
309 | n/a | loader.loadTestsFromModule( |
---|
310 | n/a | m, use_load_tests=False, very_bad=True, worse=False) |
---|
311 | n/a | self.assertEqual(type(cm.exception), TypeError) |
---|
312 | n/a | # The error message names the first bad argument alphabetically, |
---|
313 | n/a | # however use_load_tests (which sorts first) is ignored. |
---|
314 | n/a | self.assertEqual( |
---|
315 | n/a | str(cm.exception), |
---|
316 | n/a | "loadTestsFromModule() got an unexpected keyword argument 'very_bad'") |
---|
317 | n/a | |
---|
318 | n/a | def test_loadTestsFromModule__pattern(self): |
---|
319 | n/a | m = types.ModuleType('m') |
---|
320 | n/a | class MyTestCase(unittest.TestCase): |
---|
321 | n/a | def test(self): |
---|
322 | n/a | pass |
---|
323 | n/a | m.testcase_1 = MyTestCase |
---|
324 | n/a | |
---|
325 | n/a | load_tests_args = [] |
---|
326 | n/a | def load_tests(loader, tests, pattern): |
---|
327 | n/a | self.assertIsInstance(tests, unittest.TestSuite) |
---|
328 | n/a | load_tests_args.extend((loader, tests, pattern)) |
---|
329 | n/a | return tests |
---|
330 | n/a | m.load_tests = load_tests |
---|
331 | n/a | |
---|
332 | n/a | loader = unittest.TestLoader() |
---|
333 | n/a | suite = loader.loadTestsFromModule(m, pattern='testme.*') |
---|
334 | n/a | self.assertIsInstance(suite, unittest.TestSuite) |
---|
335 | n/a | self.assertEqual(load_tests_args, [loader, suite, 'testme.*']) |
---|
336 | n/a | |
---|
337 | n/a | def test_loadTestsFromModule__faulty_load_tests(self): |
---|
338 | n/a | m = types.ModuleType('m') |
---|
339 | n/a | |
---|
340 | n/a | def load_tests(loader, tests, pattern): |
---|
341 | n/a | raise TypeError('some failure') |
---|
342 | n/a | m.load_tests = load_tests |
---|
343 | n/a | |
---|
344 | n/a | loader = unittest.TestLoader() |
---|
345 | n/a | suite = loader.loadTestsFromModule(m) |
---|
346 | n/a | self.assertIsInstance(suite, unittest.TestSuite) |
---|
347 | n/a | self.assertEqual(suite.countTestCases(), 1) |
---|
348 | n/a | # Errors loading the suite are also captured for introspection. |
---|
349 | n/a | self.assertNotEqual([], loader.errors) |
---|
350 | n/a | self.assertEqual(1, len(loader.errors)) |
---|
351 | n/a | error = loader.errors[0] |
---|
352 | n/a | self.assertTrue( |
---|
353 | n/a | 'Failed to call load_tests:' in error, |
---|
354 | n/a | 'missing error string in %r' % error) |
---|
355 | n/a | test = list(suite)[0] |
---|
356 | n/a | |
---|
357 | n/a | self.assertRaisesRegex(TypeError, "some failure", test.m) |
---|
358 | n/a | |
---|
359 | n/a | ################################################################ |
---|
360 | n/a | ### /Tests for TestLoader.loadTestsFromModule() |
---|
361 | n/a | |
---|
362 | n/a | ### Tests for TestLoader.loadTestsFromName() |
---|
363 | n/a | ################################################################ |
---|
364 | n/a | |
---|
365 | n/a | # "The specifier name is a ``dotted name'' that may resolve either to |
---|
366 | n/a | # a module, a test case class, a TestSuite instance, a test method |
---|
367 | n/a | # within a test case class, or a callable object which returns a |
---|
368 | n/a | # TestCase or TestSuite instance." |
---|
369 | n/a | # |
---|
370 | n/a | # Is ValueError raised in response to an empty name? |
---|
371 | n/a | def test_loadTestsFromName__empty_name(self): |
---|
372 | n/a | loader = unittest.TestLoader() |
---|
373 | n/a | |
---|
374 | n/a | try: |
---|
375 | n/a | loader.loadTestsFromName('') |
---|
376 | n/a | except ValueError as e: |
---|
377 | n/a | self.assertEqual(str(e), "Empty module name") |
---|
378 | n/a | else: |
---|
379 | n/a | self.fail("TestLoader.loadTestsFromName failed to raise ValueError") |
---|
380 | n/a | |
---|
381 | n/a | # "The specifier name is a ``dotted name'' that may resolve either to |
---|
382 | n/a | # a module, a test case class, a TestSuite instance, a test method |
---|
383 | n/a | # within a test case class, or a callable object which returns a |
---|
384 | n/a | # TestCase or TestSuite instance." |
---|
385 | n/a | # |
---|
386 | n/a | # What happens when the name contains invalid characters? |
---|
387 | n/a | def test_loadTestsFromName__malformed_name(self): |
---|
388 | n/a | loader = unittest.TestLoader() |
---|
389 | n/a | |
---|
390 | n/a | suite = loader.loadTestsFromName('abc () //') |
---|
391 | n/a | error, test = self.check_deferred_error(loader, suite) |
---|
392 | n/a | expected = "Failed to import test module: abc () //" |
---|
393 | n/a | expected_regex = r"Failed to import test module: abc \(\) //" |
---|
394 | n/a | self.assertIn( |
---|
395 | n/a | expected, error, |
---|
396 | n/a | 'missing error string in %r' % error) |
---|
397 | n/a | self.assertRaisesRegex( |
---|
398 | n/a | ImportError, expected_regex, getattr(test, 'abc () //')) |
---|
399 | n/a | |
---|
400 | n/a | # "The specifier name is a ``dotted name'' that may resolve ... to a |
---|
401 | n/a | # module" |
---|
402 | n/a | # |
---|
403 | n/a | # What happens when a module by that name can't be found? |
---|
404 | n/a | def test_loadTestsFromName__unknown_module_name(self): |
---|
405 | n/a | loader = unittest.TestLoader() |
---|
406 | n/a | |
---|
407 | n/a | suite = loader.loadTestsFromName('sdasfasfasdf') |
---|
408 | n/a | expected = "No module named 'sdasfasfasdf'" |
---|
409 | n/a | error, test = self.check_deferred_error(loader, suite) |
---|
410 | n/a | self.assertIn( |
---|
411 | n/a | expected, error, |
---|
412 | n/a | 'missing error string in %r' % error) |
---|
413 | n/a | self.assertRaisesRegex(ImportError, expected, test.sdasfasfasdf) |
---|
414 | n/a | |
---|
415 | n/a | # "The specifier name is a ``dotted name'' that may resolve either to |
---|
416 | n/a | # a module, a test case class, a TestSuite instance, a test method |
---|
417 | n/a | # within a test case class, or a callable object which returns a |
---|
418 | n/a | # TestCase or TestSuite instance." |
---|
419 | n/a | # |
---|
420 | n/a | # What happens when the module is found, but the attribute isn't? |
---|
421 | n/a | def test_loadTestsFromName__unknown_attr_name_on_module(self): |
---|
422 | n/a | loader = unittest.TestLoader() |
---|
423 | n/a | |
---|
424 | n/a | suite = loader.loadTestsFromName('unittest.loader.sdasfasfasdf') |
---|
425 | n/a | expected = "module 'unittest.loader' has no attribute 'sdasfasfasdf'" |
---|
426 | n/a | error, test = self.check_deferred_error(loader, suite) |
---|
427 | n/a | self.assertIn( |
---|
428 | n/a | expected, error, |
---|
429 | n/a | 'missing error string in %r' % error) |
---|
430 | n/a | self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf) |
---|
431 | n/a | |
---|
432 | n/a | # "The specifier name is a ``dotted name'' that may resolve either to |
---|
433 | n/a | # a module, a test case class, a TestSuite instance, a test method |
---|
434 | n/a | # within a test case class, or a callable object which returns a |
---|
435 | n/a | # TestCase or TestSuite instance." |
---|
436 | n/a | # |
---|
437 | n/a | # What happens when the module is found, but the attribute isn't? |
---|
438 | n/a | def test_loadTestsFromName__unknown_attr_name_on_package(self): |
---|
439 | n/a | loader = unittest.TestLoader() |
---|
440 | n/a | |
---|
441 | n/a | suite = loader.loadTestsFromName('unittest.sdasfasfasdf') |
---|
442 | n/a | expected = "No module named 'unittest.sdasfasfasdf'" |
---|
443 | n/a | error, test = self.check_deferred_error(loader, suite) |
---|
444 | n/a | self.assertIn( |
---|
445 | n/a | expected, error, |
---|
446 | n/a | 'missing error string in %r' % error) |
---|
447 | n/a | self.assertRaisesRegex(ImportError, expected, test.sdasfasfasdf) |
---|
448 | n/a | |
---|
449 | n/a | # "The specifier name is a ``dotted name'' that may resolve either to |
---|
450 | n/a | # a module, a test case class, a TestSuite instance, a test method |
---|
451 | n/a | # within a test case class, or a callable object which returns a |
---|
452 | n/a | # TestCase or TestSuite instance." |
---|
453 | n/a | # |
---|
454 | n/a | # What happens when we provide the module, but the attribute can't be |
---|
455 | n/a | # found? |
---|
456 | n/a | def test_loadTestsFromName__relative_unknown_name(self): |
---|
457 | n/a | loader = unittest.TestLoader() |
---|
458 | n/a | |
---|
459 | n/a | suite = loader.loadTestsFromName('sdasfasfasdf', unittest) |
---|
460 | n/a | expected = "module 'unittest' has no attribute 'sdasfasfasdf'" |
---|
461 | n/a | error, test = self.check_deferred_error(loader, suite) |
---|
462 | n/a | self.assertIn( |
---|
463 | n/a | expected, error, |
---|
464 | n/a | 'missing error string in %r' % error) |
---|
465 | n/a | self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf) |
---|
466 | n/a | |
---|
467 | n/a | # "The specifier name is a ``dotted name'' that may resolve either to |
---|
468 | n/a | # a module, a test case class, a TestSuite instance, a test method |
---|
469 | n/a | # within a test case class, or a callable object which returns a |
---|
470 | n/a | # TestCase or TestSuite instance." |
---|
471 | n/a | # ... |
---|
472 | n/a | # "The method optionally resolves name relative to the given module" |
---|
473 | n/a | # |
---|
474 | n/a | # Does loadTestsFromName raise ValueError when passed an empty |
---|
475 | n/a | # name relative to a provided module? |
---|
476 | n/a | # |
---|
477 | n/a | # XXX Should probably raise a ValueError instead of an AttributeError |
---|
478 | n/a | def test_loadTestsFromName__relative_empty_name(self): |
---|
479 | n/a | loader = unittest.TestLoader() |
---|
480 | n/a | |
---|
481 | n/a | suite = loader.loadTestsFromName('', unittest) |
---|
482 | n/a | error, test = self.check_deferred_error(loader, suite) |
---|
483 | n/a | expected = "has no attribute ''" |
---|
484 | n/a | self.assertIn( |
---|
485 | n/a | expected, error, |
---|
486 | n/a | 'missing error string in %r' % error) |
---|
487 | n/a | self.assertRaisesRegex(AttributeError, expected, getattr(test, '')) |
---|
488 | n/a | |
---|
489 | n/a | # "The specifier name is a ``dotted name'' that may resolve either to |
---|
490 | n/a | # a module, a test case class, a TestSuite instance, a test method |
---|
491 | n/a | # within a test case class, or a callable object which returns a |
---|
492 | n/a | # TestCase or TestSuite instance." |
---|
493 | n/a | # ... |
---|
494 | n/a | # "The method optionally resolves name relative to the given module" |
---|
495 | n/a | # |
---|
496 | n/a | # What happens when an impossible name is given, relative to the provided |
---|
497 | n/a | # `module`? |
---|
498 | n/a | def test_loadTestsFromName__relative_malformed_name(self): |
---|
499 | n/a | loader = unittest.TestLoader() |
---|
500 | n/a | |
---|
501 | n/a | # XXX Should this raise AttributeError or ValueError? |
---|
502 | n/a | suite = loader.loadTestsFromName('abc () //', unittest) |
---|
503 | n/a | error, test = self.check_deferred_error(loader, suite) |
---|
504 | n/a | expected = "module 'unittest' has no attribute 'abc () //'" |
---|
505 | n/a | expected_regex = r"module 'unittest' has no attribute 'abc \(\) //'" |
---|
506 | n/a | self.assertIn( |
---|
507 | n/a | expected, error, |
---|
508 | n/a | 'missing error string in %r' % error) |
---|
509 | n/a | self.assertRaisesRegex( |
---|
510 | n/a | AttributeError, expected_regex, getattr(test, 'abc () //')) |
---|
511 | n/a | |
---|
512 | n/a | # "The method optionally resolves name relative to the given module" |
---|
513 | n/a | # |
---|
514 | n/a | # Does loadTestsFromName raise TypeError when the `module` argument |
---|
515 | n/a | # isn't a module object? |
---|
516 | n/a | # |
---|
517 | n/a | # XXX Accepts the not-a-module object, ignoring the object's type |
---|
518 | n/a | # This should raise an exception or the method name should be changed |
---|
519 | n/a | # |
---|
520 | n/a | # XXX Some people are relying on this, so keep it for now |
---|
521 | n/a | def test_loadTestsFromName__relative_not_a_module(self): |
---|
522 | n/a | class MyTestCase(unittest.TestCase): |
---|
523 | n/a | def test(self): |
---|
524 | n/a | pass |
---|
525 | n/a | |
---|
526 | n/a | class NotAModule(object): |
---|
527 | n/a | test_2 = MyTestCase |
---|
528 | n/a | |
---|
529 | n/a | loader = unittest.TestLoader() |
---|
530 | n/a | suite = loader.loadTestsFromName('test_2', NotAModule) |
---|
531 | n/a | |
---|
532 | n/a | reference = [MyTestCase('test')] |
---|
533 | n/a | self.assertEqual(list(suite), reference) |
---|
534 | n/a | |
---|
535 | n/a | # "The specifier name is a ``dotted name'' that may resolve either to |
---|
536 | n/a | # a module, a test case class, a TestSuite instance, a test method |
---|
537 | n/a | # within a test case class, or a callable object which returns a |
---|
538 | n/a | # TestCase or TestSuite instance." |
---|
539 | n/a | # |
---|
540 | n/a | # Does it raise an exception if the name resolves to an invalid |
---|
541 | n/a | # object? |
---|
542 | n/a | def test_loadTestsFromName__relative_bad_object(self): |
---|
543 | n/a | m = types.ModuleType('m') |
---|
544 | n/a | m.testcase_1 = object() |
---|
545 | n/a | |
---|
546 | n/a | loader = unittest.TestLoader() |
---|
547 | n/a | try: |
---|
548 | n/a | loader.loadTestsFromName('testcase_1', m) |
---|
549 | n/a | except TypeError: |
---|
550 | n/a | pass |
---|
551 | n/a | else: |
---|
552 | n/a | self.fail("Should have raised TypeError") |
---|
553 | n/a | |
---|
554 | n/a | # "The specifier name is a ``dotted name'' that may |
---|
555 | n/a | # resolve either to ... a test case class" |
---|
556 | n/a | def test_loadTestsFromName__relative_TestCase_subclass(self): |
---|
557 | n/a | m = types.ModuleType('m') |
---|
558 | n/a | class MyTestCase(unittest.TestCase): |
---|
559 | n/a | def test(self): |
---|
560 | n/a | pass |
---|
561 | n/a | m.testcase_1 = MyTestCase |
---|
562 | n/a | |
---|
563 | n/a | loader = unittest.TestLoader() |
---|
564 | n/a | suite = loader.loadTestsFromName('testcase_1', m) |
---|
565 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
566 | n/a | self.assertEqual(list(suite), [MyTestCase('test')]) |
---|
567 | n/a | |
---|
568 | n/a | # "The specifier name is a ``dotted name'' that may resolve either to |
---|
569 | n/a | # a module, a test case class, a TestSuite instance, a test method |
---|
570 | n/a | # within a test case class, or a callable object which returns a |
---|
571 | n/a | # TestCase or TestSuite instance." |
---|
572 | n/a | def test_loadTestsFromName__relative_TestSuite(self): |
---|
573 | n/a | m = types.ModuleType('m') |
---|
574 | n/a | class MyTestCase(unittest.TestCase): |
---|
575 | n/a | def test(self): |
---|
576 | n/a | pass |
---|
577 | n/a | m.testsuite = unittest.TestSuite([MyTestCase('test')]) |
---|
578 | n/a | |
---|
579 | n/a | loader = unittest.TestLoader() |
---|
580 | n/a | suite = loader.loadTestsFromName('testsuite', m) |
---|
581 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
582 | n/a | |
---|
583 | n/a | self.assertEqual(list(suite), [MyTestCase('test')]) |
---|
584 | n/a | |
---|
585 | n/a | # "The specifier name is a ``dotted name'' that may resolve ... to |
---|
586 | n/a | # ... a test method within a test case class" |
---|
587 | n/a | def test_loadTestsFromName__relative_testmethod(self): |
---|
588 | n/a | m = types.ModuleType('m') |
---|
589 | n/a | class MyTestCase(unittest.TestCase): |
---|
590 | n/a | def test(self): |
---|
591 | n/a | pass |
---|
592 | n/a | m.testcase_1 = MyTestCase |
---|
593 | n/a | |
---|
594 | n/a | loader = unittest.TestLoader() |
---|
595 | n/a | suite = loader.loadTestsFromName('testcase_1.test', m) |
---|
596 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
597 | n/a | |
---|
598 | n/a | self.assertEqual(list(suite), [MyTestCase('test')]) |
---|
599 | n/a | |
---|
600 | n/a | # "The specifier name is a ``dotted name'' that may resolve either to |
---|
601 | n/a | # a module, a test case class, a TestSuite instance, a test method |
---|
602 | n/a | # within a test case class, or a callable object which returns a |
---|
603 | n/a | # TestCase or TestSuite instance." |
---|
604 | n/a | # |
---|
605 | n/a | # Does loadTestsFromName() raise the proper exception when trying to |
---|
606 | n/a | # resolve "a test method within a test case class" that doesn't exist |
---|
607 | n/a | # for the given name (relative to a provided module)? |
---|
608 | n/a | def test_loadTestsFromName__relative_invalid_testmethod(self): |
---|
609 | n/a | m = types.ModuleType('m') |
---|
610 | n/a | class MyTestCase(unittest.TestCase): |
---|
611 | n/a | def test(self): |
---|
612 | n/a | pass |
---|
613 | n/a | m.testcase_1 = MyTestCase |
---|
614 | n/a | |
---|
615 | n/a | loader = unittest.TestLoader() |
---|
616 | n/a | suite = loader.loadTestsFromName('testcase_1.testfoo', m) |
---|
617 | n/a | expected = "type object 'MyTestCase' has no attribute 'testfoo'" |
---|
618 | n/a | error, test = self.check_deferred_error(loader, suite) |
---|
619 | n/a | self.assertIn( |
---|
620 | n/a | expected, error, |
---|
621 | n/a | 'missing error string in %r' % error) |
---|
622 | n/a | self.assertRaisesRegex(AttributeError, expected, test.testfoo) |
---|
623 | n/a | |
---|
624 | n/a | # "The specifier name is a ``dotted name'' that may resolve ... to |
---|
625 | n/a | # ... a callable object which returns a ... TestSuite instance" |
---|
626 | n/a | def test_loadTestsFromName__callable__TestSuite(self): |
---|
627 | n/a | m = types.ModuleType('m') |
---|
628 | n/a | testcase_1 = unittest.FunctionTestCase(lambda: None) |
---|
629 | n/a | testcase_2 = unittest.FunctionTestCase(lambda: None) |
---|
630 | n/a | def return_TestSuite(): |
---|
631 | n/a | return unittest.TestSuite([testcase_1, testcase_2]) |
---|
632 | n/a | m.return_TestSuite = return_TestSuite |
---|
633 | n/a | |
---|
634 | n/a | loader = unittest.TestLoader() |
---|
635 | n/a | suite = loader.loadTestsFromName('return_TestSuite', m) |
---|
636 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
637 | n/a | self.assertEqual(list(suite), [testcase_1, testcase_2]) |
---|
638 | n/a | |
---|
639 | n/a | # "The specifier name is a ``dotted name'' that may resolve ... to |
---|
640 | n/a | # ... a callable object which returns a TestCase ... instance" |
---|
641 | n/a | def test_loadTestsFromName__callable__TestCase_instance(self): |
---|
642 | n/a | m = types.ModuleType('m') |
---|
643 | n/a | testcase_1 = unittest.FunctionTestCase(lambda: None) |
---|
644 | n/a | def return_TestCase(): |
---|
645 | n/a | return testcase_1 |
---|
646 | n/a | m.return_TestCase = return_TestCase |
---|
647 | n/a | |
---|
648 | n/a | loader = unittest.TestLoader() |
---|
649 | n/a | suite = loader.loadTestsFromName('return_TestCase', m) |
---|
650 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
651 | n/a | self.assertEqual(list(suite), [testcase_1]) |
---|
652 | n/a | |
---|
653 | n/a | # "The specifier name is a ``dotted name'' that may resolve ... to |
---|
654 | n/a | # ... a callable object which returns a TestCase ... instance" |
---|
655 | n/a | #***************************************************************** |
---|
656 | n/a | #Override the suiteClass attribute to ensure that the suiteClass |
---|
657 | n/a | #attribute is used |
---|
658 | n/a | def test_loadTestsFromName__callable__TestCase_instance_ProperSuiteClass(self): |
---|
659 | n/a | class SubTestSuite(unittest.TestSuite): |
---|
660 | n/a | pass |
---|
661 | n/a | m = types.ModuleType('m') |
---|
662 | n/a | testcase_1 = unittest.FunctionTestCase(lambda: None) |
---|
663 | n/a | def return_TestCase(): |
---|
664 | n/a | return testcase_1 |
---|
665 | n/a | m.return_TestCase = return_TestCase |
---|
666 | n/a | |
---|
667 | n/a | loader = unittest.TestLoader() |
---|
668 | n/a | loader.suiteClass = SubTestSuite |
---|
669 | n/a | suite = loader.loadTestsFromName('return_TestCase', m) |
---|
670 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
671 | n/a | self.assertEqual(list(suite), [testcase_1]) |
---|
672 | n/a | |
---|
673 | n/a | # "The specifier name is a ``dotted name'' that may resolve ... to |
---|
674 | n/a | # ... a test method within a test case class" |
---|
675 | n/a | #***************************************************************** |
---|
676 | n/a | #Override the suiteClass attribute to ensure that the suiteClass |
---|
677 | n/a | #attribute is used |
---|
678 | n/a | def test_loadTestsFromName__relative_testmethod_ProperSuiteClass(self): |
---|
679 | n/a | class SubTestSuite(unittest.TestSuite): |
---|
680 | n/a | pass |
---|
681 | n/a | m = types.ModuleType('m') |
---|
682 | n/a | class MyTestCase(unittest.TestCase): |
---|
683 | n/a | def test(self): |
---|
684 | n/a | pass |
---|
685 | n/a | m.testcase_1 = MyTestCase |
---|
686 | n/a | |
---|
687 | n/a | loader = unittest.TestLoader() |
---|
688 | n/a | loader.suiteClass=SubTestSuite |
---|
689 | n/a | suite = loader.loadTestsFromName('testcase_1.test', m) |
---|
690 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
691 | n/a | |
---|
692 | n/a | self.assertEqual(list(suite), [MyTestCase('test')]) |
---|
693 | n/a | |
---|
694 | n/a | # "The specifier name is a ``dotted name'' that may resolve ... to |
---|
695 | n/a | # ... a callable object which returns a TestCase or TestSuite instance" |
---|
696 | n/a | # |
---|
697 | n/a | # What happens if the callable returns something else? |
---|
698 | n/a | def test_loadTestsFromName__callable__wrong_type(self): |
---|
699 | n/a | m = types.ModuleType('m') |
---|
700 | n/a | def return_wrong(): |
---|
701 | n/a | return 6 |
---|
702 | n/a | m.return_wrong = return_wrong |
---|
703 | n/a | |
---|
704 | n/a | loader = unittest.TestLoader() |
---|
705 | n/a | try: |
---|
706 | n/a | suite = loader.loadTestsFromName('return_wrong', m) |
---|
707 | n/a | except TypeError: |
---|
708 | n/a | pass |
---|
709 | n/a | else: |
---|
710 | n/a | self.fail("TestLoader.loadTestsFromName failed to raise TypeError") |
---|
711 | n/a | |
---|
712 | n/a | # "The specifier can refer to modules and packages which have not been |
---|
713 | n/a | # imported; they will be imported as a side-effect" |
---|
714 | n/a | def test_loadTestsFromName__module_not_loaded(self): |
---|
715 | n/a | # We're going to try to load this module as a side-effect, so it |
---|
716 | n/a | # better not be loaded before we try. |
---|
717 | n/a | # |
---|
718 | n/a | module_name = 'unittest.test.dummy' |
---|
719 | n/a | sys.modules.pop(module_name, None) |
---|
720 | n/a | |
---|
721 | n/a | loader = unittest.TestLoader() |
---|
722 | n/a | try: |
---|
723 | n/a | suite = loader.loadTestsFromName(module_name) |
---|
724 | n/a | |
---|
725 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
726 | n/a | self.assertEqual(list(suite), []) |
---|
727 | n/a | |
---|
728 | n/a | # module should now be loaded, thanks to loadTestsFromName() |
---|
729 | n/a | self.assertIn(module_name, sys.modules) |
---|
730 | n/a | finally: |
---|
731 | n/a | if module_name in sys.modules: |
---|
732 | n/a | del sys.modules[module_name] |
---|
733 | n/a | |
---|
734 | n/a | ################################################################ |
---|
735 | n/a | ### Tests for TestLoader.loadTestsFromName() |
---|
736 | n/a | |
---|
737 | n/a | ### Tests for TestLoader.loadTestsFromNames() |
---|
738 | n/a | ################################################################ |
---|
739 | n/a | |
---|
740 | n/a | def check_deferred_error(self, loader, suite): |
---|
741 | n/a | """Helper function for checking that errors in loading are reported. |
---|
742 | n/a | |
---|
743 | n/a | :param loader: A loader with some errors. |
---|
744 | n/a | :param suite: A suite that should have a late bound error. |
---|
745 | n/a | :return: The first error message from the loader and the test object |
---|
746 | n/a | from the suite. |
---|
747 | n/a | """ |
---|
748 | n/a | self.assertIsInstance(suite, unittest.TestSuite) |
---|
749 | n/a | self.assertEqual(suite.countTestCases(), 1) |
---|
750 | n/a | # Errors loading the suite are also captured for introspection. |
---|
751 | n/a | self.assertNotEqual([], loader.errors) |
---|
752 | n/a | self.assertEqual(1, len(loader.errors)) |
---|
753 | n/a | error = loader.errors[0] |
---|
754 | n/a | test = list(suite)[0] |
---|
755 | n/a | return error, test |
---|
756 | n/a | |
---|
757 | n/a | # "Similar to loadTestsFromName(), but takes a sequence of names rather |
---|
758 | n/a | # than a single name." |
---|
759 | n/a | # |
---|
760 | n/a | # What happens if that sequence of names is empty? |
---|
761 | n/a | def test_loadTestsFromNames__empty_name_list(self): |
---|
762 | n/a | loader = unittest.TestLoader() |
---|
763 | n/a | |
---|
764 | n/a | suite = loader.loadTestsFromNames([]) |
---|
765 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
766 | n/a | self.assertEqual(list(suite), []) |
---|
767 | n/a | |
---|
768 | n/a | # "Similar to loadTestsFromName(), but takes a sequence of names rather |
---|
769 | n/a | # than a single name." |
---|
770 | n/a | # ... |
---|
771 | n/a | # "The method optionally resolves name relative to the given module" |
---|
772 | n/a | # |
---|
773 | n/a | # What happens if that sequence of names is empty? |
---|
774 | n/a | # |
---|
775 | n/a | # XXX Should this raise a ValueError or just return an empty TestSuite? |
---|
776 | n/a | def test_loadTestsFromNames__relative_empty_name_list(self): |
---|
777 | n/a | loader = unittest.TestLoader() |
---|
778 | n/a | |
---|
779 | n/a | suite = loader.loadTestsFromNames([], unittest) |
---|
780 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
781 | n/a | self.assertEqual(list(suite), []) |
---|
782 | n/a | |
---|
783 | n/a | # "The specifier name is a ``dotted name'' that may resolve either to |
---|
784 | n/a | # a module, a test case class, a TestSuite instance, a test method |
---|
785 | n/a | # within a test case class, or a callable object which returns a |
---|
786 | n/a | # TestCase or TestSuite instance." |
---|
787 | n/a | # |
---|
788 | n/a | # Is ValueError raised in response to an empty name? |
---|
789 | n/a | def test_loadTestsFromNames__empty_name(self): |
---|
790 | n/a | loader = unittest.TestLoader() |
---|
791 | n/a | |
---|
792 | n/a | try: |
---|
793 | n/a | loader.loadTestsFromNames(['']) |
---|
794 | n/a | except ValueError as e: |
---|
795 | n/a | self.assertEqual(str(e), "Empty module name") |
---|
796 | n/a | else: |
---|
797 | n/a | self.fail("TestLoader.loadTestsFromNames failed to raise ValueError") |
---|
798 | n/a | |
---|
799 | n/a | # "The specifier name is a ``dotted name'' that may resolve either to |
---|
800 | n/a | # a module, a test case class, a TestSuite instance, a test method |
---|
801 | n/a | # within a test case class, or a callable object which returns a |
---|
802 | n/a | # TestCase or TestSuite instance." |
---|
803 | n/a | # |
---|
804 | n/a | # What happens when presented with an impossible module name? |
---|
805 | n/a | def test_loadTestsFromNames__malformed_name(self): |
---|
806 | n/a | loader = unittest.TestLoader() |
---|
807 | n/a | |
---|
808 | n/a | # XXX Should this raise ValueError or ImportError? |
---|
809 | n/a | suite = loader.loadTestsFromNames(['abc () //']) |
---|
810 | n/a | error, test = self.check_deferred_error(loader, list(suite)[0]) |
---|
811 | n/a | expected = "Failed to import test module: abc () //" |
---|
812 | n/a | expected_regex = r"Failed to import test module: abc \(\) //" |
---|
813 | n/a | self.assertIn( |
---|
814 | n/a | expected, error, |
---|
815 | n/a | 'missing error string in %r' % error) |
---|
816 | n/a | self.assertRaisesRegex( |
---|
817 | n/a | ImportError, expected_regex, getattr(test, 'abc () //')) |
---|
818 | n/a | |
---|
819 | n/a | # "The specifier name is a ``dotted name'' that may resolve either to |
---|
820 | n/a | # a module, a test case class, a TestSuite instance, a test method |
---|
821 | n/a | # within a test case class, or a callable object which returns a |
---|
822 | n/a | # TestCase or TestSuite instance." |
---|
823 | n/a | # |
---|
824 | n/a | # What happens when no module can be found for the given name? |
---|
825 | n/a | def test_loadTestsFromNames__unknown_module_name(self): |
---|
826 | n/a | loader = unittest.TestLoader() |
---|
827 | n/a | |
---|
828 | n/a | suite = loader.loadTestsFromNames(['sdasfasfasdf']) |
---|
829 | n/a | error, test = self.check_deferred_error(loader, list(suite)[0]) |
---|
830 | n/a | expected = "Failed to import test module: sdasfasfasdf" |
---|
831 | n/a | self.assertIn( |
---|
832 | n/a | expected, error, |
---|
833 | n/a | 'missing error string in %r' % error) |
---|
834 | n/a | self.assertRaisesRegex(ImportError, expected, test.sdasfasfasdf) |
---|
835 | n/a | |
---|
836 | n/a | # "The specifier name is a ``dotted name'' that may resolve either to |
---|
837 | n/a | # a module, a test case class, a TestSuite instance, a test method |
---|
838 | n/a | # within a test case class, or a callable object which returns a |
---|
839 | n/a | # TestCase or TestSuite instance." |
---|
840 | n/a | # |
---|
841 | n/a | # What happens when the module can be found, but not the attribute? |
---|
842 | n/a | def test_loadTestsFromNames__unknown_attr_name(self): |
---|
843 | n/a | loader = unittest.TestLoader() |
---|
844 | n/a | |
---|
845 | n/a | suite = loader.loadTestsFromNames( |
---|
846 | n/a | ['unittest.loader.sdasfasfasdf', 'unittest.test.dummy']) |
---|
847 | n/a | error, test = self.check_deferred_error(loader, list(suite)[0]) |
---|
848 | n/a | expected = "module 'unittest.loader' has no attribute 'sdasfasfasdf'" |
---|
849 | n/a | self.assertIn( |
---|
850 | n/a | expected, error, |
---|
851 | n/a | 'missing error string in %r' % error) |
---|
852 | n/a | self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf) |
---|
853 | n/a | |
---|
854 | n/a | # "The specifier name is a ``dotted name'' that may resolve either to |
---|
855 | n/a | # a module, a test case class, a TestSuite instance, a test method |
---|
856 | n/a | # within a test case class, or a callable object which returns a |
---|
857 | n/a | # TestCase or TestSuite instance." |
---|
858 | n/a | # ... |
---|
859 | n/a | # "The method optionally resolves name relative to the given module" |
---|
860 | n/a | # |
---|
861 | n/a | # What happens when given an unknown attribute on a specified `module` |
---|
862 | n/a | # argument? |
---|
863 | n/a | def test_loadTestsFromNames__unknown_name_relative_1(self): |
---|
864 | n/a | loader = unittest.TestLoader() |
---|
865 | n/a | |
---|
866 | n/a | suite = loader.loadTestsFromNames(['sdasfasfasdf'], unittest) |
---|
867 | n/a | error, test = self.check_deferred_error(loader, list(suite)[0]) |
---|
868 | n/a | expected = "module 'unittest' has no attribute 'sdasfasfasdf'" |
---|
869 | n/a | self.assertIn( |
---|
870 | n/a | expected, error, |
---|
871 | n/a | 'missing error string in %r' % error) |
---|
872 | n/a | self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf) |
---|
873 | n/a | |
---|
874 | n/a | # "The specifier name is a ``dotted name'' that may resolve either to |
---|
875 | n/a | # a module, a test case class, a TestSuite instance, a test method |
---|
876 | n/a | # within a test case class, or a callable object which returns a |
---|
877 | n/a | # TestCase or TestSuite instance." |
---|
878 | n/a | # ... |
---|
879 | n/a | # "The method optionally resolves name relative to the given module" |
---|
880 | n/a | # |
---|
881 | n/a | # Do unknown attributes (relative to a provided module) still raise an |
---|
882 | n/a | # exception even in the presence of valid attribute names? |
---|
883 | n/a | def test_loadTestsFromNames__unknown_name_relative_2(self): |
---|
884 | n/a | loader = unittest.TestLoader() |
---|
885 | n/a | |
---|
886 | n/a | suite = loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest) |
---|
887 | n/a | error, test = self.check_deferred_error(loader, list(suite)[1]) |
---|
888 | n/a | expected = "module 'unittest' has no attribute 'sdasfasfasdf'" |
---|
889 | n/a | self.assertIn( |
---|
890 | n/a | expected, error, |
---|
891 | n/a | 'missing error string in %r' % error) |
---|
892 | n/a | self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf) |
---|
893 | n/a | |
---|
894 | n/a | # "The specifier name is a ``dotted name'' that may resolve either to |
---|
895 | n/a | # a module, a test case class, a TestSuite instance, a test method |
---|
896 | n/a | # within a test case class, or a callable object which returns a |
---|
897 | n/a | # TestCase or TestSuite instance." |
---|
898 | n/a | # ... |
---|
899 | n/a | # "The method optionally resolves name relative to the given module" |
---|
900 | n/a | # |
---|
901 | n/a | # What happens when faced with the empty string? |
---|
902 | n/a | # |
---|
903 | n/a | # XXX This currently raises AttributeError, though ValueError is probably |
---|
904 | n/a | # more appropriate |
---|
905 | n/a | def test_loadTestsFromNames__relative_empty_name(self): |
---|
906 | n/a | loader = unittest.TestLoader() |
---|
907 | n/a | |
---|
908 | n/a | suite = loader.loadTestsFromNames([''], unittest) |
---|
909 | n/a | error, test = self.check_deferred_error(loader, list(suite)[0]) |
---|
910 | n/a | expected = "has no attribute ''" |
---|
911 | n/a | self.assertIn( |
---|
912 | n/a | expected, error, |
---|
913 | n/a | 'missing error string in %r' % error) |
---|
914 | n/a | self.assertRaisesRegex(AttributeError, expected, getattr(test, '')) |
---|
915 | n/a | |
---|
916 | n/a | # "The specifier name is a ``dotted name'' that may resolve either to |
---|
917 | n/a | # a module, a test case class, a TestSuite instance, a test method |
---|
918 | n/a | # within a test case class, or a callable object which returns a |
---|
919 | n/a | # TestCase or TestSuite instance." |
---|
920 | n/a | # ... |
---|
921 | n/a | # "The method optionally resolves name relative to the given module" |
---|
922 | n/a | # |
---|
923 | n/a | # What happens when presented with an impossible attribute name? |
---|
924 | n/a | def test_loadTestsFromNames__relative_malformed_name(self): |
---|
925 | n/a | loader = unittest.TestLoader() |
---|
926 | n/a | |
---|
927 | n/a | # XXX Should this raise AttributeError or ValueError? |
---|
928 | n/a | suite = loader.loadTestsFromNames(['abc () //'], unittest) |
---|
929 | n/a | error, test = self.check_deferred_error(loader, list(suite)[0]) |
---|
930 | n/a | expected = "module 'unittest' has no attribute 'abc () //'" |
---|
931 | n/a | expected_regex = r"module 'unittest' has no attribute 'abc \(\) //'" |
---|
932 | n/a | self.assertIn( |
---|
933 | n/a | expected, error, |
---|
934 | n/a | 'missing error string in %r' % error) |
---|
935 | n/a | self.assertRaisesRegex( |
---|
936 | n/a | AttributeError, expected_regex, getattr(test, 'abc () //')) |
---|
937 | n/a | |
---|
938 | n/a | # "The method optionally resolves name relative to the given module" |
---|
939 | n/a | # |
---|
940 | n/a | # Does loadTestsFromNames() make sure the provided `module` is in fact |
---|
941 | n/a | # a module? |
---|
942 | n/a | # |
---|
943 | n/a | # XXX This validation is currently not done. This flexibility should |
---|
944 | n/a | # either be documented or a TypeError should be raised. |
---|
945 | n/a | def test_loadTestsFromNames__relative_not_a_module(self): |
---|
946 | n/a | class MyTestCase(unittest.TestCase): |
---|
947 | n/a | def test(self): |
---|
948 | n/a | pass |
---|
949 | n/a | |
---|
950 | n/a | class NotAModule(object): |
---|
951 | n/a | test_2 = MyTestCase |
---|
952 | n/a | |
---|
953 | n/a | loader = unittest.TestLoader() |
---|
954 | n/a | suite = loader.loadTestsFromNames(['test_2'], NotAModule) |
---|
955 | n/a | |
---|
956 | n/a | reference = [unittest.TestSuite([MyTestCase('test')])] |
---|
957 | n/a | self.assertEqual(list(suite), reference) |
---|
958 | n/a | |
---|
959 | n/a | # "The specifier name is a ``dotted name'' that may resolve either to |
---|
960 | n/a | # a module, a test case class, a TestSuite instance, a test method |
---|
961 | n/a | # within a test case class, or a callable object which returns a |
---|
962 | n/a | # TestCase or TestSuite instance." |
---|
963 | n/a | # |
---|
964 | n/a | # Does it raise an exception if the name resolves to an invalid |
---|
965 | n/a | # object? |
---|
966 | n/a | def test_loadTestsFromNames__relative_bad_object(self): |
---|
967 | n/a | m = types.ModuleType('m') |
---|
968 | n/a | m.testcase_1 = object() |
---|
969 | n/a | |
---|
970 | n/a | loader = unittest.TestLoader() |
---|
971 | n/a | try: |
---|
972 | n/a | loader.loadTestsFromNames(['testcase_1'], m) |
---|
973 | n/a | except TypeError: |
---|
974 | n/a | pass |
---|
975 | n/a | else: |
---|
976 | n/a | self.fail("Should have raised TypeError") |
---|
977 | n/a | |
---|
978 | n/a | # "The specifier name is a ``dotted name'' that may resolve ... to |
---|
979 | n/a | # ... a test case class" |
---|
980 | n/a | def test_loadTestsFromNames__relative_TestCase_subclass(self): |
---|
981 | n/a | m = types.ModuleType('m') |
---|
982 | n/a | class MyTestCase(unittest.TestCase): |
---|
983 | n/a | def test(self): |
---|
984 | n/a | pass |
---|
985 | n/a | m.testcase_1 = MyTestCase |
---|
986 | n/a | |
---|
987 | n/a | loader = unittest.TestLoader() |
---|
988 | n/a | suite = loader.loadTestsFromNames(['testcase_1'], m) |
---|
989 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
990 | n/a | |
---|
991 | n/a | expected = loader.suiteClass([MyTestCase('test')]) |
---|
992 | n/a | self.assertEqual(list(suite), [expected]) |
---|
993 | n/a | |
---|
994 | n/a | # "The specifier name is a ``dotted name'' that may resolve ... to |
---|
995 | n/a | # ... a TestSuite instance" |
---|
996 | n/a | def test_loadTestsFromNames__relative_TestSuite(self): |
---|
997 | n/a | m = types.ModuleType('m') |
---|
998 | n/a | class MyTestCase(unittest.TestCase): |
---|
999 | n/a | def test(self): |
---|
1000 | n/a | pass |
---|
1001 | n/a | m.testsuite = unittest.TestSuite([MyTestCase('test')]) |
---|
1002 | n/a | |
---|
1003 | n/a | loader = unittest.TestLoader() |
---|
1004 | n/a | suite = loader.loadTestsFromNames(['testsuite'], m) |
---|
1005 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
1006 | n/a | |
---|
1007 | n/a | self.assertEqual(list(suite), [m.testsuite]) |
---|
1008 | n/a | |
---|
1009 | n/a | # "The specifier name is a ``dotted name'' that may resolve ... to ... a |
---|
1010 | n/a | # test method within a test case class" |
---|
1011 | n/a | def test_loadTestsFromNames__relative_testmethod(self): |
---|
1012 | n/a | m = types.ModuleType('m') |
---|
1013 | n/a | class MyTestCase(unittest.TestCase): |
---|
1014 | n/a | def test(self): |
---|
1015 | n/a | pass |
---|
1016 | n/a | m.testcase_1 = MyTestCase |
---|
1017 | n/a | |
---|
1018 | n/a | loader = unittest.TestLoader() |
---|
1019 | n/a | suite = loader.loadTestsFromNames(['testcase_1.test'], m) |
---|
1020 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
1021 | n/a | |
---|
1022 | n/a | ref_suite = unittest.TestSuite([MyTestCase('test')]) |
---|
1023 | n/a | self.assertEqual(list(suite), [ref_suite]) |
---|
1024 | n/a | |
---|
1025 | n/a | # #14971: Make sure the dotted name resolution works even if the actual |
---|
1026 | n/a | # function doesn't have the same name as is used to find it. |
---|
1027 | n/a | def test_loadTestsFromName__function_with_different_name_than_method(self): |
---|
1028 | n/a | # lambdas have the name '<lambda>'. |
---|
1029 | n/a | m = types.ModuleType('m') |
---|
1030 | n/a | class MyTestCase(unittest.TestCase): |
---|
1031 | n/a | test = lambda: 1 |
---|
1032 | n/a | m.testcase_1 = MyTestCase |
---|
1033 | n/a | |
---|
1034 | n/a | loader = unittest.TestLoader() |
---|
1035 | n/a | suite = loader.loadTestsFromNames(['testcase_1.test'], m) |
---|
1036 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
1037 | n/a | |
---|
1038 | n/a | ref_suite = unittest.TestSuite([MyTestCase('test')]) |
---|
1039 | n/a | self.assertEqual(list(suite), [ref_suite]) |
---|
1040 | n/a | |
---|
1041 | n/a | # "The specifier name is a ``dotted name'' that may resolve ... to ... a |
---|
1042 | n/a | # test method within a test case class" |
---|
1043 | n/a | # |
---|
1044 | n/a | # Does the method gracefully handle names that initially look like they |
---|
1045 | n/a | # resolve to "a test method within a test case class" but don't? |
---|
1046 | n/a | def test_loadTestsFromNames__relative_invalid_testmethod(self): |
---|
1047 | n/a | m = types.ModuleType('m') |
---|
1048 | n/a | class MyTestCase(unittest.TestCase): |
---|
1049 | n/a | def test(self): |
---|
1050 | n/a | pass |
---|
1051 | n/a | m.testcase_1 = MyTestCase |
---|
1052 | n/a | |
---|
1053 | n/a | loader = unittest.TestLoader() |
---|
1054 | n/a | suite = loader.loadTestsFromNames(['testcase_1.testfoo'], m) |
---|
1055 | n/a | error, test = self.check_deferred_error(loader, list(suite)[0]) |
---|
1056 | n/a | expected = "type object 'MyTestCase' has no attribute 'testfoo'" |
---|
1057 | n/a | self.assertIn( |
---|
1058 | n/a | expected, error, |
---|
1059 | n/a | 'missing error string in %r' % error) |
---|
1060 | n/a | self.assertRaisesRegex(AttributeError, expected, test.testfoo) |
---|
1061 | n/a | |
---|
1062 | n/a | # "The specifier name is a ``dotted name'' that may resolve ... to |
---|
1063 | n/a | # ... a callable object which returns a ... TestSuite instance" |
---|
1064 | n/a | def test_loadTestsFromNames__callable__TestSuite(self): |
---|
1065 | n/a | m = types.ModuleType('m') |
---|
1066 | n/a | testcase_1 = unittest.FunctionTestCase(lambda: None) |
---|
1067 | n/a | testcase_2 = unittest.FunctionTestCase(lambda: None) |
---|
1068 | n/a | def return_TestSuite(): |
---|
1069 | n/a | return unittest.TestSuite([testcase_1, testcase_2]) |
---|
1070 | n/a | m.return_TestSuite = return_TestSuite |
---|
1071 | n/a | |
---|
1072 | n/a | loader = unittest.TestLoader() |
---|
1073 | n/a | suite = loader.loadTestsFromNames(['return_TestSuite'], m) |
---|
1074 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
1075 | n/a | |
---|
1076 | n/a | expected = unittest.TestSuite([testcase_1, testcase_2]) |
---|
1077 | n/a | self.assertEqual(list(suite), [expected]) |
---|
1078 | n/a | |
---|
1079 | n/a | # "The specifier name is a ``dotted name'' that may resolve ... to |
---|
1080 | n/a | # ... a callable object which returns a TestCase ... instance" |
---|
1081 | n/a | def test_loadTestsFromNames__callable__TestCase_instance(self): |
---|
1082 | n/a | m = types.ModuleType('m') |
---|
1083 | n/a | testcase_1 = unittest.FunctionTestCase(lambda: None) |
---|
1084 | n/a | def return_TestCase(): |
---|
1085 | n/a | return testcase_1 |
---|
1086 | n/a | m.return_TestCase = return_TestCase |
---|
1087 | n/a | |
---|
1088 | n/a | loader = unittest.TestLoader() |
---|
1089 | n/a | suite = loader.loadTestsFromNames(['return_TestCase'], m) |
---|
1090 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
1091 | n/a | |
---|
1092 | n/a | ref_suite = unittest.TestSuite([testcase_1]) |
---|
1093 | n/a | self.assertEqual(list(suite), [ref_suite]) |
---|
1094 | n/a | |
---|
1095 | n/a | # "The specifier name is a ``dotted name'' that may resolve ... to |
---|
1096 | n/a | # ... a callable object which returns a TestCase or TestSuite instance" |
---|
1097 | n/a | # |
---|
1098 | n/a | # Are staticmethods handled correctly? |
---|
1099 | n/a | def test_loadTestsFromNames__callable__call_staticmethod(self): |
---|
1100 | n/a | m = types.ModuleType('m') |
---|
1101 | n/a | class Test1(unittest.TestCase): |
---|
1102 | n/a | def test(self): |
---|
1103 | n/a | pass |
---|
1104 | n/a | |
---|
1105 | n/a | testcase_1 = Test1('test') |
---|
1106 | n/a | class Foo(unittest.TestCase): |
---|
1107 | n/a | @staticmethod |
---|
1108 | n/a | def foo(): |
---|
1109 | n/a | return testcase_1 |
---|
1110 | n/a | m.Foo = Foo |
---|
1111 | n/a | |
---|
1112 | n/a | loader = unittest.TestLoader() |
---|
1113 | n/a | suite = loader.loadTestsFromNames(['Foo.foo'], m) |
---|
1114 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
1115 | n/a | |
---|
1116 | n/a | ref_suite = unittest.TestSuite([testcase_1]) |
---|
1117 | n/a | self.assertEqual(list(suite), [ref_suite]) |
---|
1118 | n/a | |
---|
1119 | n/a | # "The specifier name is a ``dotted name'' that may resolve ... to |
---|
1120 | n/a | # ... a callable object which returns a TestCase or TestSuite instance" |
---|
1121 | n/a | # |
---|
1122 | n/a | # What happens when the callable returns something else? |
---|
1123 | n/a | def test_loadTestsFromNames__callable__wrong_type(self): |
---|
1124 | n/a | m = types.ModuleType('m') |
---|
1125 | n/a | def return_wrong(): |
---|
1126 | n/a | return 6 |
---|
1127 | n/a | m.return_wrong = return_wrong |
---|
1128 | n/a | |
---|
1129 | n/a | loader = unittest.TestLoader() |
---|
1130 | n/a | try: |
---|
1131 | n/a | suite = loader.loadTestsFromNames(['return_wrong'], m) |
---|
1132 | n/a | except TypeError: |
---|
1133 | n/a | pass |
---|
1134 | n/a | else: |
---|
1135 | n/a | self.fail("TestLoader.loadTestsFromNames failed to raise TypeError") |
---|
1136 | n/a | |
---|
1137 | n/a | # "The specifier can refer to modules and packages which have not been |
---|
1138 | n/a | # imported; they will be imported as a side-effect" |
---|
1139 | n/a | def test_loadTestsFromNames__module_not_loaded(self): |
---|
1140 | n/a | # We're going to try to load this module as a side-effect, so it |
---|
1141 | n/a | # better not be loaded before we try. |
---|
1142 | n/a | # |
---|
1143 | n/a | module_name = 'unittest.test.dummy' |
---|
1144 | n/a | sys.modules.pop(module_name, None) |
---|
1145 | n/a | |
---|
1146 | n/a | loader = unittest.TestLoader() |
---|
1147 | n/a | try: |
---|
1148 | n/a | suite = loader.loadTestsFromNames([module_name]) |
---|
1149 | n/a | |
---|
1150 | n/a | self.assertIsInstance(suite, loader.suiteClass) |
---|
1151 | n/a | self.assertEqual(list(suite), [unittest.TestSuite()]) |
---|
1152 | n/a | |
---|
1153 | n/a | # module should now be loaded, thanks to loadTestsFromName() |
---|
1154 | n/a | self.assertIn(module_name, sys.modules) |
---|
1155 | n/a | finally: |
---|
1156 | n/a | if module_name in sys.modules: |
---|
1157 | n/a | del sys.modules[module_name] |
---|
1158 | n/a | |
---|
1159 | n/a | ################################################################ |
---|
1160 | n/a | ### /Tests for TestLoader.loadTestsFromNames() |
---|
1161 | n/a | |
---|
1162 | n/a | ### Tests for TestLoader.getTestCaseNames() |
---|
1163 | n/a | ################################################################ |
---|
1164 | n/a | |
---|
1165 | n/a | # "Return a sorted sequence of method names found within testCaseClass" |
---|
1166 | n/a | # |
---|
1167 | n/a | # Test.foobar is defined to make sure getTestCaseNames() respects |
---|
1168 | n/a | # loader.testMethodPrefix |
---|
1169 | n/a | def test_getTestCaseNames(self): |
---|
1170 | n/a | class Test(unittest.TestCase): |
---|
1171 | n/a | def test_1(self): pass |
---|
1172 | n/a | def test_2(self): pass |
---|
1173 | n/a | def foobar(self): pass |
---|
1174 | n/a | |
---|
1175 | n/a | loader = unittest.TestLoader() |
---|
1176 | n/a | |
---|
1177 | n/a | self.assertEqual(loader.getTestCaseNames(Test), ['test_1', 'test_2']) |
---|
1178 | n/a | |
---|
1179 | n/a | # "Return a sorted sequence of method names found within testCaseClass" |
---|
1180 | n/a | # |
---|
1181 | n/a | # Does getTestCaseNames() behave appropriately if no tests are found? |
---|
1182 | n/a | def test_getTestCaseNames__no_tests(self): |
---|
1183 | n/a | class Test(unittest.TestCase): |
---|
1184 | n/a | def foobar(self): pass |
---|
1185 | n/a | |
---|
1186 | n/a | loader = unittest.TestLoader() |
---|
1187 | n/a | |
---|
1188 | n/a | self.assertEqual(loader.getTestCaseNames(Test), []) |
---|
1189 | n/a | |
---|
1190 | n/a | # "Return a sorted sequence of method names found within testCaseClass" |
---|
1191 | n/a | # |
---|
1192 | n/a | # Are not-TestCases handled gracefully? |
---|
1193 | n/a | # |
---|
1194 | n/a | # XXX This should raise a TypeError, not return a list |
---|
1195 | n/a | # |
---|
1196 | n/a | # XXX It's too late in the 2.5 release cycle to fix this, but it should |
---|
1197 | n/a | # probably be revisited for 2.6 |
---|
1198 | n/a | def test_getTestCaseNames__not_a_TestCase(self): |
---|
1199 | n/a | class BadCase(int): |
---|
1200 | n/a | def test_foo(self): |
---|
1201 | n/a | pass |
---|
1202 | n/a | |
---|
1203 | n/a | loader = unittest.TestLoader() |
---|
1204 | n/a | names = loader.getTestCaseNames(BadCase) |
---|
1205 | n/a | |
---|
1206 | n/a | self.assertEqual(names, ['test_foo']) |
---|
1207 | n/a | |
---|
1208 | n/a | # "Return a sorted sequence of method names found within testCaseClass" |
---|
1209 | n/a | # |
---|
1210 | n/a | # Make sure inherited names are handled. |
---|
1211 | n/a | # |
---|
1212 | n/a | # TestP.foobar is defined to make sure getTestCaseNames() respects |
---|
1213 | n/a | # loader.testMethodPrefix |
---|
1214 | n/a | def test_getTestCaseNames__inheritance(self): |
---|
1215 | n/a | class TestP(unittest.TestCase): |
---|
1216 | n/a | def test_1(self): pass |
---|
1217 | n/a | def test_2(self): pass |
---|
1218 | n/a | def foobar(self): pass |
---|
1219 | n/a | |
---|
1220 | n/a | class TestC(TestP): |
---|
1221 | n/a | def test_1(self): pass |
---|
1222 | n/a | def test_3(self): pass |
---|
1223 | n/a | |
---|
1224 | n/a | loader = unittest.TestLoader() |
---|
1225 | n/a | |
---|
1226 | n/a | names = ['test_1', 'test_2', 'test_3'] |
---|
1227 | n/a | self.assertEqual(loader.getTestCaseNames(TestC), names) |
---|
1228 | n/a | |
---|
1229 | n/a | ################################################################ |
---|
1230 | n/a | ### /Tests for TestLoader.getTestCaseNames() |
---|
1231 | n/a | |
---|
1232 | n/a | ### Tests for TestLoader.testMethodPrefix |
---|
1233 | n/a | ################################################################ |
---|
1234 | n/a | |
---|
1235 | n/a | # "String giving the prefix of method names which will be interpreted as |
---|
1236 | n/a | # test methods" |
---|
1237 | n/a | # |
---|
1238 | n/a | # Implicit in the documentation is that testMethodPrefix is respected by |
---|
1239 | n/a | # all loadTestsFrom* methods. |
---|
1240 | n/a | def test_testMethodPrefix__loadTestsFromTestCase(self): |
---|
1241 | n/a | class Foo(unittest.TestCase): |
---|
1242 | n/a | def test_1(self): pass |
---|
1243 | n/a | def test_2(self): pass |
---|
1244 | n/a | def foo_bar(self): pass |
---|
1245 | n/a | |
---|
1246 | n/a | tests_1 = unittest.TestSuite([Foo('foo_bar')]) |
---|
1247 | n/a | tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')]) |
---|
1248 | n/a | |
---|
1249 | n/a | loader = unittest.TestLoader() |
---|
1250 | n/a | loader.testMethodPrefix = 'foo' |
---|
1251 | n/a | self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_1) |
---|
1252 | n/a | |
---|
1253 | n/a | loader.testMethodPrefix = 'test' |
---|
1254 | n/a | self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_2) |
---|
1255 | n/a | |
---|
1256 | n/a | # "String giving the prefix of method names which will be interpreted as |
---|
1257 | n/a | # test methods" |
---|
1258 | n/a | # |
---|
1259 | n/a | # Implicit in the documentation is that testMethodPrefix is respected by |
---|
1260 | n/a | # all loadTestsFrom* methods. |
---|
1261 | n/a | def test_testMethodPrefix__loadTestsFromModule(self): |
---|
1262 | n/a | m = types.ModuleType('m') |
---|
1263 | n/a | class Foo(unittest.TestCase): |
---|
1264 | n/a | def test_1(self): pass |
---|
1265 | n/a | def test_2(self): pass |
---|
1266 | n/a | def foo_bar(self): pass |
---|
1267 | n/a | m.Foo = Foo |
---|
1268 | n/a | |
---|
1269 | n/a | tests_1 = [unittest.TestSuite([Foo('foo_bar')])] |
---|
1270 | n/a | tests_2 = [unittest.TestSuite([Foo('test_1'), Foo('test_2')])] |
---|
1271 | n/a | |
---|
1272 | n/a | loader = unittest.TestLoader() |
---|
1273 | n/a | loader.testMethodPrefix = 'foo' |
---|
1274 | n/a | self.assertEqual(list(loader.loadTestsFromModule(m)), tests_1) |
---|
1275 | n/a | |
---|
1276 | n/a | loader.testMethodPrefix = 'test' |
---|
1277 | n/a | self.assertEqual(list(loader.loadTestsFromModule(m)), tests_2) |
---|
1278 | n/a | |
---|
1279 | n/a | # "String giving the prefix of method names which will be interpreted as |
---|
1280 | n/a | # test methods" |
---|
1281 | n/a | # |
---|
1282 | n/a | # Implicit in the documentation is that testMethodPrefix is respected by |
---|
1283 | n/a | # all loadTestsFrom* methods. |
---|
1284 | n/a | def test_testMethodPrefix__loadTestsFromName(self): |
---|
1285 | n/a | m = types.ModuleType('m') |
---|
1286 | n/a | class Foo(unittest.TestCase): |
---|
1287 | n/a | def test_1(self): pass |
---|
1288 | n/a | def test_2(self): pass |
---|
1289 | n/a | def foo_bar(self): pass |
---|
1290 | n/a | m.Foo = Foo |
---|
1291 | n/a | |
---|
1292 | n/a | tests_1 = unittest.TestSuite([Foo('foo_bar')]) |
---|
1293 | n/a | tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')]) |
---|
1294 | n/a | |
---|
1295 | n/a | loader = unittest.TestLoader() |
---|
1296 | n/a | loader.testMethodPrefix = 'foo' |
---|
1297 | n/a | self.assertEqual(loader.loadTestsFromName('Foo', m), tests_1) |
---|
1298 | n/a | |
---|
1299 | n/a | loader.testMethodPrefix = 'test' |
---|
1300 | n/a | self.assertEqual(loader.loadTestsFromName('Foo', m), tests_2) |
---|
1301 | n/a | |
---|
1302 | n/a | # "String giving the prefix of method names which will be interpreted as |
---|
1303 | n/a | # test methods" |
---|
1304 | n/a | # |
---|
1305 | n/a | # Implicit in the documentation is that testMethodPrefix is respected by |
---|
1306 | n/a | # all loadTestsFrom* methods. |
---|
1307 | n/a | def test_testMethodPrefix__loadTestsFromNames(self): |
---|
1308 | n/a | m = types.ModuleType('m') |
---|
1309 | n/a | class Foo(unittest.TestCase): |
---|
1310 | n/a | def test_1(self): pass |
---|
1311 | n/a | def test_2(self): pass |
---|
1312 | n/a | def foo_bar(self): pass |
---|
1313 | n/a | m.Foo = Foo |
---|
1314 | n/a | |
---|
1315 | n/a | tests_1 = unittest.TestSuite([unittest.TestSuite([Foo('foo_bar')])]) |
---|
1316 | n/a | tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')]) |
---|
1317 | n/a | tests_2 = unittest.TestSuite([tests_2]) |
---|
1318 | n/a | |
---|
1319 | n/a | loader = unittest.TestLoader() |
---|
1320 | n/a | loader.testMethodPrefix = 'foo' |
---|
1321 | n/a | self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_1) |
---|
1322 | n/a | |
---|
1323 | n/a | loader.testMethodPrefix = 'test' |
---|
1324 | n/a | self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_2) |
---|
1325 | n/a | |
---|
1326 | n/a | # "The default value is 'test'" |
---|
1327 | n/a | def test_testMethodPrefix__default_value(self): |
---|
1328 | n/a | loader = unittest.TestLoader() |
---|
1329 | n/a | self.assertEqual(loader.testMethodPrefix, 'test') |
---|
1330 | n/a | |
---|
1331 | n/a | ################################################################ |
---|
1332 | n/a | ### /Tests for TestLoader.testMethodPrefix |
---|
1333 | n/a | |
---|
1334 | n/a | ### Tests for TestLoader.sortTestMethodsUsing |
---|
1335 | n/a | ################################################################ |
---|
1336 | n/a | |
---|
1337 | n/a | # "Function to be used to compare method names when sorting them in |
---|
1338 | n/a | # getTestCaseNames() and all the loadTestsFromX() methods" |
---|
1339 | n/a | def test_sortTestMethodsUsing__loadTestsFromTestCase(self): |
---|
1340 | n/a | def reversed_cmp(x, y): |
---|
1341 | n/a | return -((x > y) - (x < y)) |
---|
1342 | n/a | |
---|
1343 | n/a | class Foo(unittest.TestCase): |
---|
1344 | n/a | def test_1(self): pass |
---|
1345 | n/a | def test_2(self): pass |
---|
1346 | n/a | |
---|
1347 | n/a | loader = unittest.TestLoader() |
---|
1348 | n/a | loader.sortTestMethodsUsing = reversed_cmp |
---|
1349 | n/a | |
---|
1350 | n/a | tests = loader.suiteClass([Foo('test_2'), Foo('test_1')]) |
---|
1351 | n/a | self.assertEqual(loader.loadTestsFromTestCase(Foo), tests) |
---|
1352 | n/a | |
---|
1353 | n/a | # "Function to be used to compare method names when sorting them in |
---|
1354 | n/a | # getTestCaseNames() and all the loadTestsFromX() methods" |
---|
1355 | n/a | def test_sortTestMethodsUsing__loadTestsFromModule(self): |
---|
1356 | n/a | def reversed_cmp(x, y): |
---|
1357 | n/a | return -((x > y) - (x < y)) |
---|
1358 | n/a | |
---|
1359 | n/a | m = types.ModuleType('m') |
---|
1360 | n/a | class Foo(unittest.TestCase): |
---|
1361 | n/a | def test_1(self): pass |
---|
1362 | n/a | def test_2(self): pass |
---|
1363 | n/a | m.Foo = Foo |
---|
1364 | n/a | |
---|
1365 | n/a | loader = unittest.TestLoader() |
---|
1366 | n/a | loader.sortTestMethodsUsing = reversed_cmp |
---|
1367 | n/a | |
---|
1368 | n/a | tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])] |
---|
1369 | n/a | self.assertEqual(list(loader.loadTestsFromModule(m)), tests) |
---|
1370 | n/a | |
---|
1371 | n/a | # "Function to be used to compare method names when sorting them in |
---|
1372 | n/a | # getTestCaseNames() and all the loadTestsFromX() methods" |
---|
1373 | n/a | def test_sortTestMethodsUsing__loadTestsFromName(self): |
---|
1374 | n/a | def reversed_cmp(x, y): |
---|
1375 | n/a | return -((x > y) - (x < y)) |
---|
1376 | n/a | |
---|
1377 | n/a | m = types.ModuleType('m') |
---|
1378 | n/a | class Foo(unittest.TestCase): |
---|
1379 | n/a | def test_1(self): pass |
---|
1380 | n/a | def test_2(self): pass |
---|
1381 | n/a | m.Foo = Foo |
---|
1382 | n/a | |
---|
1383 | n/a | loader = unittest.TestLoader() |
---|
1384 | n/a | loader.sortTestMethodsUsing = reversed_cmp |
---|
1385 | n/a | |
---|
1386 | n/a | tests = loader.suiteClass([Foo('test_2'), Foo('test_1')]) |
---|
1387 | n/a | self.assertEqual(loader.loadTestsFromName('Foo', m), tests) |
---|
1388 | n/a | |
---|
1389 | n/a | # "Function to be used to compare method names when sorting them in |
---|
1390 | n/a | # getTestCaseNames() and all the loadTestsFromX() methods" |
---|
1391 | n/a | def test_sortTestMethodsUsing__loadTestsFromNames(self): |
---|
1392 | n/a | def reversed_cmp(x, y): |
---|
1393 | n/a | return -((x > y) - (x < y)) |
---|
1394 | n/a | |
---|
1395 | n/a | m = types.ModuleType('m') |
---|
1396 | n/a | class Foo(unittest.TestCase): |
---|
1397 | n/a | def test_1(self): pass |
---|
1398 | n/a | def test_2(self): pass |
---|
1399 | n/a | m.Foo = Foo |
---|
1400 | n/a | |
---|
1401 | n/a | loader = unittest.TestLoader() |
---|
1402 | n/a | loader.sortTestMethodsUsing = reversed_cmp |
---|
1403 | n/a | |
---|
1404 | n/a | tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])] |
---|
1405 | n/a | self.assertEqual(list(loader.loadTestsFromNames(['Foo'], m)), tests) |
---|
1406 | n/a | |
---|
1407 | n/a | # "Function to be used to compare method names when sorting them in |
---|
1408 | n/a | # getTestCaseNames()" |
---|
1409 | n/a | # |
---|
1410 | n/a | # Does it actually affect getTestCaseNames()? |
---|
1411 | n/a | def test_sortTestMethodsUsing__getTestCaseNames(self): |
---|
1412 | n/a | def reversed_cmp(x, y): |
---|
1413 | n/a | return -((x > y) - (x < y)) |
---|
1414 | n/a | |
---|
1415 | n/a | class Foo(unittest.TestCase): |
---|
1416 | n/a | def test_1(self): pass |
---|
1417 | n/a | def test_2(self): pass |
---|
1418 | n/a | |
---|
1419 | n/a | loader = unittest.TestLoader() |
---|
1420 | n/a | loader.sortTestMethodsUsing = reversed_cmp |
---|
1421 | n/a | |
---|
1422 | n/a | test_names = ['test_2', 'test_1'] |
---|
1423 | n/a | self.assertEqual(loader.getTestCaseNames(Foo), test_names) |
---|
1424 | n/a | |
---|
1425 | n/a | # "The default value is the built-in cmp() function" |
---|
1426 | n/a | # Since cmp is now defunct, we simply verify that the results |
---|
1427 | n/a | # occur in the same order as they would with the default sort. |
---|
1428 | n/a | def test_sortTestMethodsUsing__default_value(self): |
---|
1429 | n/a | loader = unittest.TestLoader() |
---|
1430 | n/a | |
---|
1431 | n/a | class Foo(unittest.TestCase): |
---|
1432 | n/a | def test_2(self): pass |
---|
1433 | n/a | def test_3(self): pass |
---|
1434 | n/a | def test_1(self): pass |
---|
1435 | n/a | |
---|
1436 | n/a | test_names = ['test_2', 'test_3', 'test_1'] |
---|
1437 | n/a | self.assertEqual(loader.getTestCaseNames(Foo), sorted(test_names)) |
---|
1438 | n/a | |
---|
1439 | n/a | |
---|
1440 | n/a | # "it can be set to None to disable the sort." |
---|
1441 | n/a | # |
---|
1442 | n/a | # XXX How is this different from reassigning cmp? Are the tests returned |
---|
1443 | n/a | # in a random order or something? This behaviour should die |
---|
1444 | n/a | def test_sortTestMethodsUsing__None(self): |
---|
1445 | n/a | class Foo(unittest.TestCase): |
---|
1446 | n/a | def test_1(self): pass |
---|
1447 | n/a | def test_2(self): pass |
---|
1448 | n/a | |
---|
1449 | n/a | loader = unittest.TestLoader() |
---|
1450 | n/a | loader.sortTestMethodsUsing = None |
---|
1451 | n/a | |
---|
1452 | n/a | test_names = ['test_2', 'test_1'] |
---|
1453 | n/a | self.assertEqual(set(loader.getTestCaseNames(Foo)), set(test_names)) |
---|
1454 | n/a | |
---|
1455 | n/a | ################################################################ |
---|
1456 | n/a | ### /Tests for TestLoader.sortTestMethodsUsing |
---|
1457 | n/a | |
---|
1458 | n/a | ### Tests for TestLoader.suiteClass |
---|
1459 | n/a | ################################################################ |
---|
1460 | n/a | |
---|
1461 | n/a | # "Callable object that constructs a test suite from a list of tests." |
---|
1462 | n/a | def test_suiteClass__loadTestsFromTestCase(self): |
---|
1463 | n/a | class Foo(unittest.TestCase): |
---|
1464 | n/a | def test_1(self): pass |
---|
1465 | n/a | def test_2(self): pass |
---|
1466 | n/a | def foo_bar(self): pass |
---|
1467 | n/a | |
---|
1468 | n/a | tests = [Foo('test_1'), Foo('test_2')] |
---|
1469 | n/a | |
---|
1470 | n/a | loader = unittest.TestLoader() |
---|
1471 | n/a | loader.suiteClass = list |
---|
1472 | n/a | self.assertEqual(loader.loadTestsFromTestCase(Foo), tests) |
---|
1473 | n/a | |
---|
1474 | n/a | # It is implicit in the documentation for TestLoader.suiteClass that |
---|
1475 | n/a | # all TestLoader.loadTestsFrom* methods respect it. Let's make sure |
---|
1476 | n/a | def test_suiteClass__loadTestsFromModule(self): |
---|
1477 | n/a | m = types.ModuleType('m') |
---|
1478 | n/a | class Foo(unittest.TestCase): |
---|
1479 | n/a | def test_1(self): pass |
---|
1480 | n/a | def test_2(self): pass |
---|
1481 | n/a | def foo_bar(self): pass |
---|
1482 | n/a | m.Foo = Foo |
---|
1483 | n/a | |
---|
1484 | n/a | tests = [[Foo('test_1'), Foo('test_2')]] |
---|
1485 | n/a | |
---|
1486 | n/a | loader = unittest.TestLoader() |
---|
1487 | n/a | loader.suiteClass = list |
---|
1488 | n/a | self.assertEqual(loader.loadTestsFromModule(m), tests) |
---|
1489 | n/a | |
---|
1490 | n/a | # It is implicit in the documentation for TestLoader.suiteClass that |
---|
1491 | n/a | # all TestLoader.loadTestsFrom* methods respect it. Let's make sure |
---|
1492 | n/a | def test_suiteClass__loadTestsFromName(self): |
---|
1493 | n/a | m = types.ModuleType('m') |
---|
1494 | n/a | class Foo(unittest.TestCase): |
---|
1495 | n/a | def test_1(self): pass |
---|
1496 | n/a | def test_2(self): pass |
---|
1497 | n/a | def foo_bar(self): pass |
---|
1498 | n/a | m.Foo = Foo |
---|
1499 | n/a | |
---|
1500 | n/a | tests = [Foo('test_1'), Foo('test_2')] |
---|
1501 | n/a | |
---|
1502 | n/a | loader = unittest.TestLoader() |
---|
1503 | n/a | loader.suiteClass = list |
---|
1504 | n/a | self.assertEqual(loader.loadTestsFromName('Foo', m), tests) |
---|
1505 | n/a | |
---|
1506 | n/a | # It is implicit in the documentation for TestLoader.suiteClass that |
---|
1507 | n/a | # all TestLoader.loadTestsFrom* methods respect it. Let's make sure |
---|
1508 | n/a | def test_suiteClass__loadTestsFromNames(self): |
---|
1509 | n/a | m = types.ModuleType('m') |
---|
1510 | n/a | class Foo(unittest.TestCase): |
---|
1511 | n/a | def test_1(self): pass |
---|
1512 | n/a | def test_2(self): pass |
---|
1513 | n/a | def foo_bar(self): pass |
---|
1514 | n/a | m.Foo = Foo |
---|
1515 | n/a | |
---|
1516 | n/a | tests = [[Foo('test_1'), Foo('test_2')]] |
---|
1517 | n/a | |
---|
1518 | n/a | loader = unittest.TestLoader() |
---|
1519 | n/a | loader.suiteClass = list |
---|
1520 | n/a | self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests) |
---|
1521 | n/a | |
---|
1522 | n/a | # "The default value is the TestSuite class" |
---|
1523 | n/a | def test_suiteClass__default_value(self): |
---|
1524 | n/a | loader = unittest.TestLoader() |
---|
1525 | n/a | self.assertIs(loader.suiteClass, unittest.TestSuite) |
---|
1526 | n/a | |
---|
1527 | n/a | |
---|
1528 | n/a | if __name__ == "__main__": |
---|
1529 | n/a | unittest.main() |
---|