2828ROOT_DIR = os .path .abspath (os .path .normpath (ROOT_DIR ))
2929LOG_PREFIX = r'[0-9]+:[0-9]+:[0-9]+ (?:load avg: [0-9]+\.[0-9]{2} )?'
3030
31+ EXITCODE_BAD_TEST = 2
32+ EXITCODE_ENV_CHANGED = 3
33+ EXITCODE_NO_TESTS_RAN = 4
34+ EXITCODE_INTERRUPTED = 130
35+
3136TEST_INTERRUPTED = textwrap .dedent ("""
3237 from signal import SIGINT, raise_signal
3338 try:
@@ -497,7 +502,7 @@ def list_regex(line_format, tests):
497502result .append ('INTERRUPTED' )
498503if not any ((good ,result ,failed ,interrupted ,skipped ,
499504env_changed ,fail_env_changed )):
500- result .append ("NOTEST RUN " )
505+ result .append ("NOTESTS RAN " )
501506elif not result :
502507result .append ('SUCCESS' )
503508result = ', ' .join (result )
@@ -707,7 +712,7 @@ def test_failing(self):
707712test_failing = self .create_test ('failing' ,code = code )
708713tests = [test_ok ,test_failing ]
709714
710- output = self .run_tests (* tests ,exitcode = 2 )
715+ output = self .run_tests (* tests ,exitcode = EXITCODE_BAD_TEST )
711716self .check_executed_tests (output ,tests ,failed = test_failing )
712717
713718def test_resources (self ):
@@ -748,13 +753,14 @@ def test_random(self):
748753test = self .create_test ('random' ,code )
749754
750755# first run to get the output with the random seed
751- output = self .run_tests ('-r' ,test )
756+ output = self .run_tests ('-r' ,test , exitcode = EXITCODE_NO_TESTS_RAN )
752757randseed = self .parse_random_seed (output )
753758match = self .regex_search (r'TESTRANDOM: ([0-9]+)' ,output )
754759test_random = int (match .group (1 ))
755760
756761# try to reproduce with the random seed
757- output = self .run_tests ('-r' ,'--randseed=%s' % randseed ,test )
762+ output = self .run_tests ('-r' ,'--randseed=%s' % randseed ,test ,
763+ exitcode = EXITCODE_NO_TESTS_RAN )
758764randseed2 = self .parse_random_seed (output )
759765self .assertEqual (randseed2 ,randseed )
760766
@@ -813,7 +819,7 @@ def test_fromfile(self):
813819def test_interrupted (self ):
814820code = TEST_INTERRUPTED
815821test = self .create_test ('sigint' ,code = code )
816- output = self .run_tests (test ,exitcode = 130 )
822+ output = self .run_tests (test ,exitcode = EXITCODE_INTERRUPTED )
817823self .check_executed_tests (output ,test ,omitted = test ,
818824interrupted = True )
819825
@@ -838,7 +844,7 @@ def test_slowest_interrupted(self):
838844args = ("--slowest" ,"-j2" ,test )
839845else :
840846args = ("--slowest" ,test )
841- output = self .run_tests (* args ,exitcode = 130 )
847+ output = self .run_tests (* args ,exitcode = EXITCODE_INTERRUPTED )
842848self .check_executed_tests (output ,test ,
843849omitted = test ,interrupted = True )
844850
@@ -878,7 +884,7 @@ def test_run(self):
878884 builtins.__dict__['RUN'] = 1
879885 """ )
880886test = self .create_test ('forever' ,code = code )
881- output = self .run_tests ('--forever' ,test ,exitcode = 2 )
887+ output = self .run_tests ('--forever' ,test ,exitcode = EXITCODE_BAD_TEST )
882888self .check_executed_tests (output , [test ]* 3 ,failed = test )
883889
884890def check_leak (self ,code ,what ):
@@ -887,7 +893,7 @@ def check_leak(self, code, what):
887893filename = 'reflog.txt'
888894self .addCleanup (os_helper .unlink ,filename )
889895output = self .run_tests ('--huntrleaks' ,'3:3:' ,test ,
890- exitcode = 2 ,
896+ exitcode = EXITCODE_BAD_TEST ,
891897stderr = subprocess .STDOUT )
892898self .check_executed_tests (output , [test ],failed = test )
893899
@@ -969,7 +975,7 @@ def test_crashed(self):
969975crash_test = self .create_test (name = "crash" ,code = code )
970976
971977tests = [crash_test ]
972- output = self .run_tests ("-j2" ,* tests ,exitcode = 2 )
978+ output = self .run_tests ("-j2" ,* tests ,exitcode = EXITCODE_BAD_TEST )
973979self .check_executed_tests (output ,tests ,failed = crash_test ,
974980randomize = True )
975981
@@ -1069,7 +1075,8 @@ def test_env_changed(self):
10691075self .check_executed_tests (output , [testname ],env_changed = testname )
10701076
10711077# fail with --fail-env-changed
1072- output = self .run_tests ("--fail-env-changed" ,testname ,exitcode = 3 )
1078+ output = self .run_tests ("--fail-env-changed" ,testname ,
1079+ exitcode = EXITCODE_ENV_CHANGED )
10731080self .check_executed_tests (output , [testname ],env_changed = testname ,
10741081fail_env_changed = True )
10751082
@@ -1088,7 +1095,7 @@ def test_fail_always(self):
10881095 """ )
10891096testname = self .create_test (code = code )
10901097
1091- output = self .run_tests ("-w" ,testname ,exitcode = 2 )
1098+ output = self .run_tests ("-w" ,testname ,exitcode = EXITCODE_BAD_TEST )
10921099self .check_executed_tests (output , [testname ],
10931100failed = testname ,rerun = {testname :"test_fail_always" })
10941101
@@ -1123,7 +1130,8 @@ def test_bug(self):
11231130 """ )
11241131testname = self .create_test (code = code )
11251132
1126- output = self .run_tests (testname ,"-m" ,"nosuchtest" ,exitcode = 0 )
1133+ output = self .run_tests (testname ,"-m" ,"nosuchtest" ,
1134+ exitcode = EXITCODE_NO_TESTS_RAN )
11271135self .check_executed_tests (output , [testname ],no_test_ran = testname )
11281136
11291137def test_no_tests_ran_skip (self ):
@@ -1136,7 +1144,7 @@ def test_skipped(self):
11361144 """ )
11371145testname = self .create_test (code = code )
11381146
1139- output = self .run_tests (testname , exitcode = 0 )
1147+ output = self .run_tests (testname )
11401148self .check_executed_tests (output , [testname ])
11411149
11421150def test_no_tests_ran_multiple_tests_nonexistent (self ):
@@ -1150,7 +1158,8 @@ def test_bug(self):
11501158testname = self .create_test (code = code )
11511159testname2 = self .create_test (code = code )
11521160
1153- output = self .run_tests (testname ,testname2 ,"-m" ,"nosuchtest" ,exitcode = 0 )
1161+ output = self .run_tests (testname ,testname2 ,"-m" ,"nosuchtest" ,
1162+ exitcode = EXITCODE_NO_TESTS_RAN )
11541163self .check_executed_tests (output , [testname ,testname2 ],
11551164no_test_ran = [testname ,testname2 ])
11561165
@@ -1198,7 +1207,8 @@ def test_garbage(self):
11981207 """ )
11991208testname = self .create_test (code = code )
12001209
1201- output = self .run_tests ("--fail-env-changed" ,testname ,exitcode = 3 )
1210+ output = self .run_tests ("--fail-env-changed" ,testname ,
1211+ exitcode = EXITCODE_ENV_CHANGED )
12021212self .check_executed_tests (output , [testname ],
12031213env_changed = [testname ],
12041214fail_env_changed = True )
@@ -1224,7 +1234,8 @@ def test_sleep(self):
12241234 """ )
12251235testname = self .create_test (code = code )
12261236
1227- output = self .run_tests ("-j2" ,"--timeout=1.0" ,testname ,exitcode = 2 )
1237+ output = self .run_tests ("-j2" ,"--timeout=1.0" ,testname ,
1238+ exitcode = EXITCODE_BAD_TEST )
12281239self .check_executed_tests (output , [testname ],
12291240failed = testname )
12301241self .assertRegex (output ,
@@ -1256,7 +1267,8 @@ def test_unraisable_exc(self):
12561267 """ )
12571268testname = self .create_test (code = code )
12581269
1259- output = self .run_tests ("--fail-env-changed" ,"-v" ,testname ,exitcode = 3 )
1270+ output = self .run_tests ("--fail-env-changed" ,"-v" ,testname ,
1271+ exitcode = EXITCODE_ENV_CHANGED )
12601272self .check_executed_tests (output , [testname ],
12611273env_changed = [testname ],
12621274fail_env_changed = True )
@@ -1287,7 +1299,8 @@ def test_threading_excepthook(self):
12871299 """ )
12881300testname = self .create_test (code = code )
12891301
1290- output = self .run_tests ("--fail-env-changed" ,"-v" ,testname ,exitcode = 3 )
1302+ output = self .run_tests ("--fail-env-changed" ,"-v" ,testname ,
1303+ exitcode = EXITCODE_ENV_CHANGED )
12911304self .check_executed_tests (output , [testname ],
12921305env_changed = [testname ],
12931306fail_env_changed = True )
@@ -1328,7 +1341,7 @@ def test_print_warning(self):
13281341for option in ("-v" ,"-W" ):
13291342with self .subTest (option = option ):
13301343cmd = ["--fail-env-changed" ,option ,testname ]
1331- output = self .run_tests (* cmd ,exitcode = 3 )
1344+ output = self .run_tests (* cmd ,exitcode = EXITCODE_ENV_CHANGED )
13321345self .check_executed_tests (output , [testname ],
13331346env_changed = [testname ],
13341347fail_env_changed = True )
@@ -1373,7 +1386,8 @@ def test_leak_tmp_file(self):
13731386 """ )
13741387testnames = [self .create_test (code = code )for _ in range (3 )]
13751388
1376- output = self .run_tests ("--fail-env-changed" ,"-v" ,"-j2" ,* testnames ,exitcode = 3 )
1389+ output = self .run_tests ("--fail-env-changed" ,"-v" ,"-j2" ,* testnames ,
1390+ exitcode = EXITCODE_ENV_CHANGED )
13771391self .check_executed_tests (output ,testnames ,
13781392env_changed = testnames ,
13791393fail_env_changed = True ,