Skip to main content

Claude/ChatGPT (or Cursor) Prompt to Generate a Test Suite

Generate a full test suite for any class: unit tests, edge cases, exception paths, integration tests, mocks, and a coverage target.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt
You are a senior engineer who writes tests that actually catch regressions. Return runnable test code in my conventions, not pseudocode.

Context:
- Class or module to test: <paste class or path>
- Language and test framework: PHP / PHPUnit
- External dependencies: database and API calls
- Mocking strategy: Mockery

Deliver:
1) Unit tests for every public method.
2) Edge cases: null inputs, boundary values, empty collections, and unexpected types.
3) Error and exception-handling tests that assert the failure mode, not just that it throws.
4) Integration tests that exercise the real dependencies above.
5) Mocks for external services using the chosen strategy.
6) A note on which lines remain uncovered and why.

Follow the test_snake_case naming convention and aim for 90% coverage. Output as copy-ready test files.

What this prompt does

This prompt turns Claude Code into a test author for a single class or module. It hands the model a fixed contract — unit tests for every public method, edge cases (null inputs, boundary values, empty collections), error and exception paths, integration tests for your real [dependencies], and a [mock_strategy] for everything external — instead of a vague "write some tests" request. That structure is the whole point: it forces breadth (every public method) and depth (the failure paths people skip) in one pass.

It works because each instruction maps to a category of bug. Boundary and null cases catch the off-by-one and unguarded-input defects; the exception tests assert that your code fails the way it claims to; the mock strategy keeps unit tests fast and deterministic while the integration tests still exercise the wiring. Naming convention and coverage target give Claude an objective finish line, so it self-checks against [coverage]% rather than stopping at three happy-path tests.

When to use it

  • You inherited a class with zero tests and need a safety net before refactoring it.
  • You just wrote a service or utility and want exhaustive coverage without hand-listing every case.
  • A bug slipped past your existing suite and you suspect the edge cases were never tested.
  • You're enforcing a coverage gate in CI and a specific file is dragging the number down.
  • You're standardizing test style across a team and want every new class to follow one [naming_convention].
  • You need both isolated unit tests and integration tests for the same class in a single, consistent batch.

Example output

For a PaymentProcessor class (PHPUnit, mock strategy = Mockery), Claude Code returns a runnable test file:

final class PaymentProcessorTest extends TestCase
{
    public function test_charge_returns_receipt_on_success(): void { /* happy path */ }

    /** @dataProvider invalidAmounts */
    public function test_charge_rejects_invalid_amount($amount): void { /* 0, -1, null */ }

    public function test_charge_throws_on_gateway_timeout(): void
    {
        $gateway = Mockery::mock(Gateway::class);
        $gateway->shouldReceive('send')->andThrow(new TimeoutException);
        $this->expectException(PaymentFailedException::class);
        // ...
    }
}

It typically closes with a coverage note: which methods hit the target and which branches still need a test.

Pro tips

  • Name [dependencies] precisely — "the Stripe gateway and the Eloquent Order model" beats "dependencies." Vague input gives you vague mocks.
  • Match [mock_strategy] to your stack: Mockery or PHPUnit mocks for PHP, unittest.mock for Python, Jest mocks for TS. A mismatch makes the tests look right but not run.
  • Treat [coverage]% as a floor, not a goal. Asking for 100% invites brittle tests of trivial getters; 80-90% usually targets the code that matters.
  • Run the suite immediately and paste any failures back — Claude is better at fixing a red test than predicting your exact fixtures and container bindings.
  • Pair it with a follow-up: "now add a mutation-testing pass" or "add tests for the private methods via their public callers" to catch assertions that pass without really checking anything.

Frequently Asked Questions

Will the generated tests actually run, or do I need to fix them first?
Expect to fix a few. Claude produces correct structure and assertions, but it can't see your exact fixtures, container bindings, or test base class. The fastest workflow is to run the suite once and paste the failures back — it resolves wiring issues quickly once it sees the real errors.
Does it generate both unit and integration tests, or just one kind?
Both, by design. The template explicitly asks for unit tests per public method plus integration tests for your named [dependencies]. Unit tests use your [mock_strategy] to stay isolated and fast; integration tests exercise the real collaborators, so name those dependencies accurately.
What coverage target should I put in the [coverage] variable?
For most classes, 80-90% hits the meaningful branches without forcing brittle tests of trivial code. Reserve 100% for critical paths like payments or auth. Treat the number as a floor — coverage proves lines ran, not that your assertions actually catch regressions.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Claude Code Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

AI assistant · trained on my work

👋

Hey there!

Quick Actions

WhatsApp Direct line to me

Chat on WhatsApp

+880 1723 741224 · Replies within the hour on working days

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

mejba.13@gmail.com

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support