Claude Code Testing Commands — 15 Slash Commands for Unit, E2E, and Coverage | AI Code Toolkit
15 tested commands · Vitest · Jest · Pytest · Playwright · RSpec

Testing Slash Commands

Slash commands for every job in the test lifecycle: generation, maintenance, coverage, and infrastructure. Framework-aware, style-matching, colocation-respecting — and refreshed within 48 hours of every release.

15
Commands
4
Job Categories
6+
Frameworks Supported
48h
Update SLA

Why test-writing is the highest-ROI use of slash commands

Ask any Claude Code user which slash commands they wrote first, and testing shows up in the top three — usually alongside /commit and /review. The reason is simple math: writing tests is repetitive prompt engineering. You want the same framework detected, the same style enforced, the same edge cases considered, the same file colocation applied — every single time. That's exactly the pattern a slash command is designed for.

The 15 testing commands on this page are the field-tested set that our editorial team uses (and that community contributors submit variations of). Each is production-ready, framework-aware, and refreshed within 48 hours of every meaningful Claude Code or test-runner release.

The lens: Test-writing commands split into four jobs — generation (new tests), maintenance (fixing what breaks), quality (coverage, mutation, boundary), and infrastructure (mocks, factories, runners). Get one strong command in each category and you've covered 90% of what teams actually do.

What a great testing command looks like

The best test-writing commands don't just say "write tests". They:

  • Detect the framework. Vitest, Jest, Pytest, RSpec, Mocha, Playwright — the command reads package.json, pyproject.toml, or config files to pick the right style.
  • Mirror existing patterns. If your team uses describe/it nested style, the new tests do too. If you prefer flat test.each, they follow that.
  • Colocate correctly. Component.test.tsx next to Component.tsx? A __tests__/ folder? Sibling _test.go? The command respects your convention.
  • Restrict tools appropriately. Read, Grep, Glob, and Write(tests/**|**/*.test.*). No arbitrary shell commands. No writing production code from a test command.
  • Force the right model. Unit tests? Sonnet is fine. E2E with complex flows? Opus earns its keep. Set model: in the frontmatter so cost matches value.

Anatomy of /test-unit

Here's the shape of a well-written unit-test command:

markdown .claude/commands/test-unit.md
--- allowed-tools: Read, Grep, Glob, Write(**/*.test.*), Write(**/*.spec.*) description: Write unit tests for the current file using the project's test framework model: sonnet argument-hint: [path/to/file] [optional focus] --- # Context - Package manifest: @package.json - Existing test example (for style): !`find . -name "*.test.*" -not -path "./node_modules/*" | head -1 | xargs cat 2>/dev/null | head -80` # Task Write unit tests for the file at $ARGUMENTS. Rules: 1. Detect the test framework from package.json (vitest, jest, mocha, playwright). 2. Match the existing style shown above (describe/it vs flat test.each, colocation vs __tests__). 3. Cover: happy path, edge cases, error branches, and one integration-adjacent case if relevant. 4. Mock external dependencies with the project's existing mocking approach. 5. Colocate: write tests next to the source file (Component.tsx → Component.test.tsx). 6. Do NOT modify the source file being tested.

Testing commands vs testing hooks vs testing subagents

All three shape Claude Code's testing behavior, and mature teams use them together:

  • Slash command (/test-unit) — on-demand test writing. Fires when you type it. Best for "add tests for this file I just wrote".
  • Hook (auto-test-related) — automatic test runs after every write. Fires whether you asked or not. Best for enforcement: you can't leave broken tests behind.
  • Subagent (test-coverage-analyst) — specialized delegated work in an isolated context. Best for "find the highest-value coverage gaps and write tests for them" — a long, exploratory task that would pollute your main conversation.

The recipe most teams converge on: /test-unit for daily test writing, a auto-test-related hook that runs affected tests on save, and a test-coverage-analyst subagent invoked weekly for gap hunting.

Complete Listing

All 15 testing commands, grouped by job

Every command is production-tested against real projects and refreshed on a rolling 90-day cycle. Click any name for the full Markdown, frontmatter, and usage notes.

Decision Framework

Which testing command should I use?

A quick decision framework for the situations teams hit most often.

For a specific file you just wrote
Use /test-unit. Point it at the file, let it detect your framework, and mirror your existing patterns. Fastest path from source to tested code.
For a critical user flow
Use /test-e2e. Force model: opus in the frontmatter — the extra reasoning pays off for complex E2E flows with waits, fixtures, and page objects.
For a test that keeps failing randomly
Use /fix-flaky-test. It reads the test, git-blame history, and any recent CI failures to diagnose whether the flakiness is async, order-dependent, or environmental.
For coverage gaps
Use /coverage-check for on-demand analysis, or delegate to the test-coverage-analyst subagent for a longer, isolated exploration.
For third-party API dependencies
Use /mock-external-apis to generate the mocks first, then use /test-unit or /test-integration. Mocks first, tests second.
For migrating test frameworks
Use /test-runner-migrate. Handles the big three transitions: Jest→Vitest (most common), Mocha→Vitest, and Python unittest→pytest.

🐛 Hit an error while using these?

Our sister site AI Error Hub covers Claude Code errors, MCP connection failures, and stack traces — cross-referenced with everything on this site.

Visit AI Error Hub →
FAQ

Frequently asked questions

The questions developers ask most about testing.

/test-unit is the go-to for most projects. It reads your package.json or pyproject.toml, detects your test framework (Vitest, Jest, Pytest, Mocha, RSpec), reads an existing test file to match your style, and colocates the new test next to the source.

Cost is low (Sonnet by default) and it's the highest-frequency testing command teams install first.

It reads your dependency manifest:

  • JSpackage.json devDependencies for vitest, jest, mocha, or ava.
  • Pythonpyproject.toml or requirements-dev.txt for pytest, unittest, or nose.
  • RubyGemfile for rspec vs minitest.

This is why the command works across stacks without manual config.

Slash command for on-demand, per-file work — /test-unit at the moment you finish the source file.

Subagent for longer, exploratory tasks — for example, delegating "find the top 10 coverage gaps and write tests for all of them" to a test-coverage-analyst subagent lets it work in an isolated context without polluting your main conversation.

Most teams use both.

Narrow ones. A test-writing command needs:

  • Read, Grep, Glob — to understand the file being tested and existing patterns.
  • Write(**/*.test.*), Write(**/*.spec.*), Write(tests/**) — scoped to test paths only.

It should not have Bash access (tests aren't a place to shell out) and should not be able to write to non-test files. Narrow allowed-tools is the safety net.

Bake determinism rules into the command body. Explicitly instruct:

  • No arbitrary sleeps — use waitFor with conditions.
  • Reset state between tests — use before/after hooks with cleanup.
  • Fix timezone and Date.now in test setup.
  • Use test fixtures for auth rather than logging in during each test.

Force model: opus in the frontmatter — E2E test design benefits from the deeper reasoning.

Yes, on the common cases. It's most effective when given the failing test and recent CI history (which it reads via git log and, if configured, an MCP server for your CI provider).

Common patterns it recognizes:

  • Awaited promises resolving in different orders
  • Tests depending on execution order
  • Non-deterministic date/random values
  • Network timeouts

For deep environmental flakiness (Docker networking, GPU driver issues), you'll still need to dig manually.

Yes, and it's one of the highest-ROI hooks a team can install. The auto-test-related hook fires PostToolUse on Write/Edit events, detects which tests import the changed file, and runs just those.

If a test fails, the output goes back to Claude so it can fix on the same turn. Combined with /test-unit for authoring new tests, this pair keeps your test suite healthy without you thinking about it.

The command should detect your project's mocking approach and match it:

  • Vitestvi.mock
  • Jestjest.mock
  • Network → MSW (browser + node), nock, or vcrpy
  • Spiessinon

For network mocking specifically, /mock-external-apis generates MSW handlers, nock scopes, or vcrpy cassettes based on the API calls in the code.

Four commands cover most of what a team needs:

  1. /test-unit — daily unit test authoring.
  2. /test-e2e — critical user flows.
  3. /coverage-check — periodic gap analysis.
  4. /fix-flaky-test — the inevitable CI complaints.

Add /mock-external-apis if your project has significant third-party API dependencies. All five are on this page.

Yes. Pass the framework as an argument. For example:

/test-unit UserService.ts --framework=jest

…forces Jest even if the project uses Vitest elsewhere. Useful when you're incrementally migrating or supporting multiple test runners in a monorepo. The command's argument-hint field documents this.

Share with