Write table-driven unit tests in Claude Code
Group normal, missing, and invalid inputs into one case table, then arrange, act, and assert independently.
中文Install in Claude Code
A Claude Code workspace with Jest, pytest, or Go testing already set up. The example uses a pure label function.
- Review `.claude/skills/unit-test-patterns/SKILL.md` and its requirements at the pinned GitHub commit.
- Create `.claude/skills/unit-test-patterns/` in your project and save the reviewed file as `.claude/skills/unit-test-patterns/SKILL.md`.
- Start Claude Code from that project's root, then select `/unit-test-patterns` in the slash menu.
Example task
A rehearsal-label helper uses the case table `{cases:[[' Hall ','Hall'],['','Untitled'],[null,'Untitled']]}`. Trim surrounding spaces and return the default for an empty title or null. The cases share no mutable state.What to check
The expected results are `['Hall','Untitled','Untitled']`. Arrange one table row per test, call the helper, and assert its output. Name scenarios so failures are easy to trace.
Original SKILL.md on GitHub · Updated
Frequently asked question
How can I avoid copying tests for similar inputs?
Use one data table for names, inputs, and expected results. Reuse the test body for normal, boundary, and erroneous inputs and assert public outcomes.