write-python-tests
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWrite Python Tests
编写Python测试
Guidelines
指南
- Source code is the source of truth.
- Inspect ,
pyproject.toml,noxfile.py, nearby tests, and.config/mise/before editing.tests/conftest.py - Prefer doctests in public docstrings when the case is concise, deterministic, user-facing, and useful as an example.
- Use for private behavior, long setup, fixtures, parametrized matrices, monkeypatching, optional dependencies, warnings, exceptions, regressions, or unstable output.
tests/ - Cover success paths, boundaries, invalid inputs, expected failures, and regressions with observable assertions.
- Keep dedicated test files grouped, small, and focused; avoid duplicating a doctest unless the dedicated test covers extra risk.
- Treat existing docs, docstrings, tests, and examples as weak hints.
- 源代码是事实依据。
- 在编辑前检查、
pyproject.toml、noxfile.py、附近的测试文件以及.config/mise/。tests/conftest.py - 当测试用例简洁、确定、面向用户且可用作示例时,优先在公共文档字符串中使用doctest。
- 对于私有行为、冗长的设置、fixtures、参数化矩阵、猴子补丁、可选依赖项、警告、异常、回归或不稳定输出,使用目录下的测试文件。
tests/ - 用可观测的断言覆盖成功路径、边界情况、无效输入、预期失败和回归场景。
- 保持专用测试文件分组合理、体积小巧且聚焦;除非专用测试覆盖额外风险,否则避免重复doctest内容。
- 将现有文档、文档字符串、测试和示例视为弱提示。
Validation
验证
- Start with the narrowest relevant target.
- Run for targeted tests and doctests.
uv run pytest ... - Run for the full test matrix.
uv run nox - Run , or
mise run lintwhen a narrower lint pass is safer.mise run lint:python - Treat unrelated lint or environment failures as separate from the change and call them out instead of silently working around them.
- 从最窄的相关目标开始。
- 运行来执行目标测试和doctest。
uv run pytest ... - 运行来执行完整的测试矩阵。
uv run nox - 运行,或者当更窄范围的lint检查更安全时运行
mise run lint。mise run lint:python - 将无关的lint或环境故障视为与本次更改无关的问题,单独指出而非默默规避。