@@ -282,9 +282,8 @@ def f1() -> None: # pragma: nested
282
282
283
283
def run_one_function (f : Callable [[], None ]) -> None :
284
284
cov .erase ()
285
- cov .start ()
286
- f () # pragma: nested
287
- cov .stop ()
285
+ with cov .collect ():
286
+ f ()
288
287
289
288
fs = cov .get_data ().measured_files ()
290
289
lines .append (cov .get_data ().lines (list (fs )[0 ]))
@@ -363,30 +362,26 @@ def test_start_stop_start_stop(self) -> None:
363
362
def test_start_save_stop (self ) -> None :
364
363
self .make_code1_code2 ()
365
364
cov = coverage .Coverage ()
366
- cov .start ()
367
- import_local_file ("code1" ) # pragma: nested
368
- cov .save () # pragma: nested
369
- import_local_file ("code2" ) # pragma: nested
370
- cov .stop ()
365
+ with cov .collect ():
366
+ import_local_file ("code1" )
367
+ cov .save ()
368
+ import_local_file ("code2" )
371
369
self .check_code1_code2 (cov )
372
370
373
371
def test_start_save_nostop (self ) -> None :
374
372
self .make_code1_code2 ()
375
373
cov = coverage .Coverage ()
376
- cov .start ()
377
- import_local_file ("code1" ) # pragma: nested
378
- cov .save () # pragma: nested
379
- import_local_file ("code2" ) # pragma: nested
380
- self .check_code1_code2 (cov ) # pragma: nested
381
- # Then stop it, or the test suite gets out of whack.
382
- cov .stop ()
374
+ with cov .collect ():
375
+ import_local_file ("code1" )
376
+ cov .save ()
377
+ import_local_file ("code2" )
378
+ self .check_code1_code2 (cov )
383
379
384
380
def test_two_getdata_only_warn_once (self ) -> None :
385
381
self .make_code1_code2 ()
386
382
cov = coverage .Coverage (source = ["." ], omit = ["code1.py" ])
387
- cov .start ()
388
- import_local_file ("code1" ) # pragma: nested
389
- cov .stop ()
383
+ with cov .collect ():
384
+ import_local_file ("code1" )
390
385
# We didn't collect any data, so we should get a warning.
391
386
with self .assert_warnings (cov , ["No data was collected" ]):
392
387
cov .get_data ()
@@ -398,17 +393,15 @@ def test_two_getdata_only_warn_once(self) -> None:
398
393
def test_two_getdata_warn_twice (self ) -> None :
399
394
self .make_code1_code2 ()
400
395
cov = coverage .Coverage (source = ["." ], omit = ["code1.py" , "code2.py" ])
401
- cov .start ()
402
- import_local_file ("code1" ) # pragma: nested
403
- # We didn't collect any data, so we should get a warning.
404
- with self .assert_warnings (cov , ["No data was collected" ]): # pragma: nested
405
- cov .save () # pragma: nested
406
- import_local_file ("code2" ) # pragma: nested
407
- # Calling get_data a second time after tracing some more will warn again.
408
- with self .assert_warnings (cov , ["No data was collected" ]): # pragma: nested
409
- cov .get_data () # pragma: nested
410
- # Then stop it, or the test suite gets out of whack.
411
- cov .stop ()
396
+ with cov .collect ():
397
+ import_local_file ("code1" )
398
+ # We didn't collect any data, so we should get a warning.
399
+ with self .assert_warnings (cov , ["No data was collected" ]):
400
+ cov .save ()
401
+ import_local_file ("code2" )
402
+ # Calling get_data a second time after tracing some more will warn again.
403
+ with self .assert_warnings (cov , ["No data was collected" ]):
404
+ cov .get_data ()
412
405
413
406
def make_good_data_files (self ) -> None :
414
407
"""Make some good data files."""
@@ -632,9 +625,7 @@ def test_switch_context_testrunner(self) -> None:
632
625
633
626
# Test runner starts
634
627
cov = coverage .Coverage ()
635
- cov .start ()
636
-
637
- if "pragma: nested" :
628
+ with cov .collect ():
638
629
# Imports the test suite
639
630
suite = import_local_file ("testsuite" )
640
631
@@ -648,7 +639,6 @@ def test_switch_context_testrunner(self) -> None:
648
639
649
640
# Runner finishes
650
641
cov .save ()
651
- cov .stop ()
652
642
653
643
# Labeled data is collected
654
644
data = cov .get_data ()
@@ -670,9 +660,7 @@ def test_switch_context_with_static(self) -> None:
670
660
671
661
# Test runner starts
672
662
cov = coverage .Coverage (context = "mysuite" )
673
- cov .start ()
674
-
675
- if "pragma: nested" :
663
+ with cov .collect ():
676
664
# Imports the test suite
677
665
suite = import_local_file ("testsuite" )
678
666
@@ -686,7 +674,6 @@ def test_switch_context_with_static(self) -> None:
686
674
687
675
# Runner finishes
688
676
cov .save ()
689
- cov .stop ()
690
677
691
678
# Labeled data is collected
692
679
data = cov .get_data ()
@@ -704,12 +691,11 @@ def test_switch_context_with_static(self) -> None:
704
691
def test_dynamic_context_conflict (self ) -> None :
705
692
cov = coverage .Coverage (source = ["." ])
706
693
cov .set_option ("run:dynamic_context" , "test_function" )
707
- cov .start ()
708
- with pytest .warns (Warning ) as warns : # pragma: nested
709
- # Switch twice, but only get one warning.
710
- cov .switch_context ("test1" )
711
- cov .switch_context ("test2" )
712
- cov .stop ()
694
+ with cov .collect ():
695
+ with pytest .warns (Warning ) as warns :
696
+ # Switch twice, but only get one warning.
697
+ cov .switch_context ("test1" )
698
+ cov .switch_context ("test2" )
713
699
assert_coverage_warnings (warns , "Conflicting dynamic contexts (dynamic-conflict)" )
714
700
715
701
def test_unknown_dynamic_context (self ) -> None :
@@ -725,10 +711,9 @@ def test_switch_context_unstarted(self) -> None:
725
711
with pytest .raises (CoverageException , match = msg ):
726
712
cov .switch_context ("test1" )
727
713
728
- cov .start ()
729
- cov .switch_context ("test2" ) # pragma: nested
714
+ with cov .collect ():
715
+ cov .switch_context ("test2" )
730
716
731
- cov .stop ()
732
717
with pytest .raises (CoverageException , match = msg ):
733
718
cov .switch_context ("test3" )
734
719
@@ -750,9 +735,8 @@ def test_config_crash_no_crash(self) -> None:
750
735
def test_run_debug_sys (self ) -> None :
751
736
# https://github.com/nedbat/coveragepy/issues/907
752
737
cov = coverage .Coverage ()
753
- cov .start ()
754
- d = dict (cov .sys_info ()) # pragma: nested
755
- cov .stop ()
738
+ with cov .collect ():
739
+ d = dict (cov .sys_info ())
756
740
assert cast (str , d ['data_file' ]).endswith (".coverage" )
757
741
758
742
@@ -779,12 +763,10 @@ def test_current(self) -> None:
779
763
self .assert_current_is_none (cur1 )
780
764
assert cur0 is cur1
781
765
# Starting the instance makes it current.
782
- cov .start ()
783
- if "# pragma: nested" :
766
+ with cov .collect ():
784
767
cur2 = coverage .Coverage .current ()
785
768
assert cur2 is cov
786
769
# Stopping the instance makes current None again.
787
- cov .stop ()
788
770
789
771
cur3 = coverage .Coverage .current ()
790
772
self .assert_current_is_none (cur3 )
@@ -900,9 +882,8 @@ def coverage_usepkgs_counts(self, **kwargs: TCovKwargs) -> Dict[str, int]:
900
882
901
883
"""
902
884
cov = coverage .Coverage (** kwargs )
903
- cov .start ()
904
- import usepkgs # pragma: nested # pylint: disable=import-error, unused-import
905
- cov .stop ()
885
+ with cov .collect ():
886
+ import usepkgs # pylint: disable=import-error, unused-import
906
887
with self .assert_warnings (cov , []):
907
888
data = cov .get_data ()
908
889
summary = line_counts (data )
@@ -993,9 +974,8 @@ class ReportIncludeOmitTest(IncludeOmitTestsMixin, CoverageTest):
993
974
def coverage_usepkgs (self , ** kwargs : TCovKwargs ) -> Iterable [str ]:
994
975
"""Try coverage.report()."""
995
976
cov = coverage .Coverage ()
996
- cov .start ()
997
- import usepkgs # pragma: nested # pylint: disable=import-error, unused-import
998
- cov .stop ()
977
+ with cov .collect ():
978
+ import usepkgs # pylint: disable=import-error, unused-import
999
979
report = io .StringIO ()
1000
980
cov .report (file = report , ** kwargs )
1001
981
return report .getvalue ()
@@ -1012,9 +992,8 @@ class XmlIncludeOmitTest(IncludeOmitTestsMixin, CoverageTest):
1012
992
def coverage_usepkgs (self , ** kwargs : TCovKwargs ) -> Iterable [str ]:
1013
993
"""Try coverage.xml_report()."""
1014
994
cov = coverage .Coverage ()
1015
- cov .start ()
1016
- import usepkgs # pragma: nested # pylint: disable=import-error, unused-import
1017
- cov .stop ()
995
+ with cov .collect ():
996
+ import usepkgs # pylint: disable=import-error, unused-import
1018
997
cov .xml_report (outfile = "-" , ** kwargs )
1019
998
return self .stdout ()
1020
999
0 commit comments