@@ -111,9 +111,6 @@ def __init__(self, ns: Namespace):
111
111
# used by --slow
112
112
self .test_times = []
113
113
114
- # used by --coverage, trace.Trace instance
115
- self .tracer = None
116
-
117
114
# used to display the progress bar "[ 3/100]"
118
115
self .start_time = time .perf_counter ()
119
116
self .test_count_text = ''
@@ -443,28 +440,18 @@ def display_result(self):
443
440
print (count (len (self .run_no_tests ), "test" ), "run no tests:" )
444
441
printlist (self .run_no_tests )
445
442
446
- def run_test (self , test_index , test_name , previous_test , save_modules ):
447
- text = test_name
448
- if previous_test :
449
- text = '%s -- %s' % (text , previous_test )
450
- self .display_progress (test_index , text )
451
-
452
- if self .tracer :
443
+ def run_test (self , test_name : str , runtests : RunTests , tracer ):
444
+ if tracer is not None :
453
445
# If we're tracing code coverage, then we don't exit with status
454
446
# if on a false return value from main.
455
- cmd = ('result = runtest(self.ns, test_name); '
456
- 'self.accumulate_result(result)' )
447
+ cmd = ('result = runtest(self.ns, test_name)' )
457
448
ns = dict (locals ())
458
- self . tracer .runctx (cmd , globals = globals (), locals = ns )
449
+ tracer .runctx (cmd , globals = globals (), locals = ns )
459
450
result = ns ['result' ]
460
451
else :
461
452
result = runtest (self .ns , test_name )
462
- self .accumulate_result (result )
463
453
464
- # Unload the newly imported modules (best effort finalization)
465
- for module in sys .modules .keys ():
466
- if module not in save_modules and module .startswith ("test." ):
467
- support .unload (module )
454
+ self .accumulate_result (result )
468
455
469
456
return result
470
457
@@ -477,7 +464,9 @@ def run_tests_sequentially(self, runtests):
477
464
478
465
if coverage :
479
466
import trace
480
- self .tracer = trace .Trace (trace = False , count = True )
467
+ tracer = trace .Trace (trace = False , count = True )
468
+ else :
469
+ tracer = None
481
470
482
471
save_modules = sys .modules .keys ()
483
472
@@ -491,8 +480,17 @@ def run_tests_sequentially(self, runtests):
491
480
for test_index , test_name in enumerate (tests_iter , 1 ):
492
481
start_time = time .perf_counter ()
493
482
494
- result = self .run_test (test_index , test_name ,
495
- previous_test , save_modules )
483
+ text = test_name
484
+ if previous_test :
485
+ text = '%s -- %s' % (text , previous_test )
486
+ self .display_progress (test_index , text )
487
+
488
+ result = self .run_test (test_name , runtests , tracer )
489
+
490
+ # Unload the newly imported modules (best effort finalization)
491
+ for module in sys .modules .keys ():
492
+ if module not in save_modules and module .startswith ("test." ):
493
+ support .unload (module )
496
494
497
495
if result .must_stop (fail_fast , fail_env_changed ):
498
496
break
@@ -508,6 +506,8 @@ def run_tests_sequentially(self, runtests):
508
506
if previous_test :
509
507
print (previous_test )
510
508
509
+ return tracer
510
+
511
511
def display_header (self ):
512
512
# Print basic platform information
513
513
print ("==" , platform .python_implementation (), * sys .version .split ())
@@ -604,45 +604,28 @@ def set_tests(self, runtests: RunTests):
604
604
self .test_count_text = '/{}' .format (len (self .tests ))
605
605
self .test_count_width = len (self .test_count_text ) - 1
606
606
607
- def run_tests (self ):
608
- # For a partial run, we do not need to clutter the output.
609
- if (self .want_header
610
- or not (self .ns .pgo or self .ns .quiet or self .ns .single
611
- or self .tests or self .ns .args )):
612
- self .display_header ()
613
-
614
- if self .ns .huntrleaks :
615
- warmup , repetitions , _ = self .ns .huntrleaks
616
- if warmup < 3 :
617
- msg = ("WARNING: Running tests with --huntrleaks/-R and less than "
618
- "3 warmup repetitions can give false positives!" )
619
- print (msg , file = sys .stdout , flush = True )
620
-
621
- if self .randomize :
622
- print ("Using random seed" , self .random_seed )
623
-
624
- tests = self .selected
625
- runtests = RunTests (tuple (tests ), forever = self .forever )
607
+ def run_tests (self , runtests : RunTests ):
626
608
self .first_runtests = runtests
627
609
self .set_tests (runtests )
628
610
if self .ns .use_mp :
629
611
self ._run_tests_mp (runtests )
612
+ tracer = None
630
613
else :
631
- self .run_tests_sequentially (runtests )
632
- return runtests
614
+ tracer = self .run_tests_sequentially (runtests )
615
+ return tracer
633
616
634
- def finalize (self ):
617
+ def finalize_tests (self , tracer ):
635
618
if self .next_single_filename :
636
619
if self .next_single_test :
637
620
with open (self .next_single_filename , 'w' ) as fp :
638
621
fp .write (self .next_single_test + '\n ' )
639
622
else :
640
623
os .unlink (self .next_single_filename )
641
624
642
- if self . tracer :
643
- r = self . tracer .results ()
644
- r .write_results (show_missing = True , summary = True ,
645
- coverdir = self .ns .coverdir )
625
+ if tracer is not None :
626
+ results = tracer .results ()
627
+ results .write_results (show_missing = True , summary = True ,
628
+ coverdir = self .ns .coverdir )
646
629
647
630
if self .ns .runleaks :
648
631
os .system ("leaks %d" % os .getpid ())
@@ -858,15 +841,32 @@ def get_exitcode(self):
858
841
return exitcode
859
842
860
843
def action_run_tests (self ):
861
- runtests = self .run_tests ()
844
+ if self .ns .huntrleaks :
845
+ warmup , repetitions , _ = self .ns .huntrleaks
846
+ if warmup < 3 :
847
+ msg = ("WARNING: Running tests with --huntrleaks/-R and less than "
848
+ "3 warmup repetitions can give false positives!" )
849
+ print (msg , file = sys .stdout , flush = True )
850
+
851
+ # For a partial run, we do not need to clutter the output.
852
+ if (self .want_header
853
+ or not (self .ns .pgo or self .ns .quiet or self .ns .single
854
+ or self .tests or self .ns .args )):
855
+ self .display_header ()
856
+
857
+ if self .randomize :
858
+ print ("Using random seed" , self .random_seed )
859
+
860
+ runtests = RunTests (tuple (self .selected ), forever = self .forever )
861
+ tracer = self .run_tests (runtests )
862
862
self .display_result ()
863
863
864
864
need_rerun = self .need_rerun
865
865
if self .ns .rerun and need_rerun :
866
866
self .rerun_failed_tests (need_rerun , runtests )
867
867
868
868
self .display_summary ()
869
- self .finalize ( )
869
+ self .finalize_tests ( tracer )
870
870
871
871
def _main (self ):
872
872
if self .is_worker ():
0 commit comments