python-testing

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Python Testing

Python测试

Overview

概述

Test observable behavior and contracts, not internal implementation. Keep unit tests fast, deterministic, and patched at module boundaries.
These are preferred defaults for common cases. When a default conflicts with project constraints, suggest a better-fit alternative, call out tradeoffs, and note compensating controls.
测试可观察的行为和契约,而非内部实现。保持单元测试快速、可预测,并在模块边界处进行补丁。
这些是针对常见场景的推荐默认准则。当默认准则与项目约束冲突时,请提出更合适的替代方案,说明权衡点,并标注补偿性控制措施。

When to Use

适用场景

  • Writing or reviewing unit, integration, or reliability-sensitive tests.
  • Tests are flaky, slow, or coupled to implementation details.
  • Adding regression tests after a bugfix.
  • Testing async lifecycles, cancellation, or cleanup paths.
  • Unsure what test coverage a change needs.
When NOT to use:
  • Pure data-shape or schema validation (see
    python-types-contracts
    ).
  • Production observability or monitoring concerns (see
    python-runtime-operations
    ).
  • Concurrency design decisions outside of test harnesses (see
    python-concurrency-performance
    ).
  • 编写或审查单元测试、集成测试或对可靠性敏感的测试。
  • 测试存在不稳定、运行缓慢或与实现细节耦合的问题。
  • 在修复bug后添加回归测试。
  • 测试异步生命周期、取消或清理路径。
  • 不确定某项变更需要哪些测试覆盖。
不适用场景:
  • 纯数据结构或模式验证(请参考
    python-types-contracts
    )。
  • 生产环境可观测性或监控相关问题(请参考
    python-runtime-operations
    )。
  • 测试框架之外的并发设计决策(请参考
    python-concurrency-performance
    )。

Quick Reference

快速参考

  • Test observable behavior, not internals.
  • Keep unit tests fast and deterministic.
  • Patch at module boundaries and import locations used by the unit under test.
  • Add regression tests for bugfixes.
  • Include timeout/retry/cancellation/cleanup coverage where relevant.
  • 测试可观察行为,而非内部实现。
  • 保持单元测试快速且可预测。
  • 在被测单元使用的模块边界和导入位置进行补丁。
  • 为bug修复添加回归测试。
  • 在相关场景中覆盖超时/重试/取消/清理逻辑。

Change-Specific Diagnostics

针对变更的诊断建议

  • Dependency updates: run
    uv run pytest scripts/test_pypi_security_audit.py -v
  • Async-heavy lifecycle changes: run
    pyleak
    diagnostics.
  • 依赖更新:运行
    uv run pytest scripts/test_pypi_security_audit.py -v
  • 涉及大量异步生命周期的变更:运行
    pyleak
    诊断。

Common Mistakes

常见误区

  • Mocking too deep — patching internals instead of module-boundary seams makes tests brittle and coupled to implementation.
  • Testing the mock — verifying mock call counts without asserting on observable output proves nothing about behavior.
  • Missing regression test — fixing a bug without a test that reproduces it first; the bug will recur.
  • Non-deterministic time/order — relying on wall-clock time or dict/set ordering instead of injecting clocks and sorting explicitly.
  • Skipping cleanup assertions — verifying the happy path but never asserting that resources are released on failure or cancellation.
  • 过度Mock —— 对内部实现而非模块边界进行补丁会导致测试脆弱且与实现细节耦合。
  • 测试Mock本身 —— 仅验证Mock的调用次数,却不断言可观察的输出,无法证明行为的正确性。
  • 缺失回归测试 —— 修复bug但未先编写可复现该bug的测试,bug很可能会再次出现。
  • 非确定性的时间/顺序 —— 依赖系统时间或字典/集合的默认顺序,而非注入时钟或显式排序。
  • 忽略清理断言 —— 仅验证正常路径,却从不断言失败或取消时资源是否已释放。

Scope Note

范围说明

  • Treat these recommendations as preferred defaults for common cases, not universal rules.
  • If a default conflicts with project constraints or worsens the outcome, suggest a better-fit alternative and explain why it is better for this case.
  • When deviating, call out tradeoffs and compensating controls (tests, observability, migration, rollback).
  • 请将这些建议视为常见场景下的推荐默认准则,而非通用规则。
  • 如果默认准则与项目约束冲突或会导致更差的结果,请提出更合适的替代方案,并解释为何该方案更适合当前场景。
  • 当偏离默认准则时,请说明权衡点和补偿性控制措施(测试、可观察性、迁移、回滚)。

Invocation Notice

调用说明

  • Inform the user when this skill is being invoked by name:
    python-design-modularity
    .
  • 当通过名称调用此技能时,请告知用户:
    python-design-modularity

References

参考资料

  • references/testing-strategy.md
  • references/pytest-practices.md
  • references/async-and-concurrency-testing.md
  • references/reliability-lifecycle-testing.md
  • references/testing-strategy.md
  • references/pytest-practices.md
  • references/async-and-concurrency-testing.md
  • references/reliability-lifecycle-testing.md