Skip to content
Back to skills

Generate Test Scaffold

ASecurity

Generate test scaffolds from Protean domain definitions. Analyzes existing aggregates, commands, events, handlers, value objects, and entities to produce focused pytest test stubs for user-written business logic, such as aggregate behavior, state transitions, invariants, event raising, handler orchestration, and end-to-end flows. Follows Protean's testing pyramid (domain unit tests as majority). Does NOT generate tests for framework-guaranteed behavior (VO immutability, field validation, regi...

  • 46 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added September 20, 2026
testingpythongoreactfastapitestingapidatabase

Works with

  • api

Security analysis

A100/100

Pro scans all 7 files and shows the line behind each finding

Scanned November 9, 2026

npx -y skills add proteanhq/protean --skill generate-test-scaffold --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Generate Test Scaffold?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Generate Test Scaffold
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/proteanhq-generate-test-scaffold/badge)](https://www.skillsdirectory.com/skills/proteanhq-generate-test-scaffold)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: generate-test-scaffold
description: Generate test scaffolds from Protean domain definitions. Analyzes existing aggregates, commands, events, handlers, value objects, and entities to produce focused pytest test stubs for user-written business logic, such as aggregate behavior, state transitions, invariants, event raising, handler orchestration, and end-to-end flows. Follows Protean's testing pyramid (domain unit tests as majority). Does NOT generate tests for framework-guaranteed behavior (VO immutability, field validation, registration). Use when the user asks to "generate tests", "scaffold tests", "create test stubs", "add tests for my domain", "write tests for aggregate", "generate test file", "add test coverage", or when they have domain code that needs test coverage.
license: Apache-2.0
compatibility: Requires Python 3.11+, protean framework
metadata:
  author: proteanhq
  version: "0.1"
  category: workflow
  composes: []
---

# Generate Test Scaffold

Generate comprehensive pytest tests for existing Protean domain definitions, focusing on **user-written business logic**.

## The testing pyramid

Domain unit tests are the **majority** of your test suite. They test the most important code (business rules) with the least overhead (no infrastructure).

## What to test vs. what to skip

### Test (user-written business logic)

1. **Aggregate factory methods**: correct state after creation, correct events raised via `raise_()`
2. **Aggregate state-change methods**: before/after state assertions, status transitions
3. **User-defined invariants**: `@invariant.pre` and `@invariant.post` enforcement
4. **Business rules**: methods that reject invalid operations (e.g., "can't cancel a draft order")
5. **Event data correctness**: events raised by `raise_()` carry the right field values
6. **Value object custom operations**: user-defined methods like `Money.add()`, not VO construction/equality
7. **Entity management**: add/remove entities through aggregate methods
8. **Handler orchestration**: `domain.process()` creates/updates the right aggregate state
9. **Event handler side effects**: cross-aggregate state changes triggered by events
10. **End-to-end flows**: Full lifecycle from command to final state

### Skip (framework guarantees)

- VO construction, equality, immutability: guaranteed by `@domain.value_object`
- Command/event immutability: guaranteed by decorators
- Field-level validation (`required`, `max_length`, `min_value`, `choices`): Protean Layer 1
- Element registration in `domain.registry`: guaranteed if decorator is used
- `meta_.part_of` associations, `meta_.stream_category`: framework wiring
- Command `.payload` dict access: framework feature

### Avoid mocks: use real domain elements

Protean's in-memory infrastructure (repositories, and event and command processing in `"sync"` mode) makes mocks unnecessary in almost all cases. Construct real aggregates, value objects, entities, commands, and events directly in tests. Use `domain.process()` and `domain.repository_for()` for integration tests. Mocks should only be used when testing interactions with truly external systems (e.g., third-party HTTP APIs) that cannot be replaced by Protean's built-in test infrastructure.

## Information to gather

Before generating tests, identify:

- [ ] Aggregate factory methods (classmethods that create instances)
- [ ] Aggregate state-change methods (instance methods that mutate state)
- [ ] Events raised by each method via `raise_()`
- [ ] User-defined invariants (`@invariant.pre`, `@invariant.post`)
- [ ] Business rules (conditionals that raise an exception), and the exact exception each one raises
- [ ] Command handlers and what commands they handle
- [ ] Event handlers and what events they react to
- [ ] Value object custom operations (user-defined methods)
- [ ] Cross-aggregate relationships (event handlers listening to other streams)

## Process

### Step 1: Analyze domain definitions

Read the domain code and fill in the checklist above.

### Step 2: Generate aggregate unit tests

For each aggregate method, generate tests for:

```python
from decimal import Decimal as D


class TestOrderBehavior:
    def test_place_sets_status_to_placed(self):
        """place() transitions status from draft to placed."""
        order = Order.create(customer_id="cust-1")
        order.add_item(product_id="p-1", quantity=2, unit_price=D("10.00"))
        order.place()
        assert order.status == "placed"

    def test_place_raises_order_placed_event(self):
        """place() raises OrderPlaced with correct data."""
        order = Order.create(customer_id="cust-1")
        order.add_item(product_id="p-1", quantity=2, unit_price=D("10.00"))
        order.place()
        assert len(order._events) == 1
        event = order._events[0]
        assert isinstance(event, OrderPlaced)
        assert event.order_id == order.id

    def test_place_fails_for_non_draft_order(self):
        """place() rejects orders not in draft status."""
        order = Order.create(customer_id="cust-1")
        order.add_item(product_id="p-1", quantity=1, unit_price=D("5.00"))
        order.place()
        # place() raises ValueError for an order that is not a draft.
        with pytest.raises(ValueError, match="Cannot place order"):
            order.place()
```

**Pattern**: `pytest.raises` names the exception the tested method raises. Read the method to find it: here `place()` raises `ValueError`, and an invariant raises `ValidationError`. Add `match=` so the test fails when a different check raises the same type.

**Pattern**: Use `_events.clear()` to isolate event assertions when testing a method after a factory that also raises events.

### Step 3: Generate invariant tests

```python
class TestOrderInvariants:
    def test_cannot_place_empty_order(self):
        """Invariant: placed orders must have items."""
        order = Order.create(customer_id="cust-1")
        with pytest.raises(ValidationError):
            order.place()  # No items added
```

### Step 4: Generate handler orchestration tests

The root `conftest.py` from Step 7 sets `command_processing = "sync"`, so `domain.process()` runs the handler before it returns and returns the handler's result.

```python
class TestHandlerProcessing:
    def test_register_user_creates_and_persists(self):
        """domain.process(RegisterUser) creates a registered User."""
        result = domain.process(RegisterUser(email="a@test.com", name="Alice"))
        user = domain.repository_for(User).get(result)
        assert user.status == "registered"
        assert user.email == "a@test.com"
```

### Step 5: Generate event handler side-effect tests

The root `conftest.py` sets `event_processing = "sync"`, so the event handler runs when the source aggregate is persisted. Without it the handler never runs in the test and the assertions fail.

```python
class TestCrossAggregateFlow:
    def test_order_placed_reserves_inventory(self):
        """OrderPlaced event triggers stock reservation."""
        inventory = Inventory(product_id="p-1", available=100, reserved=0)
        domain.repository_for(Inventory).add(inventory)

        order = Order.place(customer_id="c-1", product_id="p-1", quantity=5)
        domain.repository_for(Order).add(order)

        updated = domain.repository_for(Inventory).get(inventory.id)
        assert updated.available == 95
        assert updated.reserved == 5
```

### Step 6: Generate end-to-end lifecycle tests

Test full sequences: create → modify → verify final state + side effects.

### Step 7: Set up test infrastructure

Use the pytest integration in `protean.integrations.pytest`. Do not build a test domain by hand. A project made by `protean new` already has this setup in `tests/<package>/conftest.py`, so keep that file and skip this step. Otherwise, put one `conftest.py` at the root of the test suite:

```python
# fragment
# tests/conftest.py
import pytest

from protean.integrations.pytest import DomainFixture

from myapp import domain


@pytest.fixture(scope="session")
def app_fixture():
    domain.config["event_processing"] = "sync"
    domain.config["command_processing"] = "sync"

    fixture = DomainFixture(domain)
    fixture.setup()
    yield fixture
    fixture.teardown()


@pytest.fixture(autouse=True)
def _ctx(app_fixture):
    with app_fixture.domain_context():
        yield
```

- `DomainFixture.setup()` calls `domain.init()` and creates the database schema. `teardown()` drops it.
- `domain_context()` activates the domain for one test, then resets every provider, broker, and event store, so each test starts with empty repositories.
- Set both processing modes to `"sync"` before `setup()`. A bare `Domain()` processes events and commands asynchronously, so a handler side effect would not happen inside the test. A project made by `protean new` already sets both to `"sync"` in `domain.toml`. Setting them in the conftest as well makes a copied test pass either way.

Test files then import the elements they test and need no setup of their own:

```python
# fragment
import pytest

from protean.exceptions import ValidationError

from myapp.ordering import Order, OrderPlaced
```

## Test class organization

| Test class name | What it covers |
|----------------|---------------|
| `Test{Aggregate}Behavior` | Factory methods, state transitions, entity management |
| `Test{Aggregate}BusinessRules` | Invariants, rejected operations, error cases |
| `Test{Aggregate}Events` | Events raised with correct data |
| `TestHandlerProcessing` | domain.process() flows, persistence verification |
| `TestCrossAggregateFlow` | Event handler side effects across aggregates |
| `TestLifecycle` | Full end-to-end sequences |

## Common mistakes

- **Not creating prerequisite state**: For event handler tests, create the target aggregate in the repository before the source event fires.

## Complete examples

Each asset is a pytest module: the domain first, then its tests below the `# --- Tests ---` line. Each asset defines its own `domain = Domain()`, so do not copy a whole asset into a project's test suite. The Step 7 conftest sets up the project's domain, not the asset's, and the asset's tests would fail.

To use an asset in a project, copy only the part below `# --- Tests ---` into `tests/test_<name>.py`. Import the elements it uses from the project, for example `from myapp import domain, Order, OrderPlaced`. The Step 7 conftest then sets up the domain.

- [Scaffold conftest](assets/conftest.py): runs the assets exactly as written, in a folder of their own. Each test module there must define `domain`, so do not use this conftest as a project's root conftest. Use the Step 7 conftest there.
- [Aggregate unit tests](assets/scaffold_aggregate_unit.py): Order with entities, value objects, invariants
- [Command flow tests](assets/scaffold_command_flow.py): User registration with handler and API (the API test needs `fastapi` and `httpx`)
- [Event-driven flow tests](assets/scaffold_event_driven_flow.py): Cross-aggregate Order → Inventory sync, where an event handler in Order's cluster issues a command to Inventory, and a repeated command reserves stock once

## Detailed references

- [Aggregate test patterns](references/aggregate-tests.md): Factory methods, state transitions, invariants, entity management
- [Handler test patterns](references/handler-tests.md): Command/event handler orchestration, API endpoints
- [Integration test patterns](references/integration-tests.md): Cross-aggregate flows, lifecycle tests

## Related skills

- [aggregate](../aggregate/SKILL.md): Aggregate definition patterns
- [command](../command/SKILL.md): Command definition
- [event](../event/SKILL.md): Event definition
- [command-handler](../command-handler/SKILL.md): Handler patterns
- [event-handler](../event-handler/SKILL.md): Event handler patterns
- [value-object](../value-object/SKILL.md): Value object patterns
- [add-use-case](../add-use-case/SKILL.md): Vertical slice patterns

## Verify your work

- [Verify with check](../../references/verify-with-check.md): run `check` and resolve what it reports

Files in this skill

  • SKILL.md10.9 KB
  • assets/scaffold_aggregate_unit.py5 KB
  • assets/scaffold_command_flow.py3.5 KB
  • assets/scaffold_event_driven_flow.py3.8 KB
  • references/aggregate-tests.md5.1 KB
  • references/handler-tests.md4.8 KB
  • references/integration-tests.md4.8 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in testing →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.