# Craft Test Factory

> Crafting Test Factories. Beyond Laravel defaults.

- Skill: `attac-t/craft-test-factory` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add attac-t/craft-test-factory`
- Raw SKILL.md: https://api.skillmd.com/api/skills/attac-t/craft-test-factory/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: attac-t (https://skillmd.com/u/attac-t)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/attac-t/craft-test-factory

---


# Skill: Craft Test Factory

> "The power of a factory is not the complexity of the code, but rather one or two patterns properly applied."

## The Standard

1. **Static `new()` Constructor**: Entry point. Returns fresh instance.
2. **Immutability via Clone**: State methods return cloned instance, never mutate.
3. **Auto-Increment Uniques**: Static counter for unique fields like invoice numbers.
4. **Beyond Models**: Create DTOs, events, requests—anything tests need.

## The Anti-Patterns

| ❌ Don't                | ✅ Do                        | Why                        |
|------------------------|-----------------------------|-----------------------------|
| Mutable state          | Clone on every modifier     | Prevents test pollution     |
| Random unique values   | Static incrementing counter | Deterministic, debuggable   |
| Laravel factory only   | Custom factories too        | DTOs need factories too     |
| Inline test setup      | Named factory states        | Self-documenting tests      |

## Real-World Examples

See [examples.md](examples.md).

