write-python-tests

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Write Python Tests

编写Python测试

Guidelines

指南

  • Source code is the source of truth.
  • Inspect
    pyproject.toml
    ,
    noxfile.py
    ,
    .config/mise/
    , nearby tests, and
    tests/conftest.py
    before editing.
  • Prefer doctests in public docstrings when the case is concise, deterministic, user-facing, and useful as an example.
  • Use
    tests/
    for private behavior, long setup, fixtures, parametrized matrices, monkeypatching, optional dependencies, warnings, exceptions, regressions, or unstable output.
  • 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
    uv run pytest ...
    for targeted tests and doctests.
  • Run
    uv run nox
    for the full test matrix.
  • Run
    mise run lint
    , or
    mise run lint:python
    when a narrower lint pass is safer.
  • Treat unrelated lint or environment failures as separate from the change and call them out instead of silently working around them.
  • 从最窄的相关目标开始。
  • 运行
    uv run pytest ...
    来执行目标测试和doctest。
  • 运行
    uv run nox
    来执行完整的测试矩阵。
  • 运行
    mise run lint
    ,或者当更窄范围的lint检查更安全时运行
    mise run lint:python
  • 将无关的lint或环境故障视为与本次更改无关的问题,单独指出而非默默规避。