Skip to content

Commit 7e47998

Browse files
committed
fix: use coverage run instead of pytest --cov for accurate import-time coverage
The pytest11 entry point (`zarr = "zarr.testing"`) imports 76 zarr modules during plugin loading, before pytest-cov starts measuring. This caused all module-level code (class definitions, imports, decorators) to appear uncovered, dropping reported coverage well below actual test coverage. `coverage run -m pytest` starts tracing before any imports, resolving the issue.
1 parent da174e3 commit 7e47998

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

pyproject.toml

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ exclude_also = [
142142
[tool.coverage.run]
143143
omit = [
144144
"bench/compress_normal.py",
145+
"src/zarr/_version.py", # auto-generated by setuptools-scm
145146
"src/zarr/testing/conftest.py", # only for downstream projects
146147
]
147148

@@ -155,10 +156,6 @@ hooks.vcs.version-file = "src/zarr/_version.py"
155156
dependency-groups = ["test"]
156157

157158
[tool.hatch.envs.test.env-vars]
158-
# Required to test with a pytest plugin; see https://pytest-cov.readthedocs.io/en/latest/plugins.html
159-
COV_CORE_SOURCE = "src"
160-
COV_CORE_CONFIG = "pyproject.toml"
161-
COV_CORE_DATAFILE = ".coverage.eager"
162159

163160
[[tool.hatch.envs.test.matrix]]
164161
python = ["3.12", "3.13", "3.14"]
@@ -175,13 +172,23 @@ matrix.deps.dependency-groups = [
175172
]
176173

177174
[tool.hatch.envs.test.scripts]
178-
run-coverage = "pytest --cov-config=pyproject.toml --cov=src --cov-report xml --junitxml=junit.xml -o junit_family=legacy"
179-
run-coverage-html = "pytest --cov-config=pyproject.toml --cov=src --cov-report html"
180-
run = "run-coverage --no-cov --ignore tests/benchmarks"
175+
run-coverage = [
176+
"coverage run --source=src -m pytest --junitxml=junit.xml -o junit_family=legacy {args:}",
177+
"coverage xml",
178+
]
179+
run-coverage-html = [
180+
"coverage run --source=src -m pytest {args:}",
181+
"coverage html",
182+
]
183+
run = "pytest --ignore tests/benchmarks"
181184
run-verbose = "run-coverage --verbose"
182185
run-mypy = "mypy src"
183-
run-hypothesis = "run-coverage -nauto --run-slow-hypothesis tests/test_properties.py tests/test_store/test_stateful*"
186+
run-hypothesis = [
187+
"coverage run --source=src -m pytest -nauto --run-slow-hypothesis tests/test_properties.py tests/test_store/test_stateful* {args:}",
188+
"coverage xml",
189+
]
184190
run-benchmark = "pytest --benchmark-enable tests/benchmarks"
191+
serve-coverage-html = "python -m http.server -d htmlcov 8000"
185192
list-env = "pip list"
186193

187194
[tool.hatch.envs.gputest]
@@ -195,8 +202,11 @@ features = ["gpu"]
195202
python = ["3.12", "3.13"]
196203

197204
[tool.hatch.envs.gputest.scripts]
198-
run-coverage = "pytest -m gpu --cov-config=pyproject.toml --cov=src --cov-report xml --junitxml=junit.xml -o junit_family=legacy --ignore tests/benchmarks"
199-
run = "run-coverage --no-cov"
205+
run-coverage = [
206+
"coverage run --source=src -m pytest -m gpu --junitxml=junit.xml -o junit_family=legacy --ignore tests/benchmarks {args:}",
207+
"coverage xml",
208+
]
209+
run = "pytest -m gpu --ignore tests/benchmarks"
200210

201211
[tool.hatch.envs.upstream]
202212
template = 'test'

0 commit comments

Comments
 (0)