skillfed

python-testing

Learn to build robust Python tests using pytest with test-driven development principles. This skill covers fixtures for setup and teardown, parametrization for running tests across multiple inputs, mocking to isolate dependencies, and achieving meaningful code coverage targets.

Python Testing teaches pytest-based testing strategies including TDD, fixtures, mocking, and parametrization.

AI-generated summary based on this skill's SKILL.md

1,767 298 MIT updated by xu-xiang

Install

xu-xiang/everything-claude-code-zh/python-testing · repository language: JavaScript

git clone https://github.com/xu-xiang/everything-claude-code-zh
cp -r everything-claude-code-zh/skills/python-testing ~/.claude/skills/python-testing
npx skillfed install xu-xiang/everything-claude-code-zh/python-testing

Frequently asked questions

AI-generated answers based on this skill's SKILL.md and metadata

What is the pytest testing framework and how does python-testing teach it?

python-testing covers pytest, the leading Python testing framework. The skill teaches you to write comprehensive tests using pytest's powerful features including assertions, fixtures for setup/teardown, parametrization for testing multiple inputs, and mocking to isolate dependencies. You'll learn test-driven development workflows where you write tests before implementation, ensuring robust code from the start.

How do pytest fixtures setup and teardown work in python-testing?

python-testing explains fixture scopes and lifecycle management in pytest. Fixtures provide reusable setup and teardown logic for your tests. The skill covers conftest.py for sharing fixtures across test files, different scope levels (function, class, module, session), and how to structure fixtures for maintainability. This eliminates code duplication and keeps tests clean and focused.

Can python-testing help me write parametrized tests across multiple test cases?

Yes. python-testing teaches parametrization patterns that let you run the same test logic with different inputs efficiently. Instead of writing duplicate test functions, you use pytest's parametrize decorator to specify multiple input sets. This reduces code duplication, improves maintainability, and makes it easy to test edge cases and various scenarios systematically.

How does python-testing cover mocking and patching in Python?

python-testing teaches mocking and patching techniques to isolate units under test from external dependencies. You'll learn to mock objects, patch functions, and control their behavior during tests. This allows you to test components independently without relying on databases, APIs, or other external systems, making tests faster and more reliable.

How can python-testing help me measure and improve code coverage?

python-testing covers code coverage measurement using pytest-cov to track which lines your tests execute. The skill guides you toward meaningful coverage targets of 80%+ and teaches best practices for improving coverage. You'll learn to identify untested code paths, write tests for edge cases, and structure test suites to maximize coverage while maintaining test quality and relevance.

What test-driven development workflow does python-testing implement?

python-testing teaches TDD principles where you write tests before implementation. This workflow improves design, catches bugs early, and ensures requirements are met. The skill covers organizing and structuring test suites for maintainability, using pytest markers for test selection, and integrating testing into your Python project workflow from the start.

SKILL.md

rendered from the published skill — quoted content, verbatim

Python 测试模式(Python Testing Patterns)

使用 pytest、测试驱动开发(TDD)方法论和最佳实践的 Python 应用程序综合测试策略。

何时激活

  • 编写新的 Python 代码时(遵循 TDD:红、绿、重构)
  • 为 Python 项目设计测试套件时
  • 审查 Python 测试覆盖率时
  • 搭建测试基础设施时

核心测试哲学

测试驱动开发 (TDD)

始终遵循 TDD 循环:

  1. 红(RED):为期望的行为编写一个失败的测试
  2. 绿(GREEN):编写最少的代码使测试通过
  3. 重构(REFACTOR):在保持测试通过的同时改进代码
# 步骤 1:编写失败的测试 (红)
def test_add_numbers():
    result = add(2, 3)
    assert result == 5

# 步骤 2:编写最简实现 (绿)
def add(a, b):
    return a + b

# 步骤 3:如果需要则进行重构 (重构)
覆盖率要求
  • 目标:80% 以上的代码覆盖率
  • 关键路径:要求 100% 覆盖率
  • 使用 pytest --cov 来衡量覆盖率
pytest --cov=mypackage --cov-report=term-missing --cov-report=html

pytest 基础

基本测试结构
import pytest

def test_addition():
    """测试基本的加法。"""
    assert 2 + 2 == 4

def test_string_uppercase():
    """测试字符串转大写。"""
    text = "hello"
    assert text.upper() == "HELLO"

def test_list_append():
    """测试列表追加。"""
    items = [1, 2, 3]
    items.append(4)
    assert 4 in items
    assert len(items) == 4
断言(Assertions)

```python

(truncated - see the full file via the links below)

Read as markdown · JSON record · Browse the source repository

File tree — 1 file
skills/python-testing/SKILL.md

Related skills

Tags

test-automation quality-assurance code-validation dependency-isolation test-organization coverage-metrics behavior-verification regression-prevention