Skip to content

Commit 3e7d8e1

Browse files
authored
support setting only --cov-report arg (#24225)
fixes #24168. We should support `--cov-report=` set without the need to set `--cov=` since the default (`--cov=.`) will work for many users
1 parent 7d01dc2 commit 3e7d8e1

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

python_files/tests/pytestadapter/test_coverage.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import pathlib
55
import sys
66

7+
import pytest
8+
79
script_dir = pathlib.Path(__file__).parent.parent
810
sys.path.append(os.fspath(script_dir))
911

@@ -42,3 +44,47 @@ def test_simple_pytest_coverage():
4244
assert focal_function_coverage.get("lines_missed") is not None
4345
assert set(focal_function_coverage.get("lines_covered")) == {4, 5, 7, 9, 10, 11, 12, 13, 14, 17}
4446
assert set(focal_function_coverage.get("lines_missed")) == {18, 19, 6}
47+
48+
49+
coverage_file_path = TEST_DATA_PATH / "coverage_gen" / "coverage.json"
50+
51+
52+
@pytest.fixture
53+
def cleanup_coverage_file():
54+
# delete the coverage file if it exists as part of test cleanup
55+
yield
56+
if os.path.exists(coverage_file_path): # noqa: PTH110
57+
os.remove(coverage_file_path) # noqa: PTH107
58+
59+
60+
def test_coverage_gen_report(cleanup_coverage_file): # noqa: ARG001
61+
"""
62+
Test coverage payload is correct for simple pytest example. Output of coverage run is below.
63+
64+
Name Stmts Miss Branch BrPart Cover
65+
---------------------------------------------------
66+
__init__.py 0 0 0 0 100%
67+
reverse.py 13 3 8 2 76%
68+
test_reverse.py 11 0 0 0 100%
69+
---------------------------------------------------
70+
TOTAL 24 3 8 2 84%
71+
72+
"""
73+
args = ["--cov-report=json"]
74+
env_add = {"COVERAGE_ENABLED": "True"}
75+
cov_folder_path = TEST_DATA_PATH / "coverage_gen"
76+
actual = runner_with_cwd_env(args, cov_folder_path, env_add)
77+
assert actual
78+
coverage = actual[-1]
79+
assert coverage
80+
results = coverage["result"]
81+
assert results
82+
assert len(results) == 3
83+
focal_function_coverage = results.get(os.fspath(TEST_DATA_PATH / "coverage_gen" / "reverse.py"))
84+
assert focal_function_coverage
85+
assert focal_function_coverage.get("lines_covered") is not None
86+
assert focal_function_coverage.get("lines_missed") is not None
87+
assert set(focal_function_coverage.get("lines_covered")) == {4, 5, 7, 9, 10, 11, 12, 13, 14, 17}
88+
assert set(focal_function_coverage.get("lines_missed")) == {18, 19, 6}
89+
# assert that the coverage file was created at the right path
90+
assert os.path.exists(coverage_file_path) # noqa: PTH110

python_files/vscode_pytest/run_pytest_script.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ def run_pytest(args):
4141
if is_coverage_run == "True":
4242
# If coverage is enabled, check if the coverage plugin is already in the args, if so keep user args.
4343
for arg in args:
44-
if "--cov" in arg:
44+
# if '--cov' is an arg or if '--cov=' is in an arg (check to see if this arg is set to not override user intent)
45+
if arg == "--cov" or "--cov=" in arg:
46+
print("coverage already enabled with specific args")
4547
coverage_enabled = True
4648
break
4749
if not coverage_enabled:

0 commit comments

Comments
 (0)