claudegoodies
Skill

django-tdd

From affaan-m

Django testing strategies with pytest-django, TDD methodology, factory_boy, mocking, coverage, and testing Django REST Framework APIs.

Provides Django TDD workflow: pytest-django config, factory_boy factories, fixtures, and DRF API test patterns.

Use it when

  • Setting up pytest/pytest-django config for a Django project
  • Writing factory_boy factories for models like User, Product, Category
  • Testing Django REST Framework views with APIClient fixtures
  • Following Red-Green-Refactor cycle for Django models/views

Skip it if

  • Not using Django or Django REST Framework
  • Already have your own pytest-django/factory_boy setup and conventions
  • Documentation and comments are written in Japanese

Facts

Repository
affaan-m/ECC
Status
Actively maintained
Last commit

Source preview

The instructions Claude Code reads when this skill runs.

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

pytest、factory_boy、Django REST Frameworkを使用したDjangoアプリケーションのテスト駆動開発。

## いつ有効化するか

- 新しいDjangoアプリケーションを書くとき
- Django REST Framework APIを実装するとき
- Djangoモデル、ビュー、シリアライザーをテストするとき
- Djangoプロジェクトのテストインフラを設定するとき

## DjangoのためのTDDワークフロー

### Red-Green-Refactorサイクル

```python
# ステップ1: RED - 失敗するテストを書く
def test_user_creation():
    user = User.objects.create_user(email='[email protected]', password='testpass123')
    assert user.email == '[email protected]'
    assert user.check_password('testpass123')
    assert not user.is_staff

# ステップ2: GREEN - テストを通す
# Userモデルまたはファクトリーを作成

# ステップ3: REFACTOR - テストをグリーンに保ちながら改善
```

## セットアップ

### pytest設定

```ini
# pytest.ini
[pytest]
DJANGO_SETTINGS_MODULE = config.settings.test
testpaths = tests
python_files = test_*.py
python_classes = Test*
python_functions = test_*
addopts =
    --reuse-db
    --nomigrations
    --cov=apps
    --cov-report=html
    --cov-report=term-missing
    --strict-markers
markers =
    slow: marks tests as slow
    integration: marks tests as integration tests
```

### テスト設定

```python
# config/settings/test.py
from .base import *

DEBUG = True
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': ':memory:',
    }
}

# マイグレーションを無効化して高速化
class DisableMigrations:
    def __contains__(self, item):
        return True

    def __getitem__(self, item):
        retur
View full source on GitHub →

Other skills