Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 1 | """ |
| 2 | Tests of regrtest.py. |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 3 | |
| 4 | Note: test_regrtest cannot be run twice in parallel. |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 5 | """ |
| 6 | |
| 7 | import argparse |
Charles-François Natali | 833bf1f | 2013-09-08 12:27:33 +0200 | [diff] [blame] | 8 | import faulthandler |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 9 | import getopt |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 10 | import os.path |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 11 | import platform |
| 12 | import re |
| 13 | import subprocess |
| 14 | import sys |
| 15 | import textwrap |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 16 | import unittest |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 17 | from test import libregrtest |
| 18 | from test import support |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 19 | |
| 20 | |
| 21 | Py_DEBUG = hasattr(sys, 'getobjects') |
| 22 | ROOT_DIR = os.path.join(os.path.dirname(__file__), '..', '..') |
| 23 | ROOT_DIR = os.path.abspath(os.path.normpath(ROOT_DIR)) |
| 24 | |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 25 | |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 26 | class ParseArgsTestCase(unittest.TestCase): |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 27 | """ |
| 28 | Test regrtest's argument parsing, function _parse_args(). |
| 29 | """ |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 30 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 31 | def checkError(self, args, msg): |
| 32 | with support.captured_stderr() as err, self.assertRaises(SystemExit): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 33 | libregrtest._parse_args(args) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 34 | self.assertIn(msg, err.getvalue()) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 35 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 36 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 41 | libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 42 | self.assertIn('Run Python regression tests.', out.getvalue()) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 43 | |
Charles-François Natali | 833bf1f | 2013-09-08 12:27:33 +0200 | [diff] [blame] | 44 | @unittest.skipUnless(hasattr(faulthandler, 'dump_traceback_later'), |
| 45 | "faulthandler.dump_traceback_later() required") |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 46 | def test_timeout(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 47 | ns = libregrtest._parse_args(['--timeout', '4.2']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 48 | self.assertEqual(ns.timeout, 4.2) |
| 49 | self.checkError(['--timeout'], 'expected one argument') |
| 50 | self.checkError(['--timeout', 'foo'], 'invalid float value') |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 51 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 52 | def test_wait(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 53 | ns = libregrtest._parse_args(['--wait']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 54 | self.assertTrue(ns.wait) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 55 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 56 | def test_slaveargs(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 57 | ns = libregrtest._parse_args(['--slaveargs', '[[], {}]']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 58 | self.assertEqual(ns.slaveargs, '[[], {}]') |
| 59 | self.checkError(['--slaveargs'], 'expected one argument') |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 60 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 61 | def test_start(self): |
| 62 | for opt in '-S', '--start': |
| 63 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 64 | ns = libregrtest._parse_args([opt, 'foo']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 65 | self.assertEqual(ns.start, 'foo') |
| 66 | self.checkError([opt], 'expected one argument') |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 67 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 68 | def test_verbose(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 69 | ns = libregrtest._parse_args(['-v']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 70 | self.assertEqual(ns.verbose, 1) |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 71 | ns = libregrtest._parse_args(['-vvv']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 72 | self.assertEqual(ns.verbose, 3) |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 73 | ns = libregrtest._parse_args(['--verbose']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 74 | self.assertEqual(ns.verbose, 1) |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 75 | ns = libregrtest._parse_args(['--verbose'] * 3) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 76 | self.assertEqual(ns.verbose, 3) |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 77 | ns = libregrtest._parse_args([]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 78 | self.assertEqual(ns.verbose, 0) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 79 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 80 | def test_verbose2(self): |
| 81 | for opt in '-w', '--verbose2': |
| 82 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 83 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 84 | self.assertTrue(ns.verbose2) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 85 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 86 | def test_verbose3(self): |
| 87 | for opt in '-W', '--verbose3': |
| 88 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 89 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 90 | self.assertTrue(ns.verbose3) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 91 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 92 | def test_quiet(self): |
| 93 | for opt in '-q', '--quiet': |
| 94 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 95 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 96 | self.assertTrue(ns.quiet) |
| 97 | self.assertEqual(ns.verbose, 0) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 98 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 99 | def test_slow(self): |
| 100 | for opt in '-o', '--slow': |
| 101 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 102 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 103 | self.assertTrue(ns.print_slow) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 104 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 105 | def test_header(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 106 | ns = libregrtest._parse_args(['--header']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 107 | self.assertTrue(ns.header) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 108 | |
| 109 | def test_randomize(self): |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 110 | for opt in '-r', '--randomize': |
| 111 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 112 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 113 | self.assertTrue(ns.randomize) |
| 114 | |
| 115 | def test_randseed(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 116 | ns = libregrtest._parse_args(['--randseed', '12345']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 117 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 125 | ns = libregrtest._parse_args([opt, 'foo']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 126 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 133 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 134 | self.assertTrue(ns.exclude) |
| 135 | |
| 136 | def test_single(self): |
| 137 | for opt in '-s', '--single': |
| 138 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 139 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 140 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 146 | ns = libregrtest._parse_args([opt, 'pattern']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 147 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 153 | ns = libregrtest._parse_args([opt, '-v']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 154 | self.assertTrue(ns.failfast) |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 155 | ns = libregrtest._parse_args([opt, '-W']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 156 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 162 | ns = libregrtest._parse_args([opt, 'gui,network']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 163 | self.assertEqual(ns.use_resources, ['gui', 'network']) |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 164 | ns = libregrtest._parse_args([opt, 'gui,none,network']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 165 | self.assertEqual(ns.use_resources, ['network']) |
Victor Stinner | 98de534 | 2015-09-26 09:43:45 +0200 | [diff] [blame] | 166 | expected = list(libregrtest.RESOURCE_NAMES) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 167 | expected.remove('gui') |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 168 | ns = libregrtest._parse_args([opt, 'all,-gui']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 169 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 176 | ns = libregrtest._parse_args([opt, '4G']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 177 | self.assertEqual(ns.memlimit, '4G') |
| 178 | self.checkError([opt], 'expected one argument') |
| 179 | |
| 180 | def test_testdir(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 181 | ns = libregrtest._parse_args(['--testdir', 'foo']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 182 | self.assertEqual(ns.testdir, os.path.join(support.SAVEDCWD, 'foo')) |
| 183 | self.checkError(['--testdir'], 'expected one argument') |
| 184 | |
Benjamin Peterson | 95536b8 | 2013-09-29 15:18:43 -0400 | [diff] [blame] | 185 | def test_runleaks(self): |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 186 | for opt in '-L', '--runleaks': |
| 187 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 188 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 189 | self.assertTrue(ns.runleaks) |
| 190 | |
Benjamin Peterson | 95536b8 | 2013-09-29 15:18:43 -0400 | [diff] [blame] | 191 | def test_huntrleaks(self): |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 192 | for opt in '-R', '--huntrleaks': |
| 193 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 194 | ns = libregrtest._parse_args([opt, ':']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 195 | self.assertEqual(ns.huntrleaks, (5, 4, 'reflog.txt')) |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 196 | ns = libregrtest._parse_args([opt, '6:']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 197 | self.assertEqual(ns.huntrleaks, (6, 4, 'reflog.txt')) |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 198 | ns = libregrtest._parse_args([opt, ':3']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 199 | self.assertEqual(ns.huntrleaks, (5, 3, 'reflog.txt')) |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 200 | ns = libregrtest._parse_args([opt, '6:3:leaks.log']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 201 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 211 | ns = libregrtest._parse_args([opt, '2']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 212 | 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 Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 217 | |
Benjamin Peterson | 95536b8 | 2013-09-29 15:18:43 -0400 | [diff] [blame] | 218 | def test_coverage(self): |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 219 | for opt in '-T', '--coverage': |
| 220 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 221 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 222 | self.assertTrue(ns.trace) |
| 223 | |
| 224 | def test_coverdir(self): |
| 225 | for opt in '-D', '--coverdir': |
| 226 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 227 | ns = libregrtest._parse_args([opt, 'foo']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 228 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 235 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 236 | self.assertIsNone(ns.coverdir) |
| 237 | |
| 238 | def test_threshold(self): |
| 239 | for opt in '-t', '--threshold': |
| 240 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 241 | ns = libregrtest._parse_args([opt, '1000']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 242 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 249 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 250 | self.assertTrue(ns.nowindows) |
| 251 | |
| 252 | def test_forever(self): |
| 253 | for opt in '-F', '--forever': |
| 254 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 255 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 256 | self.assertTrue(ns.forever) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 257 | |
| 258 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 259 | def test_unrecognized_argument(self): |
| 260 | self.checkError(['--xxx'], 'usage:') |
| 261 | |
| 262 | def test_long_option__partial(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 263 | ns = libregrtest._parse_args(['--qui']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 264 | self.assertTrue(ns.quiet) |
| 265 | self.assertEqual(ns.verbose, 0) |
| 266 | |
| 267 | def test_two_options(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 268 | ns = libregrtest._parse_args(['--quiet', '--exclude']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 269 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 274 | ns = libregrtest._parse_args(['--start', '']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 275 | self.assertEqual(ns.start, '') |
| 276 | |
| 277 | def test_arg(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 278 | ns = libregrtest._parse_args(['foo']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 279 | self.assertEqual(ns.args, ['foo']) |
| 280 | |
| 281 | def test_option_and_arg(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 282 | ns = libregrtest._parse_args(['--quiet', 'foo']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 283 | self.assertTrue(ns.quiet) |
| 284 | self.assertEqual(ns.verbose, 0) |
| 285 | self.assertEqual(ns.args, ['foo']) |
| 286 | |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 287 | |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 288 | class 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 Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 333 | 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 Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 336 | |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 337 | def check_executed_tests(self, output, tests, skipped=(), failed=(), |
| 338 | randomize=False): |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 339 | if isinstance(tests, str): |
| 340 | tests = [tests] |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 341 | if isinstance(skipped, str): |
| 342 | skipped = [skipped] |
| 343 | if isinstance(failed, str): |
| 344 | failed = [failed] |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 345 | ntest = len(tests) |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 346 | nskipped = len(skipped) |
| 347 | nfailed = len(failed) |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 348 | |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 349 | executed = self.parse_executed_tests(output) |
| 350 | if randomize: |
| 351 | self.assertEqual(set(executed), set(tests), output) |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 352 | else: |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 353 | 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 Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 379 | |
| 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 Stinner | 076fc87 | 2015-10-03 00:20:56 +0200 | [diff] [blame] | 386 | def run_command(self, args, input=None, exitcode=0, **kw): |
Victor Stinner | 4277134 | 2015-09-29 14:17:09 +0200 | [diff] [blame] | 387 | if not input: |
| 388 | input = '' |
Victor Stinner | 076fc87 | 2015-10-03 00:20:56 +0200 | [diff] [blame] | 389 | if 'stderr' not in kw: |
| 390 | kw['stderr'] = subprocess.PIPE |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 391 | proc = subprocess.run(args, |
| 392 | universal_newlines=True, |
| 393 | input=input, |
| 394 | stdout=subprocess.PIPE, |
Victor Stinner | 076fc87 | 2015-10-03 00:20:56 +0200 | [diff] [blame] | 395 | **kw) |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 396 | if proc.returncode != exitcode: |
Victor Stinner | 076fc87 | 2015-10-03 00:20:56 +0200 | [diff] [blame] | 397 | 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 Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 412 | return proc |
Victor Stinner | 4277134 | 2015-09-29 14:17:09 +0200 | [diff] [blame] | 413 | |
| 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 Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 420 | |
| 421 | class 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 Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 442 | self.check_executed_tests(output, self.tests, randomize=True) |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 443 | |
| 444 | def run_tests(self, args): |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 445 | output = self.run_python(args) |
| 446 | self.check_output(output) |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 447 | |
| 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 Stinner | e143c1f | 2015-09-29 01:02:37 +0200 | [diff] [blame] | 491 | def run_batch(self, *args): |
Victor Stinner | 4277134 | 2015-09-29 14:17:09 +0200 | [diff] [blame] | 492 | proc = self.run_command(args) |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 493 | 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 Stinner | e143c1f | 2015-09-29 01:02:37 +0200 | [diff] [blame] | 499 | 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 Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 505 | |
| 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 Stinner | e143c1f | 2015-09-29 01:02:37 +0200 | [diff] [blame] | 510 | 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 Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 516 | |
| 517 | |
| 518 | class ArgsTestCase(BaseTestCase): |
| 519 | """ |
| 520 | Test arguments of the Python test suite. |
| 521 | """ |
| 522 | |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 523 | 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 Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 541 | |
| 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 Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 613 | def test_coverage(self): |
| 614 | # test --coverage |
| 615 | test = self.create_test() |
| 616 | output = self.run_tests("--coverage", test) |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 617 | self.check_executed_tests(output, [test]) |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 618 | regex = ('lines +cov% +module +\(path\)\n' |
| 619 | '(?: *[0-9]+ *[0-9]{1,2}% *[^ ]+ +\([^)]+\)+)+') |
| 620 | self.check_line(output, regex) |
| 621 | |
Victor Stinner | 4277134 | 2015-09-29 14:17:09 +0200 | [diff] [blame] | 622 | 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 Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 628 | 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 Stinner | 46d7535 | 2015-10-03 02:21:35 +0200 | [diff] [blame] | 645 | @unittest.skipUnless(Py_DEBUG, 'need a debug build') |
Victor Stinner | 076fc87 | 2015-10-03 00:20:56 +0200 | [diff] [blame] | 646 | 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 Stinner | 20d2118 | 2015-10-03 21:40:21 +0200 | [diff] [blame^] | 652 | # 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 Stinner | 076fc87 | 2015-10-03 00:20:56 +0200 | [diff] [blame] | 664 | 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 Stinner | 5f9d3ac | 2015-10-03 00:21:12 +0200 | [diff] [blame] | 688 | 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 Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 695 | |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 696 | if __name__ == '__main__': |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 697 | unittest.main() |