python-testing
БесплатноЗапускает вложенные скриптыНе проверенUse when writing or organizing tests for scientific Python code, or when the user mentions pytest, test fixtures, parametrization, numerical assertions, or…
от uw-ssec★ 33 звезды на GitHubОб этом скилле
Scientific Python Testing with pytest
Quick Reference
Choose the right assertion
# 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)
pytest configuration (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.
Project layout
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 ..
Patterns and Reference
| 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:
approxvsassert_allclose:pytest.approxis cleaner for most cases; usenumpy.testing.assert_allclosewhen you need differentrtol/atolper element or need better error messages for large arrays.abstolerance near zero: relative tolerance is scaled by the expected value, soapprox(0.0)falls back to the defaultabs=1e-12and fails for ordinary round-off (1e-9 == approx(0.0)isFalse). Pass an explicitabs=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.- Fixture scope:
scope="session"fixtures are shared across the whole run — any mutation bleeds into other tests. Usescope="module"for read-only shared data; keep mutable fixtures atscope="function". - Random seeds: prefer
np.random.default_rng(42)overnp.random.seed(42); the latter sets global state and can interact with other tests. tmp_pathbuilt-in: pytest providestmp_path(apathlib.Pathto a per-test temp dir) for free — no need to write your own temp-file fixture.
Commands
# 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
Workflow: Write → Verify → Ship
1. Wr
Установить python-testing в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
FAQ
Что делает скилл python-testing?
Use when writing or organizing tests for scientific Python code, or when the user mentions pytest, test fixtures, parametrization, numerical assertions, or property-based testing. Write and organize tests for scientific Python packages using pytest. Covers fixtures, parametrization, numerical testing with NumPy utilities, property-based testing with Hypothesis, and CI integration.
Как установить скилл python-testing?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл python-testing запускает скрипты?
Да, скилл несёт исполняемые скрипты. Проверь исходник перед установкой.
Похожие скиллы
doc-coauthoring
Guide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision…
от Anthropicacademy-guide
Stop and check this skill before finishing any reply to a question about how to use Claude or a Claude product — it recommends matching courses, tutorials…
от Anthropicinternal-comms
A set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill…
от Anthropicskill-development
This skill should be used when the user wants to "create a skill", "add a skill to plugin", "write a new skill", "improve skill description", "organize skill…
от Anthropic