pytest-domain-model-testing
How to test domain models effectively: value object testing (immutability, validation), entity testing (identity, business logic), domain exception testing, aggregate testing, high coverage patterns (95%+), and testing invariants and constraints. Use when: Testing domain layer code, validating value objects, testing entities with business logic, ensuring domain invariants, or achieving 95%+ coverage on domain models.
What this skill does
# Pytest Domain Model Testing
## Purpose
The domain layer contains business logic and should have near-perfect coverage (95-100%). Domain models have zero external dependencies, making them easy to test thoroughly. This skill focuses on testing pure domain logic effectively.
## When to Use This Skill
Use when testing domain models with "test value objects", "test entities", "test domain logic", or "achieve 95% domain coverage".
Do NOT use for application layer (use `pytest-application-layer-testing`), adapters (use `pytest-adapter-integration-testing`), or mocking (domain tests should use real objects).
## Quick Start
Test domain models directly without mocks:
```python
from app.extraction.domain.value_objects import ProductTitle
import pytest
def test_product_title_validation() -> None:
"""Test value object validation."""
# ✅ Valid
title = ProductTitle("Awesome Laptop")
assert title.value == "Awesome Laptop"
# ❌ Invalid: too short
with pytest.raises(ValueError, match="must be 1-500"):
ProductTitle("")
# ❌ Invalid: too long
with pytest.raises(ValueError, match="must be 1-500"):
ProductTitle("x" * 501)
```
## Instructions
### Step 1: Test Value Objects (Immutability & Validation)
```python
from __future__ import annotations
import pytest
from app.extraction.domain.value_objects import ProductTitle, Money, OrderId
class TestProductTitle:
"""Test ProductTitle value object."""
def test_valid_creation(self) -> None:
"""Test creating valid product title."""
title = ProductTitle("Laptop")
assert title.value == "Laptop"
def test_empty_title_raises_error(self) -> None:
"""Test that empty title is invalid."""
with pytest.raises(ValueError, match="must be 1-500 characters"):
ProductTitle("")
def test_too_long_title_raises_error(self) -> None:
"""Test that title over 500 chars is invalid."""
with pytest.raises(ValueError, match="must be 1-500 characters"):
ProductTitle("x" * 501)
def test_boundary_exactly_500_chars(self) -> None:
"""Test boundary: exactly 500 characters is valid."""
title = ProductTitle("x" * 500)
assert len(title.value) == 500
def test_immutability(self) -> None:
"""Test value object is immutable (frozen dataclass)."""
title = ProductTitle("Laptop")
with pytest.raises(AttributeError):
title.value = "Mouse" # Should fail
def test_unicode_characters(self) -> None:
"""Test title with unicode works."""
title = ProductTitle("Café ☕ Deluxe")
assert title.value == "Café ☕ Deluxe"
def test_whitespace_handling(self) -> None:
"""Test title with whitespace."""
title = ProductTitle(" Laptop ")
assert title.value == " Laptop " # Preserves whitespace
def test_equality(self) -> None:
"""Test two titles with same value are equal."""
title1 = ProductTitle("Laptop")
title2 = ProductTitle("Laptop")
assert title1 == title2
def test_inequality(self) -> None:
"""Test two titles with different values are not equal."""
title1 = ProductTitle("Laptop")
title2 = ProductTitle("Mouse")
assert title1 != title2
def test_hashable(self) -> None:
"""Test value object can be hashed (for sets/dicts)."""
title1 = ProductTitle("Laptop")
title2 = ProductTitle("Laptop")
title3 = ProductTitle("Mouse")
titles_set = {title1, title2, title3}
assert len(titles_set) == 2 # title1 and title2 are same
def test_string_representation(self) -> None:
"""Test __str__ returns value."""
title = ProductTitle("Laptop")
assert str(title) == "Laptop"
```
### Step 2: Test Entities (Identity & Business Logic)
```python
from __future__ import annotations
from datetime import datetime
import pytest
from app.extraction.domain.entities import Order, LineItem
from app.extraction.domain.value_objects import OrderId, ProductId, ProductTitle, Money
class TestOrderEntity:
"""Test Order aggregate."""
def test_valid_order_creation(self) -> None:
"""Test creating valid order."""
order = Order(
order_id=OrderId("123"),
created_at=datetime.now(),
customer_name="John",
line_items=[
LineItem(
product_id=ProductId("prod_1"),
product_title=ProductTitle("Laptop"),
quantity=1,
price=Money.from_float(999.99),
)
],
total_price=Money.from_float(999.99),
)
assert order.order_id.value == "123"
assert order.customer_name == "John"
def test_empty_line_items_invalid(self) -> None:
"""Test order must have at least one line item."""
with pytest.raises(ValueError, match="must have at least one line item"):
Order(
order_id=OrderId("123"),
created_at=datetime.now(),
customer_name="John",
line_items=[], # Invalid!
total_price=Money.from_float(0.0),
)
def test_total_mismatch_invalid(self) -> None:
"""Test order total must match sum of line items."""
with pytest.raises(ValueError, match="total mismatch"):
Order(
order_id=OrderId("123"),
created_at=datetime.now(),
customer_name="John",
line_items=[
LineItem(
product_id=ProductId("prod_1"),
product_title=ProductTitle("Laptop"),
quantity=1,
price=Money.from_float(999.99),
)
],
total_price=Money.from_float(500.00), # Wrong total!
)
def test_negative_quantity_invalid(self) -> None:
"""Test line item quantity must be positive."""
with pytest.raises(ValueError, match="quantity must be positive"):
LineItem(
product_id=ProductId("prod_1"),
product_title=ProductTitle("Laptop"),
quantity=-5, # Invalid!
price=Money.from_float(999.99),
)
def test_get_product_titles_behavior(self) -> None:
"""Test domain behavior: extract product titles."""
order = Order(
order_id=OrderId("123"),
created_at=datetime.now(),
customer_name="John",
line_items=[
LineItem(
product_id=ProductId("prod_1"),
product_title=ProductTitle("Laptop"),
quantity=1,
price=Money.from_float(999.99),
),
LineItem(
product_id=ProductId("prod_2"),
product_title=ProductTitle("Mouse"),
quantity=2,
price=Money.from_float(29.99),
),
],
total_price=Money.from_float(1059.97),
)
titles = order.get_product_titles()
assert titles == ["Laptop", "Mouse"]
def test_order_identity_by_id(self) -> None:
"""Test orders with same ID but different data are considered same."""
order1 = Order(
order_id=OrderId("same_id"),
created_at=datetime.now(),
customer_name="John",
line_items=[LineItem(...)],
total_price=Money.from_float(100.0),
)
order2 = Order(
order_id=OrderId("same_id"),
created_at=datetime.now(),
customer_name="Jane", # Different name
line_items=[LineItem(...)],
total_price=Money.from_float(100.0),
)
# Entities with same ID are considered equal
assertRelated in Code Review
gstack
IncludedFast headless browser for QA testing and site dogfooding. Navigate pages, interact with elements, verify state, diff before/after, take annotated screenshots, test responsive layouts, forms, uploads, dialogs, and capture bug evidence. Use when asked to open or test a site, verify a deployment, dogfood a user flow, or file a bug with screenshots. (gstack)
startup-due-diligence
IncludedLegal due diligence review for seed-stage and Series A startups (US, Delaware C-Corp focus). Supports both investor and founder perspectives. Capabilities include: (1) Interactive document review and issue spotting; (2) Document request list generation; (3) Cap table and SAFE/convertible note analysis; (4) Red flag identification with severity ratings; (5) Diligence report generation. TRIGGERS: due diligence, DD, startup investment, cap table review, Series A, seed round, investor diligence, legal review startup, SAFE analysis, convertible note, 409A, founder vesting.
interview-master
IncludedThis skill should be used when the user asks to "generate interview questions", "prepare for interview", "optimize resume", "conduct mock interview", "analyze git commits for resume", "generate resume from code", "review my resume", or mentions interview preparation, career assistance, or extracting project experience from git history. Provides comprehensive interview and career development guidance for both job seekers and interviewers.
fix-issue
IncludedFixes GitHub issues using parallel analysis agents for root cause investigation, code exploration, and regression detection. Reads issue context from gh CLI, searches codebase and memory for related patterns, generates a fix with tests, and links the resolution back to the issue via PR. Includes prevention analysis to avoid recurrence. Use when debugging errors, resolving regressions, fixing bugs, or triaging issues.
sf-apex
IncludedGenerates and reviews Salesforce Apex code with 150-point scoring. TRIGGER when: user writes, reviews, or fixes Apex classes, triggers, test classes, batch/queueable/schedulable jobs, or touches .cls/.trigger files. DO NOT TRIGGER when: LWC JavaScript (use sf-lwc), Flow XML (use sf-flow), SOQL-only queries (use sf-soql), or non-Salesforce code.
swift-development
IncludedComprehensive Swift development for building, testing, and deploying iOS/macOS applications. Use when Claude needs to: (1) Build Swift packages or Xcode projects from command line, (2) Run tests with XCTest or Swift Testing framework, (3) Manage iOS simulators with simctl, (4) Handle code signing, provisioning profiles, and app distribution, (5) Format or lint Swift code with SwiftFormat/SwiftLint, (6) Work with Swift Package Manager (SPM), (7) Implement Swift 6 concurrency patterns (async/await, actors, Sendable), (8) Create SwiftUI views with MVVM architecture, (9) Set up Core Data or SwiftData persistence, or any other Swift/iOS/macOS development tasks.