blob: 2e33555afec763975765c187d65f1ecad7859f07 [file] [log] [blame]
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -08001"""
2Tests of regrtest.py.
Victor Stinner6b415102015-09-28 23:16:17 +02003
4Note: test_regrtest cannot be run twice in parallel.
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -08005"""
6
7import argparse
Charles-François Natali833bf1f2013-09-08 12:27:33 +02008import faulthandler
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -08009import getopt
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030010import os.path
Victor Stinner6b415102015-09-28 23:16:17 +020011import platform
12import re
13import subprocess
14import sys
15import textwrap
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080016import unittest
Victor Stinner3844fe52015-09-26 10:38:01 +020017from test import libregrtest
18from test import support
Victor Stinner6b415102015-09-28 23:16:17 +020019
20
21Py_DEBUG = hasattr(sys, 'getobjects')
22ROOT_DIR = os.path.join(os.path.dirname(__file__), '..', '..')
23ROOT_DIR = os.path.abspath(os.path.normpath(ROOT_DIR))
24
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080025
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080026class ParseArgsTestCase(unittest.TestCase):
Victor Stinner6b415102015-09-28 23:16:17 +020027 """
28 Test regrtest's argument parsing, function _parse_args().
29 """
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080030
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030031 def checkError(self, args, msg):
32 with support.captured_stderr() as err, self.assertRaises(SystemExit):
Victor Stinner3844fe52015-09-26 10:38:01 +020033 libregrtest._parse_args(args)
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030034 self.assertIn(msg, err.getvalue())
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080035
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030036 def test_help(self):
37 for opt in '-h', '--help':
38 with self.subTest(opt=opt):
39 with support.captured_stdout() as out, \
40 self.assertRaises(SystemExit):
Victor Stinner3844fe52015-09-26 10:38:01 +020041 libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030042 self.assertIn('Run Python regression tests.', out.getvalue())
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080043
Charles-François Natali833bf1f2013-09-08 12:27:33 +020044 @unittest.skipUnless(hasattr(faulthandler, 'dump_traceback_later'),
45 "faulthandler.dump_traceback_later() required")
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030046 def test_timeout(self):
Victor Stinner3844fe52015-09-26 10:38:01 +020047 ns = libregrtest._parse_args(['--timeout', '4.2'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030048 self.assertEqual(ns.timeout, 4.2)
49 self.checkError(['--timeout'], 'expected one argument')
50 self.checkError(['--timeout', 'foo'], 'invalid float value')
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080051
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030052 def test_wait(self):
Victor Stinner3844fe52015-09-26 10:38:01 +020053 ns = libregrtest._parse_args(['--wait'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030054 self.assertTrue(ns.wait)
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080055
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030056 def test_slaveargs(self):
Victor Stinner3844fe52015-09-26 10:38:01 +020057 ns = libregrtest._parse_args(['--slaveargs', '[[], {}]'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030058 self.assertEqual(ns.slaveargs, '[[], {}]')
59 self.checkError(['--slaveargs'], 'expected one argument')
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080060
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030061 def test_start(self):
62 for opt in '-S', '--start':
63 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +020064 ns = libregrtest._parse_args([opt, 'foo'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030065 self.assertEqual(ns.start, 'foo')
66 self.checkError([opt], 'expected one argument')
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080067
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030068 def test_verbose(self):
Victor Stinner3844fe52015-09-26 10:38:01 +020069 ns = libregrtest._parse_args(['-v'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030070 self.assertEqual(ns.verbose, 1)
Victor Stinner3844fe52015-09-26 10:38:01 +020071 ns = libregrtest._parse_args(['-vvv'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030072 self.assertEqual(ns.verbose, 3)
Victor Stinner3844fe52015-09-26 10:38:01 +020073 ns = libregrtest._parse_args(['--verbose'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030074 self.assertEqual(ns.verbose, 1)
Victor Stinner3844fe52015-09-26 10:38:01 +020075 ns = libregrtest._parse_args(['--verbose'] * 3)
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030076 self.assertEqual(ns.verbose, 3)
Victor Stinner3844fe52015-09-26 10:38:01 +020077 ns = libregrtest._parse_args([])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030078 self.assertEqual(ns.verbose, 0)
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080079
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030080 def test_verbose2(self):
81 for opt in '-w', '--verbose2':
82 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +020083 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030084 self.assertTrue(ns.verbose2)
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080085
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030086 def test_verbose3(self):
87 for opt in '-W', '--verbose3':
88 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +020089 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030090 self.assertTrue(ns.verbose3)
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080091
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030092 def test_quiet(self):
93 for opt in '-q', '--quiet':
94 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +020095 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030096 self.assertTrue(ns.quiet)
97 self.assertEqual(ns.verbose, 0)
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080098
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030099 def test_slow(self):
100 for opt in '-o', '--slow':
101 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200102 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300103 self.assertTrue(ns.print_slow)
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -0800104
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300105 def test_header(self):
Victor Stinner3844fe52015-09-26 10:38:01 +0200106 ns = libregrtest._parse_args(['--header'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300107 self.assertTrue(ns.header)
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -0800108
109 def test_randomize(self):
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300110 for opt in '-r', '--randomize':
111 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200112 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300113 self.assertTrue(ns.randomize)
114
115 def test_randseed(self):
Victor Stinner3844fe52015-09-26 10:38:01 +0200116 ns = libregrtest._parse_args(['--randseed', '12345'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300117 self.assertEqual(ns.random_seed, 12345)
118 self.assertTrue(ns.randomize)
119 self.checkError(['--randseed'], 'expected one argument')
120 self.checkError(['--randseed', 'foo'], 'invalid int value')
121
122 def test_fromfile(self):
123 for opt in '-f', '--fromfile':
124 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200125 ns = libregrtest._parse_args([opt, 'foo'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300126 self.assertEqual(ns.fromfile, 'foo')
127 self.checkError([opt], 'expected one argument')
128 self.checkError([opt, 'foo', '-s'], "don't go together")
129
130 def test_exclude(self):
131 for opt in '-x', '--exclude':
132 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200133 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300134 self.assertTrue(ns.exclude)
135
136 def test_single(self):
137 for opt in '-s', '--single':
138 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200139 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300140 self.assertTrue(ns.single)
141 self.checkError([opt, '-f', 'foo'], "don't go together")
142
143 def test_match(self):
144 for opt in '-m', '--match':
145 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200146 ns = libregrtest._parse_args([opt, 'pattern'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300147 self.assertEqual(ns.match_tests, 'pattern')
148 self.checkError([opt], 'expected one argument')
149
150 def test_failfast(self):
151 for opt in '-G', '--failfast':
152 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200153 ns = libregrtest._parse_args([opt, '-v'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300154 self.assertTrue(ns.failfast)
Victor Stinner3844fe52015-09-26 10:38:01 +0200155 ns = libregrtest._parse_args([opt, '-W'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300156 self.assertTrue(ns.failfast)
157 self.checkError([opt], '-G/--failfast needs either -v or -W')
158
159 def test_use(self):
160 for opt in '-u', '--use':
161 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200162 ns = libregrtest._parse_args([opt, 'gui,network'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300163 self.assertEqual(ns.use_resources, ['gui', 'network'])
Victor Stinner3844fe52015-09-26 10:38:01 +0200164 ns = libregrtest._parse_args([opt, 'gui,none,network'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300165 self.assertEqual(ns.use_resources, ['network'])
Victor Stinner98de5342015-09-26 09:43:45 +0200166 expected = list(libregrtest.RESOURCE_NAMES)
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300167 expected.remove('gui')
Victor Stinner3844fe52015-09-26 10:38:01 +0200168 ns = libregrtest._parse_args([opt, 'all,-gui'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300169 self.assertEqual(ns.use_resources, expected)
170 self.checkError([opt], 'expected one argument')
171 self.checkError([opt, 'foo'], 'invalid resource')
172
173 def test_memlimit(self):
174 for opt in '-M', '--memlimit':
175 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200176 ns = libregrtest._parse_args([opt, '4G'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300177 self.assertEqual(ns.memlimit, '4G')
178 self.checkError([opt], 'expected one argument')
179
180 def test_testdir(self):
Victor Stinner3844fe52015-09-26 10:38:01 +0200181 ns = libregrtest._parse_args(['--testdir', 'foo'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300182 self.assertEqual(ns.testdir, os.path.join(support.SAVEDCWD, 'foo'))
183 self.checkError(['--testdir'], 'expected one argument')
184
Benjamin Peterson95536b82013-09-29 15:18:43 -0400185 def test_runleaks(self):
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300186 for opt in '-L', '--runleaks':
187 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200188 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300189 self.assertTrue(ns.runleaks)
190
Benjamin Peterson95536b82013-09-29 15:18:43 -0400191 def test_huntrleaks(self):
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300192 for opt in '-R', '--huntrleaks':
193 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200194 ns = libregrtest._parse_args([opt, ':'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300195 self.assertEqual(ns.huntrleaks, (5, 4, 'reflog.txt'))
Victor Stinner3844fe52015-09-26 10:38:01 +0200196 ns = libregrtest._parse_args([opt, '6:'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300197 self.assertEqual(ns.huntrleaks, (6, 4, 'reflog.txt'))
Victor Stinner3844fe52015-09-26 10:38:01 +0200198 ns = libregrtest._parse_args([opt, ':3'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300199 self.assertEqual(ns.huntrleaks, (5, 3, 'reflog.txt'))
Victor Stinner3844fe52015-09-26 10:38:01 +0200200 ns = libregrtest._parse_args([opt, '6:3:leaks.log'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300201 self.assertEqual(ns.huntrleaks, (6, 3, 'leaks.log'))
202 self.checkError([opt], 'expected one argument')
203 self.checkError([opt, '6'],
204 'needs 2 or 3 colon-separated arguments')
205 self.checkError([opt, 'foo:'], 'invalid huntrleaks value')
206 self.checkError([opt, '6:foo'], 'invalid huntrleaks value')
207
208 def test_multiprocess(self):
209 for opt in '-j', '--multiprocess':
210 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200211 ns = libregrtest._parse_args([opt, '2'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300212 self.assertEqual(ns.use_mp, 2)
213 self.checkError([opt], 'expected one argument')
214 self.checkError([opt, 'foo'], 'invalid int value')
215 self.checkError([opt, '2', '-T'], "don't go together")
216 self.checkError([opt, '2', '-l'], "don't go together")
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300217
Benjamin Peterson95536b82013-09-29 15:18:43 -0400218 def test_coverage(self):
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300219 for opt in '-T', '--coverage':
220 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200221 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300222 self.assertTrue(ns.trace)
223
224 def test_coverdir(self):
225 for opt in '-D', '--coverdir':
226 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200227 ns = libregrtest._parse_args([opt, 'foo'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300228 self.assertEqual(ns.coverdir,
229 os.path.join(support.SAVEDCWD, 'foo'))
230 self.checkError([opt], 'expected one argument')
231
232 def test_nocoverdir(self):
233 for opt in '-N', '--nocoverdir':
234 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200235 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300236 self.assertIsNone(ns.coverdir)
237
238 def test_threshold(self):
239 for opt in '-t', '--threshold':
240 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200241 ns = libregrtest._parse_args([opt, '1000'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300242 self.assertEqual(ns.threshold, 1000)
243 self.checkError([opt], 'expected one argument')
244 self.checkError([opt, 'foo'], 'invalid int value')
245
246 def test_nowindows(self):
247 for opt in '-n', '--nowindows':
248 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200249 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300250 self.assertTrue(ns.nowindows)
251
252 def test_forever(self):
253 for opt in '-F', '--forever':
254 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200255 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300256 self.assertTrue(ns.forever)
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -0800257
258
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300259 def test_unrecognized_argument(self):
260 self.checkError(['--xxx'], 'usage:')
261
262 def test_long_option__partial(self):
Victor Stinner3844fe52015-09-26 10:38:01 +0200263 ns = libregrtest._parse_args(['--qui'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300264 self.assertTrue(ns.quiet)
265 self.assertEqual(ns.verbose, 0)
266
267 def test_two_options(self):
Victor Stinner3844fe52015-09-26 10:38:01 +0200268 ns = libregrtest._parse_args(['--quiet', '--exclude'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300269 self.assertTrue(ns.quiet)
270 self.assertEqual(ns.verbose, 0)
271 self.assertTrue(ns.exclude)
272
273 def test_option_with_empty_string_value(self):
Victor Stinner3844fe52015-09-26 10:38:01 +0200274 ns = libregrtest._parse_args(['--start', ''])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300275 self.assertEqual(ns.start, '')
276
277 def test_arg(self):
Victor Stinner3844fe52015-09-26 10:38:01 +0200278 ns = libregrtest._parse_args(['foo'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300279 self.assertEqual(ns.args, ['foo'])
280
281 def test_option_and_arg(self):
Victor Stinner3844fe52015-09-26 10:38:01 +0200282 ns = libregrtest._parse_args(['--quiet', 'foo'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300283 self.assertTrue(ns.quiet)
284 self.assertEqual(ns.verbose, 0)
285 self.assertEqual(ns.args, ['foo'])
286
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -0800287
Victor Stinner6b415102015-09-28 23:16:17 +0200288class BaseTestCase(unittest.TestCase):
289 TEST_UNIQUE_ID = 1
290 TESTNAME_PREFIX = 'test_regrtest_'
291 TESTNAME_REGEX = r'test_[a-z0-9_]+'
292
293 def setUp(self):
294 self.testdir = os.path.join(ROOT_DIR, 'Lib', 'test')
295
296 # When test_regrtest is interrupted by CTRL+c, it can leave
297 # temporary test files
298 remove = [entry.path
299 for entry in os.scandir(self.testdir)
300 if (entry.name.startswith(self.TESTNAME_PREFIX)
301 and entry.name.endswith(".py"))]
302 for path in remove:
303 print("WARNING: test_regrtest: remove %s" % path)
304 support.unlink(path)
305
306 def create_test(self, name=None, code=''):
307 if not name:
308 name = 'noop%s' % BaseTestCase.TEST_UNIQUE_ID
309 BaseTestCase.TEST_UNIQUE_ID += 1
310
311 # test_regrtest cannot be run twice in parallel because
312 # of setUp() and create_test()
313 name = self.TESTNAME_PREFIX + "%s_%s" % (os.getpid(), name)
314 path = os.path.join(self.testdir, name + '.py')
315
316 self.addCleanup(support.unlink, path)
317 # Use 'x' mode to ensure that we do not override existing tests
318 with open(path, 'x', encoding='utf-8') as fp:
319 fp.write(code)
320 return name
321
322 def regex_search(self, regex, output):
323 match = re.search(regex, output, re.MULTILINE)
324 if not match:
325 self.fail("%r not found in %r" % (regex, output))
326 return match
327
328 def check_line(self, output, regex):
329 regex = re.compile(r'^' + regex, re.MULTILINE)
330 self.assertRegex(output, regex)
331
332 def parse_executed_tests(self, output):
Victor Stinner9a142142015-09-30 13:51:17 +0200333 regex = r'^\[ *[0-9]+(?:/ *[0-9]+)?\] (%s)$' % self.TESTNAME_REGEX
334 parser = re.finditer(regex, output, re.MULTILINE)
335 return list(match.group(1) for match in parser)
Victor Stinner6b415102015-09-28 23:16:17 +0200336
Victor Stinner9a142142015-09-30 13:51:17 +0200337 def check_executed_tests(self, output, tests, skipped=(), failed=(),
338 randomize=False):
Victor Stinner6b415102015-09-28 23:16:17 +0200339 if isinstance(tests, str):
340 tests = [tests]
Victor Stinner9a142142015-09-30 13:51:17 +0200341 if isinstance(skipped, str):
342 skipped = [skipped]
343 if isinstance(failed, str):
344 failed = [failed]
Victor Stinner6b415102015-09-28 23:16:17 +0200345 ntest = len(tests)
Victor Stinner9a142142015-09-30 13:51:17 +0200346 nskipped = len(skipped)
347 nfailed = len(failed)
Victor Stinner6b415102015-09-28 23:16:17 +0200348
Victor Stinner9a142142015-09-30 13:51:17 +0200349 executed = self.parse_executed_tests(output)
350 if randomize:
351 self.assertEqual(set(executed), set(tests), output)
Victor Stinner6b415102015-09-28 23:16:17 +0200352 else:
Victor Stinner9a142142015-09-30 13:51:17 +0200353 self.assertEqual(executed, tests, output)
354
355 def plural(count):
356 return 's' if count != 1 else ''
357
358 def list_regex(line_format, tests):
359 count = len(tests)
360 names = ' '.join(sorted(tests))
361 regex = line_format % (count, plural(count))
362 regex = r'%s:\n %s$' % (regex, names)
363 return regex
364
365 if skipped:
366 regex = list_regex('%s test%s skipped', skipped)
367 self.check_line(output, regex)
368
369 if failed:
370 regex = list_regex('%s test%s failed', failed)
371 self.check_line(output, regex)
372
373 good = ntest - nskipped - nfailed
374 if good:
375 regex = r'%s test%s OK\.$' % (good, plural(good))
376 if not skipped and not failed and good > 1:
377 regex = 'All %s' % regex
378 self.check_line(output, regex)
Victor Stinner6b415102015-09-28 23:16:17 +0200379
380 def parse_random_seed(self, output):
381 match = self.regex_search(r'Using random seed ([0-9]+)', output)
382 randseed = int(match.group(1))
383 self.assertTrue(0 <= randseed <= 10000000, randseed)
384 return randseed
385
Victor Stinner076fc872015-10-03 00:20:56 +0200386 def run_command(self, args, input=None, exitcode=0, **kw):
Victor Stinner42771342015-09-29 14:17:09 +0200387 if not input:
388 input = ''
Victor Stinner076fc872015-10-03 00:20:56 +0200389 if 'stderr' not in kw:
390 kw['stderr'] = subprocess.PIPE
Victor Stinner9a142142015-09-30 13:51:17 +0200391 proc = subprocess.run(args,
392 universal_newlines=True,
393 input=input,
394 stdout=subprocess.PIPE,
Victor Stinner076fc872015-10-03 00:20:56 +0200395 **kw)
Victor Stinner9a142142015-09-30 13:51:17 +0200396 if proc.returncode != exitcode:
Victor Stinner076fc872015-10-03 00:20:56 +0200397 msg = ("Command %s failed with exit code %s\n"
398 "\n"
399 "stdout:\n"
400 "---\n"
401 "%s\n"
402 "---\n"
403 % (str(args), proc.returncode, proc.stdout))
404 if proc.stderr:
405 msg += ("\n"
406 "stderr:\n"
407 "---\n"
408 "%s"
409 "---\n"
410 % proc.stderr)
411 self.fail(msg)
Victor Stinner9a142142015-09-30 13:51:17 +0200412 return proc
Victor Stinner42771342015-09-29 14:17:09 +0200413
414
415 def run_python(self, args, **kw):
416 args = [sys.executable, '-X', 'faulthandler', '-I', *args]
417 proc = self.run_command(args, **kw)
418 return proc.stdout
419
Victor Stinner6b415102015-09-28 23:16:17 +0200420
421class ProgramsTestCase(BaseTestCase):
422 """
423 Test various ways to run the Python test suite. Use options close
424 to options used on the buildbot.
425 """
426
427 NTEST = 4
428
429 def setUp(self):
430 super().setUp()
431
432 # Create NTEST tests doing nothing
433 self.tests = [self.create_test() for index in range(self.NTEST)]
434
435 self.python_args = ['-Wd', '-E', '-bb']
436 self.regrtest_args = ['-uall', '-rwW', '--timeout', '3600', '-j4']
437 if sys.platform == 'win32':
438 self.regrtest_args.append('-n')
439
440 def check_output(self, output):
441 self.parse_random_seed(output)
Victor Stinner9a142142015-09-30 13:51:17 +0200442 self.check_executed_tests(output, self.tests, randomize=True)
Victor Stinner6b415102015-09-28 23:16:17 +0200443
444 def run_tests(self, args):
Victor Stinner9a142142015-09-30 13:51:17 +0200445 output = self.run_python(args)
446 self.check_output(output)
Victor Stinner6b415102015-09-28 23:16:17 +0200447
448 def test_script_regrtest(self):
449 # Lib/test/regrtest.py
450 script = os.path.join(ROOT_DIR, 'Lib', 'test', 'regrtest.py')
451
452 args = [*self.python_args, script, *self.regrtest_args, *self.tests]
453 self.run_tests(args)
454
455 def test_module_test(self):
456 # -m test
457 args = [*self.python_args, '-m', 'test',
458 *self.regrtest_args, *self.tests]
459 self.run_tests(args)
460
461 def test_module_regrtest(self):
462 # -m test.regrtest
463 args = [*self.python_args, '-m', 'test.regrtest',
464 *self.regrtest_args, *self.tests]
465 self.run_tests(args)
466
467 def test_module_autotest(self):
468 # -m test.autotest
469 args = [*self.python_args, '-m', 'test.autotest',
470 *self.regrtest_args, *self.tests]
471 self.run_tests(args)
472
473 def test_module_from_test_autotest(self):
474 # from test import autotest
475 code = 'from test import autotest'
476 args = [*self.python_args, '-c', code,
477 *self.regrtest_args, *self.tests]
478 self.run_tests(args)
479
480 def test_script_autotest(self):
481 # Lib/test/autotest.py
482 script = os.path.join(ROOT_DIR, 'Lib', 'test', 'autotest.py')
483 args = [*self.python_args, script, *self.regrtest_args, *self.tests]
484 self.run_tests(args)
485
486 def test_tools_script_run_tests(self):
487 # Tools/scripts/run_tests.py
488 script = os.path.join(ROOT_DIR, 'Tools', 'scripts', 'run_tests.py')
489 self.run_tests([script, *self.tests])
490
Victor Stinnere143c1f2015-09-29 01:02:37 +0200491 def run_batch(self, *args):
Victor Stinner42771342015-09-29 14:17:09 +0200492 proc = self.run_command(args)
Victor Stinner6b415102015-09-28 23:16:17 +0200493 self.check_output(proc.stdout)
494
495 @unittest.skipUnless(sys.platform == 'win32', 'Windows only')
496 def test_tools_buildbot_test(self):
497 # Tools\buildbot\test.bat
498 script = os.path.join(ROOT_DIR, 'Tools', 'buildbot', 'test.bat')
Victor Stinnere143c1f2015-09-29 01:02:37 +0200499 test_args = []
500 if platform.architecture()[0] == '64bit':
501 test_args.append('-x64') # 64-bit build
502 if not Py_DEBUG:
503 test_args.append('+d') # Release build, use python.exe
504 self.run_batch(script, *test_args, *self.tests)
Victor Stinner6b415102015-09-28 23:16:17 +0200505
506 @unittest.skipUnless(sys.platform == 'win32', 'Windows only')
507 def test_pcbuild_rt(self):
508 # PCbuild\rt.bat
509 script = os.path.join(ROOT_DIR, r'PCbuild\rt.bat')
Victor Stinnere143c1f2015-09-29 01:02:37 +0200510 rt_args = ["-q"] # Quick, don't run tests twice
511 if platform.architecture()[0] == '64bit':
512 rt_args.append('-x64') # 64-bit build
513 if Py_DEBUG:
514 rt_args.append('-d') # Debug build, use python_d.exe
515 self.run_batch(script, *rt_args, *self.regrtest_args, *self.tests)
Victor Stinner6b415102015-09-28 23:16:17 +0200516
517
518class ArgsTestCase(BaseTestCase):
519 """
520 Test arguments of the Python test suite.
521 """
522
Victor Stinner9a142142015-09-30 13:51:17 +0200523 def run_tests(self, *args, **kw):
524 return self.run_python(['-m', 'test', *args], **kw)
525
526 def test_failing_test(self):
527 # test a failing test
528 code = textwrap.dedent("""
529 import unittest
530
531 class FailingTest(unittest.TestCase):
532 def test_failing(self):
533 self.fail("bug")
534 """)
535 test_ok = self.create_test()
536 test_failing = self.create_test(code=code)
537 tests = [test_ok, test_failing]
538
539 output = self.run_tests(*tests, exitcode=1)
540 self.check_executed_tests(output, tests, failed=test_failing)
Victor Stinner6b415102015-09-28 23:16:17 +0200541
542 def test_resources(self):
543 # test -u command line option
544 tests = {}
545 for resource in ('audio', 'network'):
546 code = 'from test import support\nsupport.requires(%r)' % resource
547 tests[resource] = self.create_test(resource, code)
548 test_names = sorted(tests.values())
549
550 # -u all: 2 resources enabled
551 output = self.run_tests('-u', 'all', *test_names)
552 self.check_executed_tests(output, test_names)
553
554 # -u audio: 1 resource enabled
555 output = self.run_tests('-uaudio', *test_names)
556 self.check_executed_tests(output, test_names,
557 skipped=tests['network'])
558
559 # no option: 0 resources enabled
560 output = self.run_tests(*test_names)
561 self.check_executed_tests(output, test_names,
562 skipped=test_names)
563
564 def test_random(self):
565 # test -r and --randseed command line option
566 code = textwrap.dedent("""
567 import random
568 print("TESTRANDOM: %s" % random.randint(1, 1000))
569 """)
570 test = self.create_test('random', code)
571
572 # first run to get the output with the random seed
573 output = self.run_tests('-r', test)
574 randseed = self.parse_random_seed(output)
575 match = self.regex_search(r'TESTRANDOM: ([0-9]+)', output)
576 test_random = int(match.group(1))
577
578 # try to reproduce with the random seed
579 output = self.run_tests('-r', '--randseed=%s' % randseed, test)
580 randseed2 = self.parse_random_seed(output)
581 self.assertEqual(randseed2, randseed)
582
583 match = self.regex_search(r'TESTRANDOM: ([0-9]+)', output)
584 test_random2 = int(match.group(1))
585 self.assertEqual(test_random2, test_random)
586
587 def test_fromfile(self):
588 # test --fromfile
589 tests = [self.create_test() for index in range(5)]
590
591 # Write the list of files using a format similar to regrtest output:
592 # [1/2] test_1
593 # [2/2] test_2
594 filename = support.TESTFN
595 self.addCleanup(support.unlink, filename)
596 with open(filename, "w") as fp:
597 for index, name in enumerate(tests, 1):
598 print("[%s/%s] %s" % (index, len(tests), name), file=fp)
599
600 output = self.run_tests('--fromfile', filename)
601 self.check_executed_tests(output, tests)
602
603 def test_slow(self):
604 # test --slow
605 tests = [self.create_test() for index in range(3)]
606 output = self.run_tests("--slow", *tests)
607 self.check_executed_tests(output, tests)
608 regex = ('10 slowest tests:\n'
609 '(?:%s: [0-9]+\.[0-9]+s\n){%s}'
610 % (self.TESTNAME_REGEX, len(tests)))
611 self.check_line(output, regex)
612
Victor Stinner6b415102015-09-28 23:16:17 +0200613 def test_coverage(self):
614 # test --coverage
615 test = self.create_test()
616 output = self.run_tests("--coverage", test)
Victor Stinner9a142142015-09-30 13:51:17 +0200617 self.check_executed_tests(output, [test])
Victor Stinner6b415102015-09-28 23:16:17 +0200618 regex = ('lines +cov% +module +\(path\)\n'
619 '(?: *[0-9]+ *[0-9]{1,2}% *[^ ]+ +\([^)]+\)+)+')
620 self.check_line(output, regex)
621
Victor Stinner42771342015-09-29 14:17:09 +0200622 def test_wait(self):
623 # test --wait
624 test = self.create_test()
625 output = self.run_tests("--wait", test, input='key')
626 self.check_line(output, 'Press any key to continue')
627
Victor Stinner9a142142015-09-30 13:51:17 +0200628 def test_forever(self):
629 # test --forever
630 code = textwrap.dedent("""
631 import unittest
632
633 class ForeverTester(unittest.TestCase):
634 RUN = 1
635
636 def test_run(self):
637 ForeverTester.RUN += 1
638 if ForeverTester.RUN > 3:
639 self.fail("fail at the 3rd runs")
640 """)
641 test = self.create_test(code=code)
642 output = self.run_tests('--forever', test, exitcode=1)
643 self.check_executed_tests(output, [test]*3, failed=test)
644
Victor Stinner46d75352015-10-03 02:21:35 +0200645 @unittest.skipUnless(Py_DEBUG, 'need a debug build')
Victor Stinner076fc872015-10-03 00:20:56 +0200646 def test_huntrleaks_fd_leak(self):
647 # test --huntrleaks for file descriptor leak
648 code = textwrap.dedent("""
649 import os
650 import unittest
651
Victor Stinner20d21182015-10-03 21:40:21 +0200652 # Issue #25306: Disable popups and logs to stderr on assertion
653 # failures in MSCRT
654 try:
655 import msvcrt
656 msvcrt.CrtSetReportMode
657 except (ImportError, AttributeError):
658 # no Windows, o release build
659 pass
660 else:
661 for m in [msvcrt.CRT_WARN, msvcrt.CRT_ERROR, msvcrt.CRT_ASSERT]:
662 msvcrt.CrtSetReportMode(m, 0)
663
Victor Stinner076fc872015-10-03 00:20:56 +0200664 class FDLeakTest(unittest.TestCase):
665 def test_leak(self):
666 fd = os.open(__file__, os.O_RDONLY)
667 # bug: never cloes the file descriptor
668 """)
669 test = self.create_test(code=code)
670
671 filename = 'reflog.txt'
672 self.addCleanup(support.unlink, filename)
673 output = self.run_tests('--huntrleaks', '3:3:', test,
674 exitcode=1,
675 stderr=subprocess.STDOUT)
676 self.check_executed_tests(output, [test], failed=test)
677
678 line = 'beginning 6 repetitions\n123456\n......\n'
679 self.check_line(output, re.escape(line))
680
681 line2 = '%s leaked [1, 1, 1] file descriptors, sum=3\n' % test
682 self.check_line(output, re.escape(line2))
683
684 with open(filename) as fp:
685 reflog = fp.read()
686 self.assertEqual(reflog, line2)
687
Victor Stinner5f9d3ac2015-10-03 00:21:12 +0200688 def test_list_tests(self):
689 # test --list-tests
690 tests = [self.create_test() for i in range(5)]
691 output = self.run_tests('--list-tests', *tests)
692 self.assertEqual(output.rstrip().splitlines(),
693 tests)
694
Victor Stinner6b415102015-09-28 23:16:17 +0200695
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -0800696if __name__ == '__main__':
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300697 unittest.main()