/scientific-python-development:python-testing runs instead · from scientific-python-development
Writes and organizes tests for scientific Python packages using pytest. Covers fixtures, parametrization, numerical assertions with NumPy, property-based testing with Hypothesis, and CI integration.
This skill never runs in Claude Code
It ships in scientific-python-development at skills/, but /scientific-python-development:python-testing (a command, or a skill Claude Code finds first) has the same name: typing it, or Claude choosing it, runs that one instead.
# Scalar floating-point → pytest.approx (default rel=1e-6)
from pytest import approx
assert result == approx(0.333, rel=1e-6)
assert result == approx(0.0, abs=1e-10) # near-zero: use abs tolerance
# NumPy array, element-wise → approx works; for complex tolerances use numpy.testing
assert np.array([0.1, 0.2]) == approx(np.array([0.1, 0.2]))
np.testing.assert_allclose(result, expected, rtol=1e-7, atol=1e-10)
# Exact integer arrays
np.testing.assert_array_equal(result, expected)
# Exceptions
with pytest.raises(ValueError, match="must be positive"):
compute_sqrt(-1)
# Warnings
with pytest.warns(DeprecationWarning):
old_function()
# Multiple inputs → parametrize
@pytest.mark.parametrize("x,expected", [(0, 0), (2, 4), (-2, 4)])
def test_square(x, expected):
assert x**2 == expected
# Reusable setup → fixture
@pytest.fixture
def sample_data():
return np.array([1.0, 2.0, 3.0, 4.0, 5.0])
def test_mean(sample_data):
assert np.mean(sample_data) == approx(3.0)
# Property-based (Hypothesis)
from hypothesis import given, strategies as st
from hypothesis.extra.numpy import arrays
@given(arrays(np.float64, shape=st.integers(1, 50),
elements=st.floats(-100, 100, allow_nan=False)))
def test_mean_bounded(arr):
assert np.min(arr) <= np.mean(arr) <= np.max(arr)
pyproject.toml)[tool.pytest.ini_options]
minversion = "8.0"
addopts = ["-ra", "--showlocals", "--strict-markers", "--strict-config", "--import-mode=importlib"]
testpaths = ["tests"]
markers = [
"slow: mark test as slow",
"integration: mark as integration test",
]
[tool.coverage.run]
source = ["src"]
branch = true
[tool.coverage.report]
fail_under = 80
See assets/pyproject-pytest.toml for the complete annotated configuration and assets/conftest-example.py for shared fixtures.
my-package/
├── src/my_package/
├── tests/
│ ├── conftest.py ← shared fixtures
│ ├── test_analysis.py
│ └── test_utils.py
└── pyproject.toml
No __init__.py in tests/; --import-mode=importlib (above) lets test files in different directories share a basename. Install the package in editable mode so tests run against it: with pixi, add it as an editable PyPI dependency and run pixi run pytest; otherwise pip install -e ..
| Reference | Contents |
|---|---|
references/test-patterns.md | Focused tests (Arrange-Act-Assert), exceptions/warnings, floating-point comparisons, fixtures with scope and teardown, parametrization with custom IDs, markers (slow/skip/xfail), directory-based test suites, mocking/monkeypatching |
references/scientific-patterns.md | Numerical stability and convergence tests, dtype parametrization, stochastic code with fixed seeds, data pipeline testing, Hypothesis property-based testing |
references/common-pitfalls.md | Testing implementation vs. behaviour, non-deterministic tests, exact float comparisons, testing too much in one test |
Non-obvious gotchas:
approx vs assert_allclose: pytest.approx is cleaner for most cases; use numpy.testing.assert_allclose when you need different rtol/atol per element or need better error messages for large arrays.abs tolerance near zero: relative tolerance is scaled by the expected value, so approx(0.0) falls back to the default abs=1e-12 and fails for ordinary round-off (1e-9 == approx(0.0) is False). Pass an explicit abs= sized to your numerics whenever the expected value is or could be zero.--strict-markers: add it from day one; it turns typo'd marker names into errors rather than silent no-ops.scope="session" fixtures are shared across the whole run — any mutation bleeds into other tests. Use scope="module" for read-only shared data; keep mutable fixtures at scope="function".np.random.default_rng(42) over np.random.seed(42); the latter sets global state and can interact with other tests.tmp_path built-in: pytest provides tmp_path (a pathlib.Path to a per-test temp dir) for free — no need to write your own temp-file fixture.# Run
pytest # all tests
pytest tests/test_analysis.py # one file
pytest tests/test_analysis.py::test_mean # one test
pytest -k "mean or median" # pattern match
pytest -m "not slow" # skip slow tests
# Debug
pytest -x # stop at first failure
pytest --lf # rerun last failures only
pytest --pdb # drop into debugger on failure
pytest --collect-only # dry run (see what would run)
# Coverage
pytest --cov=src --cov-report=term-missing
pytest --cov=src --cov-fail-under=80
1. Write the test
└─ Run it: pytest tests/test_foo.py::test_new -v
└─ It should FAIL (if it passes on unwritten code, the test is wrong)
2. Write the implementation
3. Run the test again
└─ It should now PASS
4. Run the full suite
└─ pytest -x (stop on first regression)
5. Check coverage
└─ pytest --cov=src --cov-report=term-missing
└─ Are the new lines covered?
6. Commit
└─ All tests green, coverage threshold met
Checkpoint: always verify a new test fails before the implementation exists.
A test that passes on broken code gives false confidence and will never catch regressions.
See assets/github-actions-tests.yml for a ready-to-use GitHub Actions workflow (matrix across Python versions, coverage upload).
Minimal workflow step:
- name: Run tests
run: pytest --cov=src --cov-report=xml
- name: Upload coverage
uses: codecov/codecov-action@303a32d7a59b442fa8d48b6a1cc6825c09c847a5 # v7.1.1 (pin to a full SHA)
assets/conftest-example.py (opens in a new tab)assets/github-actions-tests.yml (opens in a new tab)assets/pyproject-pytest.toml (opens in a new tab)references/common-pitfalls.md (opens in a new tab)references/scientific-patterns.md (opens in a new tab)references/test-patterns.md (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
pytest, scientific-testing, hypothesis, numpy-testing, ci-integration
Generated from the plugin's indexed repository (uw-ssec/savvy) — no native marketplace selected
npx claudepluginhub p/uw-ssec-scientific-python-development-plugins-scientific-python-development5 skills in this plugin
/code-quality-toolsUse when the user asks about setting up linting, type checking, code formatting, or pre-commit hooks for Python projects, or mentions ruff, mypy, or pre-commit configuration. Configure and use automated code quality tools (ruff, mypy, pre-commit) for scientific Python projects. Covers linting rules, type checking configuration, formatting, and CI integration./pixi-package-managerUse when the user works with pixi (pixi.toml, pixi.lock, pixi add/run/task/global/exec) or wants a reproducible scientific Python environment that mixes conda-forge and PyPI packages: creating environments, adding dependencies, defining tasks, multi-platform lockfiles, or migrating from conda environment.yml / requirements.txt. Not for pure uv/pip projects or for building and publishing packages to PyPI (use python-packaging)./python-packagingCreate and publish distributable scientific Python packages following Scientific Python community best practices. Covers pyproject.toml, src layout, Hatchling, metadata, CLI entry points, and PyPI publishing. Use when the user wants to create a new Python package, set up pyproject.toml, configure Hatchling, publish to PyPI, or follow Scientific Python packaging standards./scientific-documentationUse when the user asks about setting up docs for a Python package, configuring Sphinx or MkDocs, writing NumPy-style docstrings, or deploying to Read the Docs. Set up and maintain documentation for scientific Python packages. Covers Sphinx, MkDocs, NumPy-style docstrings, Diataxis framework, accessibility standards, and documentation hosting with Read the Docs.Other plugins' skills for the same job