@@ -377,19 +377,19 @@ def parse_executed_tests(self, output):
377
377
return list (match .group (1 ) for match in parser )
378
378
379
379
def check_executed_tests (self , output , tests , skipped = (), failed = (),
380
- omitted = (), randomize = False , interrupted = False ):
380
+ env_changed = (), omitted = (),
381
+ randomize = False , interrupted = False ,
382
+ fail_env_changed = False ):
381
383
if isinstance (tests , str ):
382
384
tests = [tests ]
383
385
if isinstance (skipped , str ):
384
386
skipped = [skipped ]
385
387
if isinstance (failed , str ):
386
388
failed = [failed ]
389
+ if isinstance (env_changed , str ):
390
+ env_changed = [env_changed ]
387
391
if isinstance (omitted , str ):
388
392
omitted = [omitted ]
389
- ntest = len (tests )
390
- nskipped = len (skipped )
391
- nfailed = len (failed )
392
- nomitted = len (omitted )
393
393
394
394
executed = self .parse_executed_tests (output )
395
395
if randomize :
@@ -415,11 +415,17 @@ def list_regex(line_format, tests):
415
415
regex = list_regex ('%s test%s failed' , failed )
416
416
self .check_line (output , regex )
417
417
418
+ if env_changed :
419
+ regex = list_regex ('%s test%s altered the execution environment' ,
420
+ env_changed )
421
+ self .check_line (output , regex )
422
+
418
423
if omitted :
419
424
regex = list_regex ('%s test%s omitted' , omitted )
420
425
self .check_line (output , regex )
421
426
422
- good = ntest - nskipped - nfailed - nomitted
427
+ good = (len (tests ) - len (skipped ) - len (failed )
428
+ - len (omitted ) - len (env_changed ))
423
429
if good :
424
430
regex = r'%s test%s OK\.$' % (good , plural (good ))
425
431
if not skipped and not failed and good > 1 :
@@ -429,10 +435,12 @@ def list_regex(line_format, tests):
429
435
if interrupted :
430
436
self .check_line (output , 'Test suite interrupted by signal SIGINT.' )
431
437
432
- if nfailed :
438
+ if failed :
433
439
result = 'FAILURE'
434
440
elif interrupted :
435
441
result = 'INTERRUPTED'
442
+ elif fail_env_changed and env_changed :
443
+ result = 'ENV CHANGED'
436
444
else :
437
445
result = 'SUCCESS'
438
446
self .check_line (output , 'Tests result: %s' % result )
@@ -604,7 +612,7 @@ def test_failing(self):
604
612
test_failing = self .create_test ('failing' , code = code )
605
613
tests = [test_ok , test_failing ]
606
614
607
- output = self .run_tests (* tests , exitcode = 1 )
615
+ output = self .run_tests (* tests , exitcode = 2 )
608
616
self .check_executed_tests (output , tests , failed = test_failing )
609
617
610
618
def test_resources (self ):
@@ -703,7 +711,7 @@ def test_fromfile(self):
703
711
def test_interrupted (self ):
704
712
code = TEST_INTERRUPTED
705
713
test = self .create_test ('sigint' , code = code )
706
- output = self .run_tests (test , exitcode = 1 )
714
+ output = self .run_tests (test , exitcode = 130 )
707
715
self .check_executed_tests (output , test , omitted = test ,
708
716
interrupted = True )
709
717
@@ -732,7 +740,7 @@ def test_slow_interrupted(self):
732
740
args = ("--slowest" , "-j2" , test )
733
741
else :
734
742
args = ("--slowest" , test )
735
- output = self .run_tests (* args , exitcode = 1 )
743
+ output = self .run_tests (* args , exitcode = 130 )
736
744
self .check_executed_tests (output , test ,
737
745
omitted = test , interrupted = True )
738
746
@@ -772,9 +780,43 @@ def test_run(self):
772
780
builtins.__dict__['RUN'] = 1
773
781
""" )
774
782
test = self .create_test ('forever' , code = code )
775
- output = self .run_tests ('--forever' , test , exitcode = 1 )
783
+ output = self .run_tests ('--forever' , test , exitcode = 2 )
776
784
self .check_executed_tests (output , [test ]* 3 , failed = test )
777
785
786
+ def check_leak (self , code , what ):
787
+ test = self .create_test ('huntrleaks' , code = code )
788
+
789
+ filename = 'reflog.txt'
790
+ self .addCleanup (support .unlink , filename )
791
+ output = self .run_tests ('--huntrleaks' , '3:3:' , test ,
792
+ exitcode = 2 ,
793
+ stderr = subprocess .STDOUT )
794
+ self .check_executed_tests (output , [test ], failed = test )
795
+
796
+ line = 'beginning 6 repetitions\n 123456\n ......\n '
797
+ self .check_line (output , re .escape (line ))
798
+
799
+ line2 = '%s leaked [1, 1, 1] %s, sum=3\n ' % (test , what )
800
+ self .assertIn (line2 , output )
801
+
802
+ with open (filename ) as fp :
803
+ reflog = fp .read ()
804
+ self .assertIn (line2 , reflog )
805
+
806
+ @unittest .skipUnless (Py_DEBUG , 'need a debug build' )
807
+ def test_huntrleaks (self ):
808
+ # test --huntrleaks
809
+ code = textwrap .dedent ("""
810
+ import unittest
811
+
812
+ GLOBAL_LIST = []
813
+
814
+ class RefLeakTest(unittest.TestCase):
815
+ def test_leak(self):
816
+ GLOBAL_LIST.append(object())
817
+ """ )
818
+ self .check_leak (code , 'references' )
819
+
778
820
def test_list_tests (self ):
779
821
# test --list-tests
780
822
tests = [self .create_test () for i in range (5 )]
@@ -794,19 +836,28 @@ def test_method2(self):
794
836
pass
795
837
""" )
796
838
testname = self .create_test (code = code )
839
+
840
+ # Test --list-cases
797
841
all_methods = ['%s.Tests.test_method1' % testname ,
798
842
'%s.Tests.test_method2' % testname ]
799
843
output = self .run_tests ('--list-cases' , testname )
800
844
self .assertEqual (output .splitlines (), all_methods )
801
845
846
+ # Test --list-cases with --match
847
+ all_methods = ['%s.Tests.test_method1' % testname ]
848
+ output = self .run_tests ('--list-cases' ,
849
+ '-m' , 'test_method1' ,
850
+ testname )
851
+ self .assertEqual (output .splitlines (), all_methods )
852
+
802
853
def test_crashed (self ):
803
854
# Any code which causes a crash
804
855
code = 'import faulthandler; faulthandler._sigsegv()'
805
856
crash_test = self .create_test (name = "crash" , code = code )
806
857
ok_test = self .create_test (name = "ok" )
807
858
808
859
tests = [crash_test , ok_test ]
809
- output = self .run_tests ("-j2" , * tests , exitcode = 1 )
860
+ output = self .run_tests ("-j2" , * tests , exitcode = 2 )
810
861
self .check_executed_tests (output , tests , failed = crash_test ,
811
862
randomize = True )
812
863
@@ -855,6 +906,25 @@ def test_method4(self):
855
906
subset = ['test_method1' , 'test_method3' ]
856
907
self .assertEqual (methods , subset )
857
908
909
+ def test_env_changed (self ):
910
+ code = textwrap .dedent ("""
911
+ import unittest
912
+
913
+ class Tests(unittest.TestCase):
914
+ def test_env_changed(self):
915
+ open("env_changed", "w").close()
916
+ """ )
917
+ testname = self .create_test (code = code )
918
+
919
+ # don't fail by default
920
+ output = self .run_tests (testname )
921
+ self .check_executed_tests (output , [testname ], env_changed = testname )
922
+
923
+ # fail with --fail-env-changed
924
+ output = self .run_tests ("--fail-env-changed" , testname , exitcode = 3 )
925
+ self .check_executed_tests (output , [testname ], env_changed = testname ,
926
+ fail_env_changed = True )
927
+
858
928
859
929
if __name__ == '__main__' :
860
930
unittest .main ()
0 commit comments