python-fastapi-ddd-testing-skill

Guides unit testing for Python DDD + Onion Architecture apps (Domain Entities/Value Objects and UseCases) using pytest and repository mocks, based on the dddpy reference. Use when adding tests, choosing what to mock, or structuring test folders for a DDD FastAPI project.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "python-fastapi-ddd-testing-skill" with this command: npx skills add iktakahiro/python-fastapi-ddd-skill/iktakahiro-python-fastapi-ddd-skill-python-fastapi-ddd-testing-skill

Testing DDD Layers with pytest (Domain / UseCase)

This skill focuses on unit tests for the inner layers:

  • Domain: Value Objects + Entities (pure business rules)
  • UseCase: application workflows that orchestrate Domain + Repository interfaces

It intentionally avoids full HTTP/API tests unless explicitly requested.

Test strategy (recommended)

  1. Domain tests: no mocks, assert invariants and state transitions.
  2. UseCase tests: mock repository interfaces (Mock(spec=...)), assert:
    • correct repository method calls
    • correct domain exceptions raised
    • correct entity state transitions
  3. Infrastructure/Presentation: add integration tests separately (optional).

Folder structure

Mirror the Onion layers:

tests/
  domain/{aggregate}/...
  usecase/{aggregate}/...
  infrastructure/...   # optional integration tests
  presentation/...     # optional API tests

UseCase testing pattern (dddpy-based)

Use unittest.mock.Mock(spec=RepoInterface) so typos fail fast.

from unittest.mock import Mock
import pytest

@pytest.fixture
def todo_repository_mock():
    return Mock(spec=TodoRepository)

def test_create_todo_calls_save(todo_repository_mock):
    usecase = CreateTodoUseCaseImpl(todo_repository_mock)
    title = TodoTitle("Test")

    todo = usecase.execute(title=title)

    todo_repository_mock.save.assert_called_once_with(todo)

For more examples (Value Object validation, entity lifecycle tests, and exception cases), read references/TESTING.md.

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

python-fastapi-ddd-skill

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

python-fastapi-ddd-presentation-skill

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

commit

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

openclaw-version-monitor

监控 OpenClaw GitHub 版本更新,获取最新版本发布说明,翻译成中文, 并推送到 Telegram 和 Feishu。用于:(1) 定时检查版本更新 (2) 推送版本更新通知 (3) 生成中文版发布说明

Archived SourceRecently Updated
python-fastapi-ddd-testing-skill | V50.AI