python-testing

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Python Testing Hub

Python测试中心

Testing standards for pytest configuration, fixture management, and TDD implementation.
本指南涵盖pytest配置、fixture管理和TDD实现的测试标准。

Table of Contents

目录

Quick Start

快速开始

  1. Dependencies:
    pip install pytest pytest-cov pytest-asyncio pytest-mock
  2. Configuration: Add the following to
    pyproject.toml
    :
    toml
    [tool.pytest.ini_options]
    testpaths = ["tests"]
    addopts = "--cov=src"
  3. Verification: Run
    pytest
    to confirm discovery of files matching
    test_*.py
    .
  1. 依赖项
    pip install pytest pytest-cov pytest-asyncio pytest-mock
  2. 配置:在
    pyproject.toml
    中添加以下内容:
    toml
    [tool.pytest.ini_options]
    testpaths = ["tests"]
    addopts = "--cov=src"
  3. 验证:运行
    pytest
    确认能发现匹配
    test_*.py
    的文件。

When To Use

适用场景

  • Constructing unit and integration tests for Python 3.9+ projects.
  • Isolating external dependencies using
    pytest-mock
    or custom monkeypatching.
  • Validating asynchronous logic with
    pytest-asyncio
    markers and event loop management.
  • Configuring project-wide coverage thresholds and reporting.
  • 为Python 3.9+项目构建单元测试和集成测试。
  • 使用
    pytest-mock
    或自定义monkeypatching隔离外部依赖。
  • 使用
    pytest-asyncio
    标记和事件循环管理验证异步逻辑。
  • 配置项目级别的覆盖率阈值和报告。

When NOT To Use

不适用场景

  • Evaluating test quality - use pensive:test-review instead
  • Infrastructure test config - use leyline:pytest-config
  • Evaluating test quality - use pensive:test-review instead
  • Infrastructure test config - use leyline:pytest-config
  • 评估测试质量时 - 请改用pensive:test-review
  • 基础设施测试配置时 - 请改用leyline:pytest-config
  • 评估测试质量时 - 请改用pensive:test-review
  • 基础设施测试配置时 - 请改用leyline:pytest-config

Modules

模块

This skill uses modular loading to manage the system prompt budget.
本技能采用模块化加载方式管理系统提示词预算。

Core Implementation

核心实现

  • See
    modules/unit-testing.md
    - AAA (Arrange-Act-Assert) pattern, basic test structure, and exception validation.
  • See
    modules/fixtures-and-mocking.md
    - Request-scoped fixtures, parameterization, and boundary mocking.
  • See
    modules/async-testing.md
    - Coroutine testing, async fixtures, and concurrency validation.
  • 查看
    modules/unit-testing.md
    - AAA(Arrange-Act-Assert)模式、基础测试结构和异常验证。
  • 查看
    modules/fixtures-and-mocking.md
    - 请求作用域fixtures、参数化和边界模拟。
  • 查看
    modules/async-testing.md
    - 协程测试、异步fixtures和并发验证。

Infrastructure & Workflow

基础设施与工作流

  • See
    modules/test-infrastructure.md
    - Directory standards,
    conftest.py
    management, and coverage tools.
  • See
    modules/testing-workflows.md
    - Local execution patterns and GitHub Actions integration.
  • 查看
    modules/test-infrastructure.md
    - 目录标准、
    conftest.py
    管理和覆盖率工具。
  • 查看
    modules/testing-workflows.md
    - 本地执行模式和GitHub Actions集成。

Standards

标准

  • See
    modules/test-quality.md
    - Identification of common anti-patterns like broad exception catching or shared state between tests.
  • 查看
    modules/test-quality.md
    - 识别常见反模式,如宽泛的异常捕获或测试间共享状态。

Exit Criteria

验收标准

  • Tests implement the AAA pattern.
  • Coverage reaches the 80% project minimum.
  • Individual tests are independent and do not rely on execution order.
  • Fixtures are scoped appropriately (function, class, or session) to prevent side effects.
  • Mocking is restricted to external system boundaries.
  • 测试实现AAA模式。
  • 覆盖率达到项目最低要求的80%。
  • 单个测试相互独立,不依赖执行顺序。
  • Fixtures的作用域设置恰当(函数、类或会话级别),以避免副作用。
  • 仅在外部系统边界处使用模拟。

Troubleshooting

故障排除

  • Test Discovery: Verify filenames match the
    test_*.py
    pattern. Use
    pytest --collect-only
    to debug discovery paths.
  • Import Errors: Ensure the local source directory is in the path, typically by installing in editable mode with
    pip install -e .
    .
  • Async Failures: Confirm that
    pytest-asyncio
    is installed and that async tests use the
    @pytest.mark.asyncio
    decorator or corresponding auto-mode configuration.
  • 测试发现问题:验证文件名是否符合
    test_*.py
    模式。使用
    pytest --collect-only
    调试发现路径。
  • 导入错误:确保本地源码目录在路径中,通常可通过
    pip install -e .
    以可编辑模式安装项目。
  • 异步测试失败:确认已安装
    pytest-asyncio
    ,且异步测试使用
    @pytest.mark.asyncio
    装饰器或相应的自动模式配置。