mcpField
P
python-testing-patterns

python-testing-patterns

github.com/wshobson测试与质量41.9k37.5k 次安装2 周前更新
代码仓库

关于 python-testing-patterns

面向 Python 开发者的测试模式指南,覆盖 pytest、夹具、模拟和参数化等内容。适用于从单元测试到异步、数据库及 CI/CD 测试的规划与排错。

主要功能

  1. 01单元、集成与端到端测试
  2. 02pytest 夹具与测试组织
  3. 03模拟与参数化测试
  4. 04异步及数据库测试指导
  5. 05覆盖率与 CI/CD 实践

适用场景

  1. 01为 Python 项目搭建测试套件
  2. 02编写 API 和服务的集成测试
  3. 03模拟外部依赖并排查失败测试
  4. 04在 CI/CD 中规划持续测试

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/wshobson/agents --skill python-testing-patterns

安装后在 Claude Code 中输入/python-testing-patterns确认已加载。

附带文件references/advanced-patterns.mdreferences/details.md

详细介绍

Python 测试模式

在 Python 中使用 pytest、fixture、mock、参数化和测试驱动开发实践来实施健壮测试策略的全面指南。

何时使用此技能

  • 为 Python 代码编写单元测试
  • 搭建测试套件和测试基础设施
  • 实施测试驱动开发(TDD)
  • 为 API 和服务创建集成测试
  • Mock 外部依赖和服务
  • 测试异步代码和并发操作
  • 在 CI/CD 中设置持续测试
  • 实施基于属性的测试
  • 测试数据库操作
  • 调试失败的测试

核心概念

1. 测试类型
  • 单元测试:隔离测试单个函数/类
  • 集成测试:测试组件之间的交互
  • 功能测试:端到端测试完整功能
  • 性能测试:衡量速度和资源使用情况
2. 测试结构(AAA 模式)
  • Arrange:设置测试数据和前置条件
  • Act:执行被测代码
  • Assert:验证结果
3. 测试覆盖率
  • 衡量测试覆盖了哪些代码
  • 识别未测试的代码路径
  • 追求有意义的覆盖率,而不仅仅是高百分比
4. 测试隔离
  • 测试应当相互独立
  • 测试之间不共享状态
  • 每个测试都应自行清理

快速开始

# test_example.py
def add(a, b):
    return a + b

def test_add():
    """Basic test example."""
    result = add(2, 3)
    assert result == 5

def test_add_negative():
    """Test with negative numbers."""
    assert add(-1, 1) == 0

# Run with: pytest test_example.py

详细模式与示例

详细的模式文档位于 references/details.md。当上述导航层级不够用时,请阅读该文件。

测试最佳实践

测试组织
# tests/
#   __init__.py
#   conftest.py           # Shared fixtures
#   test_unit/            # Unit tests
#     test_models.py
#     test_utils.py
#   test_integration/     # Integration tests
#     test_api.py
#     test_database.py
#   test_e2e/            # End-to-end tests
#     test_workflows.py
测试命名约定

一种常见模式:test_<unit>_<scenario>_<expected_outcome>。可根据团队偏好进行调整。

# Pattern: test_<unit>_<scenario>_<expected>
def test_create_user_with_valid_data_returns_user():
    ...

def test_create_user_with_duplicate_email_raises_conflict():
    ...

def test_get_user_with_unknown_id_returns_none():
    ...

# Good test names - clear and descriptive
def test_user_creation_with_valid_data():
    """Clear name describes what is being tested."""
    pass

def test_login_fails_with_invalid_password():
    """Name describes expected behavior."""
    pass

def test_api_returns_404_for_missing_resource():
    """Specific about inputs and expected outcomes."""
    pass

# Bad test names - avoid these
def test_1():  # Not descriptive
    pass

def test_user():  # Too vague
    pass

def test_function():  # Doesn't explain what's tested
    pass
测试重试行为

使用 mock 副作用验证重试逻辑是否正确工作。

from unittest.mock import Mock

def test_retries_on_transient_error():
    """Test that service retries on transient failures."""
    client = Mock()
    # Fail twice, then succeed
    client.request.side_effect = [
        ConnectionError("Failed"),
        ConnectionError("Failed"),
        {"status": "ok"},
    ]

    service = ServiceWithRetry(client, max_retries=3)
    result = service.fetch()

    assert result == {"status": "ok"}
    assert client.request.call_count == 3

def test_gives_up_after_max_retries():
    """Test that service stops retrying after max attempts."""
    client = Mock()
    client.request.side_effect = ConnectionError("Failed")

    service = ServiceWithRetry(client, max_retries=3)

    with pytest.raises(ConnectionError):
        service.fetch()

    assert client.request.call_count == 3

def test_does_not_retry_on_permanent_error():
    """Test that permanent errors are not retried."""
    client = Mock()
    client.request.side_effect = ValueError("Invalid input")

    service = ServiceWithRetry(client, max_retries=3)

    with pytest.raises(ValueError):
        service.fetch()

    # Only called once - no retry for ValueError
    assert client.request.call_count == 1
使用 Freezegun Mock 时间

使用 freezegun 在测试中控制时间,以实现可预测的时间相关行为。

from freezegun import freeze_time
from datetime import datetime, timedelta

@freeze_time("2026-01-15 10:00:00")
def test_token_expiry():
    """Test token expires at correct time."""
    token = create_token(expires_in_seconds=3600)
    assert token.expires_at == datetime(2026, 1, 15, 11, 0, 0)

@freeze_time("2026-01-15 10:00:00")
def test_is_expired_returns_false_before_expiry():
    """Test token is not expired when within validity period."""
    token = create_token(expires_in_seconds=3600)
    assert not token.is_expired()

@freeze_time("2026-01-15 12:00:00")
def test_is_expired_returns_true_after_expiry():
    """Test token is expired after validity period."""
    token = Token(expires_at=datetime(2026, 1, 15, 11, 30, 0))
    assert token.is_expired()

def test_with_time_travel():
    """Test behavior across time using freeze_time context."""
    with freeze_time("2026-01-01") as frozen_time:
        item = create_item()
        assert item.created_at == datetime(2026, 1, 1)

        # Move forward in time
        frozen_time.move_to("2026-01-15")
        assert item.age_days == 14
测试标记
# test_markers.py
import pytest

@pytest.mark.slow
def test_slow_operation():
    """Mark slow tests."""
    import time
    time.sleep(2)


@pytest.mark.integration
def test_database_integration():
    """Mark integration tests."""
    pass


@pytest.mark.skip(reason="Feature not implemented yet")
def test_future_feature():
    """Skip tests temporarily."""
    pass


@pytest.mark.skipif(os.name == "nt", reason="Unix only test")
def test_unix_specific():
    """Conditional skip."""
    pass


@pytest.mark.xfail(reason="Known bug #123")
def test_known_bug():
    """Mark expected failures."""
    assert False


# Run with:
# pytest -m slow          # Run only slow tests
# pytest -m "not slow"    # Skip slow tests
# pytest -m integration   # Run integration tests
覆盖率报告
# Install coverage
pip install pytest-cov

# Run tests with coverage
pytest --cov=myapp tests/

# Generate HTML report
pytest --cov=myapp --cov-report=html tests/

# Fail if coverage below threshold
pytest --cov=myapp --cov-fail-under=80 tests/

# Show missing lines
pytest --cov=myapp --cov-report=term-missing tests/

有关高级模式(异步测试、monkeypatching、基于属性的测试、数据库测试、CI/CD 集成和配置),请参阅 references/advanced-patterns.md

评论

支持 Markdown