1 | n/a | import io |
---|
2 | n/a | |
---|
3 | n/a | import os |
---|
4 | n/a | import sys |
---|
5 | n/a | from test import support |
---|
6 | n/a | import unittest |
---|
7 | n/a | import unittest.test |
---|
8 | n/a | |
---|
9 | n/a | |
---|
10 | n/a | class Test_TestProgram(unittest.TestCase): |
---|
11 | n/a | |
---|
12 | n/a | def test_discovery_from_dotted_path(self): |
---|
13 | n/a | loader = unittest.TestLoader() |
---|
14 | n/a | |
---|
15 | n/a | tests = [self] |
---|
16 | n/a | expectedPath = os.path.abspath(os.path.dirname(unittest.test.__file__)) |
---|
17 | n/a | |
---|
18 | n/a | self.wasRun = False |
---|
19 | n/a | def _find_tests(start_dir, pattern): |
---|
20 | n/a | self.wasRun = True |
---|
21 | n/a | self.assertEqual(start_dir, expectedPath) |
---|
22 | n/a | return tests |
---|
23 | n/a | loader._find_tests = _find_tests |
---|
24 | n/a | suite = loader.discover('unittest.test') |
---|
25 | n/a | self.assertTrue(self.wasRun) |
---|
26 | n/a | self.assertEqual(suite._tests, tests) |
---|
27 | n/a | |
---|
28 | n/a | # Horrible white box test |
---|
29 | n/a | def testNoExit(self): |
---|
30 | n/a | result = object() |
---|
31 | n/a | test = object() |
---|
32 | n/a | |
---|
33 | n/a | class FakeRunner(object): |
---|
34 | n/a | def run(self, test): |
---|
35 | n/a | self.test = test |
---|
36 | n/a | return result |
---|
37 | n/a | |
---|
38 | n/a | runner = FakeRunner() |
---|
39 | n/a | |
---|
40 | n/a | oldParseArgs = unittest.TestProgram.parseArgs |
---|
41 | n/a | def restoreParseArgs(): |
---|
42 | n/a | unittest.TestProgram.parseArgs = oldParseArgs |
---|
43 | n/a | unittest.TestProgram.parseArgs = lambda *args: None |
---|
44 | n/a | self.addCleanup(restoreParseArgs) |
---|
45 | n/a | |
---|
46 | n/a | def removeTest(): |
---|
47 | n/a | del unittest.TestProgram.test |
---|
48 | n/a | unittest.TestProgram.test = test |
---|
49 | n/a | self.addCleanup(removeTest) |
---|
50 | n/a | |
---|
51 | n/a | program = unittest.TestProgram(testRunner=runner, exit=False, verbosity=2) |
---|
52 | n/a | |
---|
53 | n/a | self.assertEqual(program.result, result) |
---|
54 | n/a | self.assertEqual(runner.test, test) |
---|
55 | n/a | self.assertEqual(program.verbosity, 2) |
---|
56 | n/a | |
---|
57 | n/a | class FooBar(unittest.TestCase): |
---|
58 | n/a | def testPass(self): |
---|
59 | n/a | assert True |
---|
60 | n/a | def testFail(self): |
---|
61 | n/a | assert False |
---|
62 | n/a | |
---|
63 | n/a | class FooBarLoader(unittest.TestLoader): |
---|
64 | n/a | """Test loader that returns a suite containing FooBar.""" |
---|
65 | n/a | def loadTestsFromModule(self, module): |
---|
66 | n/a | return self.suiteClass( |
---|
67 | n/a | [self.loadTestsFromTestCase(Test_TestProgram.FooBar)]) |
---|
68 | n/a | |
---|
69 | n/a | def loadTestsFromNames(self, names, module): |
---|
70 | n/a | return self.suiteClass( |
---|
71 | n/a | [self.loadTestsFromTestCase(Test_TestProgram.FooBar)]) |
---|
72 | n/a | |
---|
73 | n/a | def test_defaultTest_with_string(self): |
---|
74 | n/a | class FakeRunner(object): |
---|
75 | n/a | def run(self, test): |
---|
76 | n/a | self.test = test |
---|
77 | n/a | return True |
---|
78 | n/a | |
---|
79 | n/a | old_argv = sys.argv |
---|
80 | n/a | sys.argv = ['faketest'] |
---|
81 | n/a | runner = FakeRunner() |
---|
82 | n/a | program = unittest.TestProgram(testRunner=runner, exit=False, |
---|
83 | n/a | defaultTest='unittest.test', |
---|
84 | n/a | testLoader=self.FooBarLoader()) |
---|
85 | n/a | sys.argv = old_argv |
---|
86 | n/a | self.assertEqual(('unittest.test',), program.testNames) |
---|
87 | n/a | |
---|
88 | n/a | def test_defaultTest_with_iterable(self): |
---|
89 | n/a | class FakeRunner(object): |
---|
90 | n/a | def run(self, test): |
---|
91 | n/a | self.test = test |
---|
92 | n/a | return True |
---|
93 | n/a | |
---|
94 | n/a | old_argv = sys.argv |
---|
95 | n/a | sys.argv = ['faketest'] |
---|
96 | n/a | runner = FakeRunner() |
---|
97 | n/a | program = unittest.TestProgram( |
---|
98 | n/a | testRunner=runner, exit=False, |
---|
99 | n/a | defaultTest=['unittest.test', 'unittest.test2'], |
---|
100 | n/a | testLoader=self.FooBarLoader()) |
---|
101 | n/a | sys.argv = old_argv |
---|
102 | n/a | self.assertEqual(['unittest.test', 'unittest.test2'], |
---|
103 | n/a | program.testNames) |
---|
104 | n/a | |
---|
105 | n/a | def test_NonExit(self): |
---|
106 | n/a | program = unittest.main(exit=False, |
---|
107 | n/a | argv=["foobar"], |
---|
108 | n/a | testRunner=unittest.TextTestRunner(stream=io.StringIO()), |
---|
109 | n/a | testLoader=self.FooBarLoader()) |
---|
110 | n/a | self.assertTrue(hasattr(program, 'result')) |
---|
111 | n/a | |
---|
112 | n/a | |
---|
113 | n/a | def test_Exit(self): |
---|
114 | n/a | self.assertRaises( |
---|
115 | n/a | SystemExit, |
---|
116 | n/a | unittest.main, |
---|
117 | n/a | argv=["foobar"], |
---|
118 | n/a | testRunner=unittest.TextTestRunner(stream=io.StringIO()), |
---|
119 | n/a | exit=True, |
---|
120 | n/a | testLoader=self.FooBarLoader()) |
---|
121 | n/a | |
---|
122 | n/a | |
---|
123 | n/a | def test_ExitAsDefault(self): |
---|
124 | n/a | self.assertRaises( |
---|
125 | n/a | SystemExit, |
---|
126 | n/a | unittest.main, |
---|
127 | n/a | argv=["foobar"], |
---|
128 | n/a | testRunner=unittest.TextTestRunner(stream=io.StringIO()), |
---|
129 | n/a | testLoader=self.FooBarLoader()) |
---|
130 | n/a | |
---|
131 | n/a | |
---|
132 | n/a | class InitialisableProgram(unittest.TestProgram): |
---|
133 | n/a | exit = False |
---|
134 | n/a | result = None |
---|
135 | n/a | verbosity = 1 |
---|
136 | n/a | defaultTest = None |
---|
137 | n/a | tb_locals = False |
---|
138 | n/a | testRunner = None |
---|
139 | n/a | testLoader = unittest.defaultTestLoader |
---|
140 | n/a | module = '__main__' |
---|
141 | n/a | progName = 'test' |
---|
142 | n/a | test = 'test' |
---|
143 | n/a | def __init__(self, *args): |
---|
144 | n/a | pass |
---|
145 | n/a | |
---|
146 | n/a | RESULT = object() |
---|
147 | n/a | |
---|
148 | n/a | class FakeRunner(object): |
---|
149 | n/a | initArgs = None |
---|
150 | n/a | test = None |
---|
151 | n/a | raiseError = 0 |
---|
152 | n/a | |
---|
153 | n/a | def __init__(self, **kwargs): |
---|
154 | n/a | FakeRunner.initArgs = kwargs |
---|
155 | n/a | if FakeRunner.raiseError: |
---|
156 | n/a | FakeRunner.raiseError -= 1 |
---|
157 | n/a | raise TypeError |
---|
158 | n/a | |
---|
159 | n/a | def run(self, test): |
---|
160 | n/a | FakeRunner.test = test |
---|
161 | n/a | return RESULT |
---|
162 | n/a | |
---|
163 | n/a | |
---|
164 | n/a | class TestCommandLineArgs(unittest.TestCase): |
---|
165 | n/a | |
---|
166 | n/a | def setUp(self): |
---|
167 | n/a | self.program = InitialisableProgram() |
---|
168 | n/a | self.program.createTests = lambda: None |
---|
169 | n/a | FakeRunner.initArgs = None |
---|
170 | n/a | FakeRunner.test = None |
---|
171 | n/a | FakeRunner.raiseError = 0 |
---|
172 | n/a | |
---|
173 | n/a | def testVerbosity(self): |
---|
174 | n/a | program = self.program |
---|
175 | n/a | |
---|
176 | n/a | for opt in '-q', '--quiet': |
---|
177 | n/a | program.verbosity = 1 |
---|
178 | n/a | program.parseArgs([None, opt]) |
---|
179 | n/a | self.assertEqual(program.verbosity, 0) |
---|
180 | n/a | |
---|
181 | n/a | for opt in '-v', '--verbose': |
---|
182 | n/a | program.verbosity = 1 |
---|
183 | n/a | program.parseArgs([None, opt]) |
---|
184 | n/a | self.assertEqual(program.verbosity, 2) |
---|
185 | n/a | |
---|
186 | n/a | def testBufferCatchFailfast(self): |
---|
187 | n/a | program = self.program |
---|
188 | n/a | for arg, attr in (('buffer', 'buffer'), ('failfast', 'failfast'), |
---|
189 | n/a | ('catch', 'catchbreak')): |
---|
190 | n/a | if attr == 'catch' and not hasInstallHandler: |
---|
191 | n/a | continue |
---|
192 | n/a | |
---|
193 | n/a | setattr(program, attr, None) |
---|
194 | n/a | program.parseArgs([None]) |
---|
195 | n/a | self.assertIs(getattr(program, attr), False) |
---|
196 | n/a | |
---|
197 | n/a | false = [] |
---|
198 | n/a | setattr(program, attr, false) |
---|
199 | n/a | program.parseArgs([None]) |
---|
200 | n/a | self.assertIs(getattr(program, attr), false) |
---|
201 | n/a | |
---|
202 | n/a | true = [42] |
---|
203 | n/a | setattr(program, attr, true) |
---|
204 | n/a | program.parseArgs([None]) |
---|
205 | n/a | self.assertIs(getattr(program, attr), true) |
---|
206 | n/a | |
---|
207 | n/a | short_opt = '-%s' % arg[0] |
---|
208 | n/a | long_opt = '--%s' % arg |
---|
209 | n/a | for opt in short_opt, long_opt: |
---|
210 | n/a | setattr(program, attr, None) |
---|
211 | n/a | program.parseArgs([None, opt]) |
---|
212 | n/a | self.assertIs(getattr(program, attr), True) |
---|
213 | n/a | |
---|
214 | n/a | setattr(program, attr, False) |
---|
215 | n/a | with support.captured_stderr() as stderr, \ |
---|
216 | n/a | self.assertRaises(SystemExit) as cm: |
---|
217 | n/a | program.parseArgs([None, opt]) |
---|
218 | n/a | self.assertEqual(cm.exception.args, (2,)) |
---|
219 | n/a | |
---|
220 | n/a | setattr(program, attr, True) |
---|
221 | n/a | with support.captured_stderr() as stderr, \ |
---|
222 | n/a | self.assertRaises(SystemExit) as cm: |
---|
223 | n/a | program.parseArgs([None, opt]) |
---|
224 | n/a | self.assertEqual(cm.exception.args, (2,)) |
---|
225 | n/a | |
---|
226 | n/a | def testWarning(self): |
---|
227 | n/a | """Test the warnings argument""" |
---|
228 | n/a | # see #10535 |
---|
229 | n/a | class FakeTP(unittest.TestProgram): |
---|
230 | n/a | def parseArgs(self, *args, **kw): pass |
---|
231 | n/a | def runTests(self, *args, **kw): pass |
---|
232 | n/a | warnoptions = sys.warnoptions[:] |
---|
233 | n/a | try: |
---|
234 | n/a | sys.warnoptions[:] = [] |
---|
235 | n/a | # no warn options, no arg -> default |
---|
236 | n/a | self.assertEqual(FakeTP().warnings, 'default') |
---|
237 | n/a | # no warn options, w/ arg -> arg value |
---|
238 | n/a | self.assertEqual(FakeTP(warnings='ignore').warnings, 'ignore') |
---|
239 | n/a | sys.warnoptions[:] = ['somevalue'] |
---|
240 | n/a | # warn options, no arg -> None |
---|
241 | n/a | # warn options, w/ arg -> arg value |
---|
242 | n/a | self.assertEqual(FakeTP().warnings, None) |
---|
243 | n/a | self.assertEqual(FakeTP(warnings='ignore').warnings, 'ignore') |
---|
244 | n/a | finally: |
---|
245 | n/a | sys.warnoptions[:] = warnoptions |
---|
246 | n/a | |
---|
247 | n/a | def testRunTestsRunnerClass(self): |
---|
248 | n/a | program = self.program |
---|
249 | n/a | |
---|
250 | n/a | program.testRunner = FakeRunner |
---|
251 | n/a | program.verbosity = 'verbosity' |
---|
252 | n/a | program.failfast = 'failfast' |
---|
253 | n/a | program.buffer = 'buffer' |
---|
254 | n/a | program.warnings = 'warnings' |
---|
255 | n/a | |
---|
256 | n/a | program.runTests() |
---|
257 | n/a | |
---|
258 | n/a | self.assertEqual(FakeRunner.initArgs, {'verbosity': 'verbosity', |
---|
259 | n/a | 'failfast': 'failfast', |
---|
260 | n/a | 'buffer': 'buffer', |
---|
261 | n/a | 'tb_locals': False, |
---|
262 | n/a | 'warnings': 'warnings'}) |
---|
263 | n/a | self.assertEqual(FakeRunner.test, 'test') |
---|
264 | n/a | self.assertIs(program.result, RESULT) |
---|
265 | n/a | |
---|
266 | n/a | def testRunTestsRunnerInstance(self): |
---|
267 | n/a | program = self.program |
---|
268 | n/a | |
---|
269 | n/a | program.testRunner = FakeRunner() |
---|
270 | n/a | FakeRunner.initArgs = None |
---|
271 | n/a | |
---|
272 | n/a | program.runTests() |
---|
273 | n/a | |
---|
274 | n/a | # A new FakeRunner should not have been instantiated |
---|
275 | n/a | self.assertIsNone(FakeRunner.initArgs) |
---|
276 | n/a | |
---|
277 | n/a | self.assertEqual(FakeRunner.test, 'test') |
---|
278 | n/a | self.assertIs(program.result, RESULT) |
---|
279 | n/a | |
---|
280 | n/a | def test_locals(self): |
---|
281 | n/a | program = self.program |
---|
282 | n/a | |
---|
283 | n/a | program.testRunner = FakeRunner |
---|
284 | n/a | program.parseArgs([None, '--locals']) |
---|
285 | n/a | self.assertEqual(True, program.tb_locals) |
---|
286 | n/a | program.runTests() |
---|
287 | n/a | self.assertEqual(FakeRunner.initArgs, {'buffer': False, |
---|
288 | n/a | 'failfast': False, |
---|
289 | n/a | 'tb_locals': True, |
---|
290 | n/a | 'verbosity': 1, |
---|
291 | n/a | 'warnings': None}) |
---|
292 | n/a | |
---|
293 | n/a | def testRunTestsOldRunnerClass(self): |
---|
294 | n/a | program = self.program |
---|
295 | n/a | |
---|
296 | n/a | # Two TypeErrors are needed to fall all the way back to old-style |
---|
297 | n/a | # runners - one to fail tb_locals, one to fail buffer etc. |
---|
298 | n/a | FakeRunner.raiseError = 2 |
---|
299 | n/a | program.testRunner = FakeRunner |
---|
300 | n/a | program.verbosity = 'verbosity' |
---|
301 | n/a | program.failfast = 'failfast' |
---|
302 | n/a | program.buffer = 'buffer' |
---|
303 | n/a | program.test = 'test' |
---|
304 | n/a | |
---|
305 | n/a | program.runTests() |
---|
306 | n/a | |
---|
307 | n/a | # If initialising raises a type error it should be retried |
---|
308 | n/a | # without the new keyword arguments |
---|
309 | n/a | self.assertEqual(FakeRunner.initArgs, {}) |
---|
310 | n/a | self.assertEqual(FakeRunner.test, 'test') |
---|
311 | n/a | self.assertIs(program.result, RESULT) |
---|
312 | n/a | |
---|
313 | n/a | def testCatchBreakInstallsHandler(self): |
---|
314 | n/a | module = sys.modules['unittest.main'] |
---|
315 | n/a | original = module.installHandler |
---|
316 | n/a | def restore(): |
---|
317 | n/a | module.installHandler = original |
---|
318 | n/a | self.addCleanup(restore) |
---|
319 | n/a | |
---|
320 | n/a | self.installed = False |
---|
321 | n/a | def fakeInstallHandler(): |
---|
322 | n/a | self.installed = True |
---|
323 | n/a | module.installHandler = fakeInstallHandler |
---|
324 | n/a | |
---|
325 | n/a | program = self.program |
---|
326 | n/a | program.catchbreak = True |
---|
327 | n/a | |
---|
328 | n/a | program.testRunner = FakeRunner |
---|
329 | n/a | |
---|
330 | n/a | program.runTests() |
---|
331 | n/a | self.assertTrue(self.installed) |
---|
332 | n/a | |
---|
333 | n/a | def _patch_isfile(self, names, exists=True): |
---|
334 | n/a | def isfile(path): |
---|
335 | n/a | return path in names |
---|
336 | n/a | original = os.path.isfile |
---|
337 | n/a | os.path.isfile = isfile |
---|
338 | n/a | def restore(): |
---|
339 | n/a | os.path.isfile = original |
---|
340 | n/a | self.addCleanup(restore) |
---|
341 | n/a | |
---|
342 | n/a | |
---|
343 | n/a | def testParseArgsFileNames(self): |
---|
344 | n/a | # running tests with filenames instead of module names |
---|
345 | n/a | program = self.program |
---|
346 | n/a | argv = ['progname', 'foo.py', 'bar.Py', 'baz.PY', 'wing.txt'] |
---|
347 | n/a | self._patch_isfile(argv) |
---|
348 | n/a | |
---|
349 | n/a | program.createTests = lambda: None |
---|
350 | n/a | program.parseArgs(argv) |
---|
351 | n/a | |
---|
352 | n/a | # note that 'wing.txt' is not a Python file so the name should |
---|
353 | n/a | # *not* be converted to a module name |
---|
354 | n/a | expected = ['foo', 'bar', 'baz', 'wing.txt'] |
---|
355 | n/a | self.assertEqual(program.testNames, expected) |
---|
356 | n/a | |
---|
357 | n/a | |
---|
358 | n/a | def testParseArgsFilePaths(self): |
---|
359 | n/a | program = self.program |
---|
360 | n/a | argv = ['progname', 'foo/bar/baz.py', 'green\\red.py'] |
---|
361 | n/a | self._patch_isfile(argv) |
---|
362 | n/a | |
---|
363 | n/a | program.createTests = lambda: None |
---|
364 | n/a | program.parseArgs(argv) |
---|
365 | n/a | |
---|
366 | n/a | expected = ['foo.bar.baz', 'green.red'] |
---|
367 | n/a | self.assertEqual(program.testNames, expected) |
---|
368 | n/a | |
---|
369 | n/a | |
---|
370 | n/a | def testParseArgsNonExistentFiles(self): |
---|
371 | n/a | program = self.program |
---|
372 | n/a | argv = ['progname', 'foo/bar/baz.py', 'green\\red.py'] |
---|
373 | n/a | self._patch_isfile([]) |
---|
374 | n/a | |
---|
375 | n/a | program.createTests = lambda: None |
---|
376 | n/a | program.parseArgs(argv) |
---|
377 | n/a | |
---|
378 | n/a | self.assertEqual(program.testNames, argv[1:]) |
---|
379 | n/a | |
---|
380 | n/a | def testParseArgsAbsolutePathsThatCanBeConverted(self): |
---|
381 | n/a | cur_dir = os.getcwd() |
---|
382 | n/a | program = self.program |
---|
383 | n/a | def _join(name): |
---|
384 | n/a | return os.path.join(cur_dir, name) |
---|
385 | n/a | argv = ['progname', _join('foo/bar/baz.py'), _join('green\\red.py')] |
---|
386 | n/a | self._patch_isfile(argv) |
---|
387 | n/a | |
---|
388 | n/a | program.createTests = lambda: None |
---|
389 | n/a | program.parseArgs(argv) |
---|
390 | n/a | |
---|
391 | n/a | expected = ['foo.bar.baz', 'green.red'] |
---|
392 | n/a | self.assertEqual(program.testNames, expected) |
---|
393 | n/a | |
---|
394 | n/a | def testParseArgsAbsolutePathsThatCannotBeConverted(self): |
---|
395 | n/a | program = self.program |
---|
396 | n/a | # even on Windows '/...' is considered absolute by os.path.abspath |
---|
397 | n/a | argv = ['progname', '/foo/bar/baz.py', '/green/red.py'] |
---|
398 | n/a | self._patch_isfile(argv) |
---|
399 | n/a | |
---|
400 | n/a | program.createTests = lambda: None |
---|
401 | n/a | program.parseArgs(argv) |
---|
402 | n/a | |
---|
403 | n/a | self.assertEqual(program.testNames, argv[1:]) |
---|
404 | n/a | |
---|
405 | n/a | # it may be better to use platform specific functions to normalise paths |
---|
406 | n/a | # rather than accepting '.PY' and '\' as file separator on Linux / Mac |
---|
407 | n/a | # it would also be better to check that a filename is a valid module |
---|
408 | n/a | # identifier (we have a regex for this in loader.py) |
---|
409 | n/a | # for invalid filenames should we raise a useful error rather than |
---|
410 | n/a | # leaving the current error message (import of filename fails) in place? |
---|
411 | n/a | |
---|
412 | n/a | |
---|
413 | n/a | if __name__ == '__main__': |
---|
414 | n/a | unittest.main() |
---|