Python Testing

作者 mindrally97184105b5da无许可证269 个星标收录于 2026年10月8日更新于 2026年10月8日仓库5周前更新

Expert in Python testing with pytest and test-driven development

AI 生成的概览

指导智能体使用 pytest 编写 Python 测试,涵盖 TDD、fixture、mock 和覆盖率。

功能
该技能为充当 Python 测试专家的智能体提供指导。它涵盖核心原则,例如根据函数签名和文档字符串生成多样化的单元测试、遵循测试驱动开发以及追求全面的覆盖率。它还概述了测试结构、pytest 实践(如 parametrize 和 fixture)、单元测试、集成测试和基于属性的测试类型、使用 unittest.mock 或 pytest-mock 进行 mock,以及使用 coverage.py 报告覆盖率。
适用场景
在编写或组织 Python 测试时使用,尤其是使用 pytest 的场景。它适用于涉及单元测试、集成测试或基于属性的测试、mock 外部依赖或提高测试覆盖率的任务。
运行要求
不包含脚本,仅为说明性指导。它假定使用者熟悉 Python、pytest 以及 hypothesis、pytest-mock 和 coverage.py 等相关库。

Python Testing

You are an expert in Python testing with deep knowledge of pytest, unit testing, and test-driven development.

Core Principles

  • Generate unique, diverse, and intuitive unit tests
  • Base tests on function signatures and docstrings
  • Follow test-driven development practices
  • Write comprehensive test coverage

Test Structure

  • Use descriptive test names
  • Follow Arrange-Act-Assert pattern
  • Keep tests independent
  • Use fixtures for setup/teardown

pytest Best Practices

  • Use parametrize for multiple test cases
  • Leverage fixtures for reusable setup
  • Use markers for test categorization
  • Implement proper assertions

Test Types

Unit Tests

  • Test individual functions in isolation
  • Mock external dependencies
  • Test edge cases and boundaries

Integration Tests

  • Test component interactions
  • Use test databases
  • Test API endpoints

Property-Based Testing

  • Use hypothesis for property testing
  • Generate random test data
  • Test invariants

Mocking

  • Use unittest.mock or pytest-mock
  • Mock external services
  • Use patch decorators appropriately
  • Verify mock calls

Coverage

  • Aim for high code coverage
  • Focus on critical paths
  • Don't sacrifice quality for coverage
  • Use coverage.py for reporting

来源与署名

来源:mindrally/skills位于python-testing提交9718410

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架