Skip to content

Commit da4816c

Browse files
committed
add version check to tests
1 parent e096196 commit da4816c

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

python_files/tests/pytestadapter/test_coverage.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import pathlib
66
import sys
77

8+
import coverage
89
import pytest
10+
from packaging.version import Version
911

1012
script_dir = pathlib.Path(__file__).parent.parent
1113
sys.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

5258
coverage_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-

python_files/tests/unittestadapter/test_coverage.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import pathlib
99
import sys
1010

11+
import coverage
1112
import pytest
13+
from packaging.version import Version
1214

1315
sys.path.append(os.fspath(pathlib.Path(__file__).parent))
1416

@@ -40,9 +42,9 @@ def test_basic_coverage():
4042
)
4143

4244
assert actual
43-
coverage = actual[-1]
44-
assert coverage
45-
results = coverage["result"]
45+
cov = actual[-1]
46+
assert cov
47+
results = cov["result"]
4648
assert results
4749
assert len(results) == 3
4850
focal_function_coverage = results.get(os.fspath(TEST_DATA_PATH / "coverage_ex" / "reverse.py"))
@@ -51,8 +53,11 @@ def test_basic_coverage():
5153
assert focal_function_coverage.get("lines_missed") is not None
5254
assert set(focal_function_coverage.get("lines_covered")) == {4, 5, 7, 9, 10, 11, 12, 13, 14}
5355
assert set(focal_function_coverage.get("lines_missed")) == {6}
54-
assert focal_function_coverage.get("executed_branches") == 3
55-
assert focal_function_coverage.get("total_branches") == 4
56+
coverage_version = Version(coverage.__version__)
57+
# only include check for branches if the version is >= 7.7.0
58+
if coverage_version >= Version("7.7.0"):
59+
assert focal_function_coverage.get("executed_branches") == 3
60+
assert focal_function_coverage.get("total_branches") == 4
5661

5762

5863
@pytest.mark.parametrize("manage_py_file", ["manage.py", "old_manage.py"])
@@ -81,9 +86,9 @@ def test_basic_django_coverage(manage_py_file):
8186
)
8287

8388
assert actual
84-
coverage = actual[-1]
85-
assert coverage
86-
results = coverage["result"]
89+
cov = actual[-1]
90+
assert cov
91+
results = cov["result"]
8792
assert results
8893
assert len(results) == 16
8994
polls_views_coverage = results.get(str(data_path / "polls" / "views.py"))

0 commit comments

Comments
 (0)