ホーム
  • キャンバス
  • 画像
  • 動画
  • 音声
  • MCP / CLI
  • モデル
  • スキル
  • ツール
料金プランログイン新規登録

作成

  • AI 動画
  • AI 画像
  • 画像から動画
  • テキストから動画
  • AI 動画編集
  • 画像アップスケール
  • 画像編集

画像作成・編集

  • 画像を作成

画像モデル

  • GPT Image 2.6 Sunburst
  • GPT Image 2.6 Flare
  • GPT Image 2.5 Sunburst
  • GPT Image 2.5 Flare
  • Nano Banana 2.1

動画作成・編集

  • 動画を作成
  • エフェクト
  • 画像から動画生成
  • テキストから動画生成
  • 動画を編集
  • 動画アップスケール
  • ミックスメディア
  • 顔の入れ替え

動画モデル

  • Seedance 2.5
  • Kling 4.0
  • Kling 4.0 Flash
  • Gemini Omni Flash 1.2
  • FLUX.3 Video

オーディオ

  • テキスト読み上げ
  • ボイスチェンジ
  • 翻訳

オーディオモデル

  • Seed Audio 1.0
  • Eleven v4
  • Eleven v4 Turbo
  • Eleven v3
  • Qwen Audio 3.0

スタジオ

  • マーケティングスタジオ
  • リップシンク
  • キャンバス

プラットフォーム

  • MCP
  • CLI
  • モデル
  • モデルプロバイダー
  • スキル
  • ツール
  • ツールプロバイダー

会社情報

  • エージェントインストラクション
  • ヘルプセンター
  • 利用規約
  • プライバシー
  • 脆弱性を報告

開発者

  • ドキュメント
  • API リファレンス
  • 開発者プラットフォーム
  • AI サイトマップ
© 2026 ドットメディア株式会社 すべての権利を留保します。
すべてのスキルランキングマイリスト
業務領域
リポジトリ
スキル開発者
作業
成果物
連携サービス
呼び出し方
  1. ホーム
  2. スキル
  1. ホーム
  2. スキル
  3. SKILL.md
ファイル
wshobson
agents
.agents
.claude-plugin
.cursor
.cursor-plugin
.github
docs
evals
plugins
accessibility-compliance
agent-orchestration
agent-teams
api-scaffolding
api-testing-observability
application-performance
arm-cortex-microcontrollers
avoid-ai-writing
backend-api-security
backend-development
before-you-build
block-no-verify
blockchain-web3
brand-landingpage
business-analytics
c4-architecture
cicd-automation
cloud-infrastructure
code-documentation
code-refactoring
codebase-cleanup
comprehensive-review
conductor
content-marketing
context-management
customer-sales-automation
data-engineering
data-validation-suite
database-cloud-optimization
database-design
database-migrations
debugging-toolkit
dependency-management
deployment-strategies
ブランチタグ
c96b5f6原文を開く
Wwshobson·2026/11/24
未翻訳
278 行(空行を除く 202 行) · 7.11 KB·原文
name: python-testing-patterns
description: Implement comprehensive testing strategies with pytest, fixtures, mocking, and test-driven development. Use when writing Python tests, setting up test suites, or implementing testing best practices.

Python Testing Patterns

Comprehensive guide to implementing robust testing strategies in Python using pytest, fixtures, mocking, parameterization, and test-driven development practices.

When to Use This Skill

  • Writing unit tests for Python code
  • Setting up test suites and test infrastructure
  • Implementing test-driven development (TDD)
  • Creating integration tests for APIs and services
  • Mocking external dependencies and services
  • Testing async code and concurrent operations
  • Setting up continuous testing in CI/CD
  • Implementing property-based testing
  • Testing database operations
  • Debugging failing tests

Core Concepts

1. Test Types

  • Unit Tests: Test individual functions/classes in isolation
  • Integration Tests: Test interaction between components
  • Functional Tests: Test complete features end-to-end
  • Performance Tests: Measure speed and resource usage

2. Test Structure (AAA Pattern)

  • Arrange: Set up test data and preconditions
  • Act: Execute the code under test
  • Assert: Verify the results

3. Test Coverage

  • Measure what code is exercised by tests
  • Identify untested code paths
  • Aim for meaningful coverage, not just high percentages

4. Test Isolation

  • Tests should be independent
  • No shared state between tests
  • Each test should clean up after itself

Quick Start

# 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

Detailed patterns and worked examples

Detailed pattern documentation lives in details.md. Read that file when the navigation tier above is insufficient.

Testing Best Practices

Test Organization

# 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 Naming Convention

A common pattern: test_<unit>_<scenario>_<expected_outcome>. Adapt to your team's preferences.

# 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

Testing Retry Behavior

Verify that retry logic works correctly using mock side effects.

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

Mocking Time with Freezegun

Use freezegun to control time in tests for predictable time-dependent behavior.

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

# 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

Coverage Reporting

# 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/

For advanced patterns (async testing, monkeypatching, property-based testing, database testing, CI/CD integration, and configuration), see advanced-patterns.md