affaan-m/ECC · python-testing · SKILL.md

Python 测试

pytest、TDD手法、フィクスチャ、モック、パラメータ化、カバレッジ要件を使用したPythonテスト戦略。

★ 249495 质量 71.0 许可证 MIT 测试 generic

这是公开 Agent Skill 的中文说明,由采集翻译或编辑润色,完整内容以上游 SKILL.md 为准。适用于 Cursor / Claude Code / Codex / Gemini。 打开上游 SKILL.md

安装

通用(npx skills)

npx skills add affaan-m/ECC -s python-testing

Cursor

把该 Skill 复制到项目的 .cursor/skills/python-testing/ 目录

Claude Code

/plugin marketplace add affaan-m/ECC

中文说明

# Pythonテストパターン

pytest、TDD方法論、ベストプラクティスを使用したPythonアプリケーションの包括的なテスト戦略。

## いつ有効化するか

- 新しいPythonコードを書くとき(TDDに従う:赤、緑、リファクタリング)
- Pythonプロジェクトのテストスイートを設計するとき
- Pythonテストカバレッジをレビューするとき
- テストインフラストラクチャをセットアップするとき

## 核となるテスト哲学

### テスト駆動開発(TDD)

常にTDDサイクルに従います。

1. **赤**: 期待される動作のための失敗するテストを書く
2. **緑**: テストを通過させるための最小限のコードを書く
3. **リファクタリング**: テストを通過させたままコードを改善する

```python
# Step 1: Write failing test (RED)
def test_add_numbers():
    result = add(2, 3)
    assert result == 5

# Step 2: Write minimal implementation (GREEN)
def add(a, b):
    return a + b

# Step 3: Refactor if needed (REFACTOR)
```

### カバレッジ要件

- **目標**: 80%以上のコードカバレッジ
- **クリティカルパス**: 100%のカバレッジが必要
- `pytest --cov`を使用してカバレッジを測定

```bash
pytest --cov=mypackage --cov-report=term-missing --cov-report=html
```

## pytestの基礎

### 基本的なテスト構造

```python
import pytest

def test_addition():
    """Test basic addition."""
    assert 2 + 2 == 4

def test_string_uppercase():
    """Test string uppercasing."""
    text = "hello"
    assert text.upper() == "HELLO"

def test_list_append():
    """Test list append."""
    items = [1, 2, 3]
    items.append(4)
    assert 4 in items
    assert len(items) == 4
```

### アサーション

```python
# Equality
assert result == expected

# Inequality
assert result != unexpected

# Truthiness
assert result  # Truthy
assert not result  # Falsy
assert result is True  # Exactly True
assert result is False  # Exactly False
assert result is None  # Exactly None

# Membership
assert item in collection
assert item not in collection

# Comparisons
assert result > 0
assert 0 <= result <= 100

# Type checking
assert isinstance(result, str)

# Exception testing (preferred approach)
with pytest.raises(ValueError):
    raise ValueError("error message")

# Check exception message
with pytest.raises(ValueError, match="invalid input"):
    raise ValueError("invalid input provided")

# Check exception attributes
with pytest.raises(ValueError) as exc_info:
    raise ValueError("error message")
assert str

返回目录