python-testing
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePython Testing Hub
Python测试中心
Testing standards for pytest configuration, fixture management, and TDD implementation.
本指南涵盖pytest配置、fixture管理和TDD实现的测试标准。
Table of Contents
目录
Quick Start
快速开始
- Dependencies:
pip install pytest pytest-cov pytest-asyncio pytest-mock - Configuration: Add the following to :
pyproject.tomltoml[tool.pytest.ini_options] testpaths = ["tests"] addopts = "--cov=src" - Verification: Run to confirm discovery of files matching
pytest.test_*.py
- 依赖项:
pip install pytest pytest-cov pytest-asyncio pytest-mock - 配置:在中添加以下内容:
pyproject.tomltoml[tool.pytest.ini_options] testpaths = ["tests"] addopts = "--cov=src" - 验证:运行确认能发现匹配
pytest的文件。test_*.py
When To Use
适用场景
- Constructing unit and integration tests for Python 3.9+ projects.
- Isolating external dependencies using or custom monkeypatching.
pytest-mock - Validating asynchronous logic with markers and event loop management.
pytest-asyncio - Configuring project-wide coverage thresholds and reporting.
- 为Python 3.9+项目构建单元测试和集成测试。
- 使用或自定义monkeypatching隔离外部依赖。
pytest-mock - 使用标记和事件循环管理验证异步逻辑。
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 - AAA (Arrange-Act-Assert) pattern, basic test structure, and exception validation.
modules/unit-testing.md - See - Request-scoped fixtures, parameterization, and boundary mocking.
modules/fixtures-and-mocking.md - See - Coroutine testing, async fixtures, and concurrency validation.
modules/async-testing.md
- 查看- AAA(Arrange-Act-Assert)模式、基础测试结构和异常验证。
modules/unit-testing.md - 查看- 请求作用域fixtures、参数化和边界模拟。
modules/fixtures-and-mocking.md - 查看- 协程测试、异步fixtures和并发验证。
modules/async-testing.md
Infrastructure & Workflow
基础设施与工作流
- See - Directory standards,
modules/test-infrastructure.mdmanagement, and coverage tools.conftest.py - See - Local execution patterns and GitHub Actions integration.
modules/testing-workflows.md
- 查看- 目录标准、
modules/test-infrastructure.md管理和覆盖率工具。conftest.py - 查看- 本地执行模式和GitHub Actions集成。
modules/testing-workflows.md
Standards
标准
- See - Identification of common anti-patterns like broad exception catching or shared state between tests.
modules/test-quality.md
- 查看- 识别常见反模式,如宽泛的异常捕获或测试间共享状态。
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 pattern. Use
test_*.pyto debug discovery paths.pytest --collect-only - 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 is installed and that async tests use the
pytest-asynciodecorator or corresponding auto-mode configuration.@pytest.mark.asyncio
- 测试发现问题:验证文件名是否符合模式。使用
test_*.py调试发现路径。pytest --collect-only - 导入错误:确保本地源码目录在路径中,通常可通过以可编辑模式安装项目。
pip install -e . - 异步测试失败:确认已安装,且异步测试使用
pytest-asyncio装饰器或相应的自动模式配置。@pytest.mark.asyncio