55import pathlib
66import sys
77
8+ import coverage
89import pytest
10+ from packaging .version import Version
911
1012script_dir = pathlib .Path (__file__ ).parent .parent
1113sys .path .append (os .fspath (script_dir ))
@@ -34,9 +36,9 @@ def test_simple_pytest_coverage():
3436 cov_folder_path = TEST_DATA_PATH / "coverage_gen"
3537 actual = runner_with_cwd_env (args , cov_folder_path , env_add )
3638 assert actual
37- coverage = actual [- 1 ]
38- assert coverage
39- results = coverage ["result" ]
39+ cov = actual [- 1 ]
40+ assert cov
41+ results = cov ["result" ]
4042 assert results
4143 assert len (results ) == 3
4244 focal_function_coverage = results .get (os .fspath (TEST_DATA_PATH / "coverage_gen" / "reverse.py" ))
@@ -45,8 +47,12 @@ def test_simple_pytest_coverage():
4547 assert focal_function_coverage .get ("lines_missed" ) is not None
4648 assert set (focal_function_coverage .get ("lines_covered" )) == {4 , 5 , 7 , 9 , 10 , 11 , 12 , 13 , 14 , 17 }
4749 assert len (set (focal_function_coverage .get ("lines_missed" ))) >= 3
48- assert focal_function_coverage .get ("executed_branches" ) == 4
49- assert focal_function_coverage .get ("total_branches" ) == 6
50+
51+ coverage_version = Version (coverage .__version__ )
52+ # only include check for branches if the version is >= 7.7.0
53+ if coverage_version >= Version ("7.7.0" ):
54+ assert focal_function_coverage .get ("executed_branches" ) == 4
55+ assert focal_function_coverage .get ("total_branches" ) == 6
5056
5157
5258coverage_gen_file_path = TEST_DATA_PATH / "coverage_gen" / "coverage.json"
@@ -79,9 +85,9 @@ def test_coverage_gen_report(cleanup_coverage_gen_file): # noqa: ARG001
7985 print ("cov_folder_path" , cov_folder_path )
8086 actual = runner_with_cwd_env (args , cov_folder_path , env_add )
8187 assert actual
82- coverage = actual [- 1 ]
83- assert coverage
84- results = coverage ["result" ]
88+ cov = actual [- 1 ]
89+ assert cov
90+ results = cov ["result" ]
8591 assert results
8692 assert len (results ) == 3
8793 focal_function_coverage = results .get (os .fspath (TEST_DATA_PATH / "coverage_gen" / "reverse.py" ))
@@ -90,8 +96,11 @@ def test_coverage_gen_report(cleanup_coverage_gen_file): # noqa: ARG001
9096 assert focal_function_coverage .get ("lines_missed" ) is not None
9197 assert set (focal_function_coverage .get ("lines_covered" )) == {4 , 5 , 7 , 9 , 10 , 11 , 12 , 13 , 14 , 17 }
9298 assert set (focal_function_coverage .get ("lines_missed" )) == {18 , 19 , 6 }
93- assert focal_function_coverage .get ("executed_branches" ) == 4
94- assert focal_function_coverage .get ("total_branches" ) == 6
99+ coverage_version = Version (coverage .__version__ )
100+ # only include check for branches if the version is >= 7.7.0
101+ if coverage_version >= Version ("7.7.0" ):
102+ assert focal_function_coverage .get ("executed_branches" ) == 4
103+ assert focal_function_coverage .get ("total_branches" ) == 6
95104 # assert that the coverage file was created at the right path
96105 assert os .path .exists (coverage_gen_file_path ) # noqa: PTH110
97106
@@ -127,10 +136,9 @@ def test_coverage_w_omit_config():
127136 actual = runner_with_cwd_env ([], cov_folder_path , env_add )
128137 assert actual
129138 print ("actual" , json .dumps (actual , indent = 2 ))
130- coverage = actual [- 1 ]
131- assert coverage
132- results = coverage ["result" ]
139+ cov = actual [- 1 ]
140+ assert cov
141+ results = cov ["result" ]
133142 assert results
134143 # assert one file is reported and one file (as specified in pyproject.toml) is omitted
135144 assert len (results ) == 1
136-
0 commit comments