Ask Claude to Write Tests for Existing Code That Has No Test Coverage
Retrofitting tests onto untested code is a chore nobody volunteers for. Claude reads the existing implementation, understands what it does, and writes tests that cover everything — including edge cases you might have missed when writing the code.
> read app/Services/PricingService.php and write a comprehensive
> test suite for it — cover the happy path, edge cases, and
> error conditions
Claude traces every code path — conditional branches, early returns, exception throws, null checks — and generates a test for each one. It doesn't just test that the method runs; it verifies the actual behavior at each branch.
// Claude generates tests like these from reading your code:
it('calculates subtotal from line item prices and quantities')
it('applies percentage discount before tax')
it('applies fixed discount after percentage discount')
it('throws when discount exceeds subtotal')
it('returns zero tax for tax-exempt customers')
it('rounds totals to two decimal places')
it('handles empty cart gracefully')
Notice how Claude found behaviors you might not think to test — the order discounts are applied, the rounding behavior, and the empty cart edge case — because it read the actual implementation and saw each path.
You can target specific areas:
> write tests for all the public methods in the OrderController
> — mock the dependencies and test the HTTP responses
> add test coverage for the email notification service — test
> what happens when the mail server is down
> write tests for the user registration flow end-to-end —
> from form submission through to the welcome email
After generating tests, have Claude run them to verify they pass:
> run the new tests and fix any that fail — the tests should
> match the current behavior, not an idealized version
This is crucial: the tests should document what the code does, not what you wish it did. If the tests reveal bugs, Claude flags them — but keeps the test matching current behavior so you can fix the bug separately.
Untested code is a liability. Let Claude write the safety net so you can change it with confidence.
via Claude Code
Log in to leave a comment.
Set up Claude Code as an automated reviewer in your CI pipeline — on every pull request, it reads the diff, checks for bugs, security issues, missing tests, and convention violations, then posts its findings as a PR comment. Your human reviewers get a head start because the obvious issues are already flagged before they look.
Before deploying, tell Claude to read your project — migrations, environment variables, queue workers, scheduled tasks, caching, third-party integrations — and generate a deployment checklist that's specific to your app. Not a generic "did you run migrations?" list, but one that knows YOUR infrastructure and catches the things YOUR deploy can break.
Instead of writing a README from memory or copying a template, tell Claude to read your project and generate one that's actually accurate — real setup instructions from your config, real architecture from your directory structure, real API examples from your routes, and real prerequisites from your dependency files.