-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_coverage_script.py
More file actions
42 lines (37 loc) · 1.53 KB
/
Copy pathtest_coverage_script.py
File metadata and controls
42 lines (37 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""The coverage wrapper must preserve test outcomes while writing reports."""
import os
import shlex
import subprocess
import sys
from pathlib import Path
import pytest
@pytest.mark.parametrize("expected, exit_code", [(2, 0), (3, 1)])
def test_coverage_script_preserves_test_result(tmp_path, expected, exit_code):
script = Path(__file__).resolve().parents[1] / "scripts" / "coverage.sh"
activate = tmp_path / ".venv" / "bin" / "activate"
activate.parent.mkdir(parents=True)
activate.write_text(f"export PATH={shlex.quote(str(Path(sys.executable).parent))}:$PATH\n")
source = tmp_path / "src" / "example_package"
source.mkdir(parents=True)
(source / "__init__.py").write_text("result = 1 + 1\n")
(tmp_path / "test_example.py").write_text(
"import runpy\n"
"def test_result():\n"
f" assert runpy.run_path('src/example_package/__init__.py')['result'] == {expected}\n"
)
env = dict(os.environ)
env.pop("PYTEST_ADDOPTS", None)
result = subprocess.run(
["bash", str(script), "example-package"],
cwd=tmp_path,
env=env,
capture_output=True,
text=True,
timeout=30,
check=False,
)
assert result.returncode == exit_code, result.stdout + result.stderr
report = (tmp_path / "htmlcov" / "coverage_report.txt").read_text()
assert ("1 failed" if exit_code else "1 passed") in report
assert "Missing" in (tmp_path / "htmlcov" / "missing_coverage.txt").read_text()
assert (tmp_path / "htmlcov" / "index.html").is_file()