|
4 | 4 | import pathlib
|
5 | 5 | import sys
|
6 | 6 |
|
| 7 | +import pytest |
| 8 | + |
7 | 9 | script_dir = pathlib.Path(__file__).parent.parent
|
8 | 10 | sys.path.append(os.fspath(script_dir))
|
9 | 11 |
|
@@ -42,3 +44,47 @@ def test_simple_pytest_coverage():
|
42 | 44 | assert focal_function_coverage.get("lines_missed") is not None
|
43 | 45 | assert set(focal_function_coverage.get("lines_covered")) == {4, 5, 7, 9, 10, 11, 12, 13, 14, 17}
|
44 | 46 | 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 |
0 commit comments