-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_render.py
More file actions
113 lines (85 loc) · 4.29 KB
/
Copy pathtest_render.py
File metadata and controls
113 lines (85 loc) · 4.29 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""Tests for rendering features into config/conftest/test strings."""
from __future__ import annotations
import pytest
from pytest_starter_config.presets import resolve_features
from pytest_starter_config.render import build, new_pyproject
# tomllib is stdlib on 3.11+; skip the TOML-parsing assertions on older runners.
tomllib = pytest.importorskip("tomllib")
def _pyproject_text(preset: str, package: str = "src") -> str:
result = build(resolve_features(preset).features, "pyproject", package)
return new_pyproject(result.pyproject_sections)
def test_minimal_renders_valid_toml_with_strict_defaults():
text = _pyproject_text("minimal")
data = tomllib.loads(text)
opts = data["tool"]["pytest"]["ini_options"]
assert "--strict-markers" in opts["addopts"]
assert "--strict-config" in opts["addopts"]
assert opts["xfail_strict"] is True
assert opts["filterwarnings"] == ["error"]
assert opts["testpaths"] == ["tests"]
def test_minimal_has_no_coverage_or_parallel_content():
text = _pyproject_text("minimal")
assert "--cov" not in text
assert "tool.coverage" not in text
assert "pytest-xdist" not in text
def test_full_includes_coverage_sections_and_addopts():
text = _pyproject_text("full")
data = tomllib.loads(text)
opts = data["tool"]["pytest"]["ini_options"]
assert "--cov=src" in opts["addopts"]
assert "--cov-fail-under=90" in opts["addopts"]
assert data["tool"]["coverage"]["run"]["branch"] is True
assert data["tool"]["coverage"]["run"]["source"] == ["src"]
assert data["tool"]["coverage"]["report"]["show_missing"] is True
def test_full_registers_all_four_markers():
text = _pyproject_text("full")
data = tomllib.loads(text)
markers = data["tool"]["pytest"]["ini_options"]["markers"]
names = {m.split(":", 1)[0] for m in markers}
assert names == {"slow", "integration", "unit", "smoke"}
def test_package_substitution_flows_into_coverage():
text = _pyproject_text("full", package="mypkg")
data = tomllib.loads(text)
assert "--cov=mypkg" in data["tool"]["pytest"]["ini_options"]["addopts"]
assert data["tool"]["coverage"]["run"]["source"] == ["mypkg"]
assert "__PACKAGE__" not in text
def test_parallel_and_randomize_emit_notes_and_plugins():
result = build(resolve_features("full").features, "pyproject", "src")
text = new_pyproject(result.pyproject_sections)
assert "pytest-xdist" in text
assert "-n auto" in text
assert "randomly-seed" in text
assert "pytest-cov" in result.plugins
assert "pytest-xdist" in result.plugins
assert "pytest-randomly" in result.plugins
def test_conftest_contains_the_documented_fixtures_and_hook():
result = build(resolve_features("full").features, "pyproject", "src")
conftest = result.files["conftest.py"]
assert "def tmp_workspace(" in conftest
assert "def deterministic_seed(" in conftest
assert "def make_user(" in conftest
assert "def pytest_addoption(" in conftest
assert "def pytest_collection_modifyitems(" in conftest
# Every fixture references the site.
assert "python-testing-debugging.com" in conftest
def test_conftest_is_syntactically_valid_python():
result = build(resolve_features("full").features, "pyproject", "src")
compile(result.files["conftest.py"], "conftest.py", "exec")
compile(result.files["tests/test_starter.py"], "test_starter.py", "exec")
def test_starter_test_uses_markers_and_parametrize():
result = build(resolve_features("standard").features, "pyproject", "src")
starter = result.files["tests/test_starter.py"]
assert "@pytest.mark.parametrize" in starter
assert "@pytest.mark.slow" in starter
assert "tmp_workspace" in starter
def test_ini_format_produces_pytest_ini_and_coveragerc():
result = build(resolve_features("full").features, "ini", "src")
assert "pytest.ini" in result.files
assert result.files["pytest.ini"].startswith("[pytest]")
assert ".coveragerc" in result.files
assert result.files[".coveragerc"].startswith("[run]")
# coverage config must NOT be jammed into pytest.ini (coverage won't read it).
assert "coverage" not in result.files["pytest.ini"]
def test_ini_format_without_coverage_omits_coveragerc():
result = build(resolve_features("minimal").features, "ini", "src")
assert ".coveragerc" not in result.files