Writing tests with Claude Code

11 min read text
Course contents (20 lessons)
  1. Meet Claude Code
  2. What Claude Code is and how it differs from a chat window
  3. Installing Claude Code and signing in
  4. Your first session: explore, ask, change
  5. Asking well and staying in control
  6. Asking for changes: context, goal, constraints and acceptance criteria
  7. Reading and approving edits
  8. Permission modes: how much freedom to give
  9. Project memory and slash commands
  10. CLAUDE.md: teaching the tool about your project
  11. Slash commands and custom commands
  12. Everyday coding workflows
  13. The plan-first workflow
  14. Understanding unfamiliar code
  15. Debugging with Claude Code
  16. Refactoring safely
  17. Writing tests with Claude Code
  18. Git, small changes, cost and security
  19. Git workflows: branches, commits and pull request descriptions
  20. Keeping changes small and reviewable
  21. Context and cost awareness
  22. Security habits: secrets, commands and untrusted input
  23. Extending the tool, teamwork and a final project
  24. Hooks and MCP servers explained
  25. Team practices for AI-assisted coding
  26. Final project: building a small app step by step

Tests are a good fit, with a catch

Writing tests is repetitive work that follows patterns, which suits the tool well. The catch is that a test written by looking at the code often only restates the code. If the code has a bug, the generated test may happily assert the bug. So the job is to steer the tool towards testing behaviour, and then check that the tests can fail.

Start with behaviours, not functions

I want tests for the password reset feature. Before writing any, list the behaviours that should be tested, from the user's point of view. Include normal cases, failure cases and security-related cases. Do not write code yet.

You will get a list such as "valid token resets the password", "expired token is rejected", "a token cannot be used twice", "unknown email gives the same response as a known one". Read it, add what is missing, remove what is not needed. This is where your knowledge of the real requirements counts.

Follow the existing style

Write tests for the behaviours we agreed, in tests/PasswordResetTests.cs. Follow the style of tests/LoginTests.cs: same framework, same naming pattern, same way of creating test data. Use the existing test helpers, do not create new ones. One behaviour per test.

Why it works

Naming an example file makes the new tests fit in. "One behaviour per test" makes failures easy to read. "Do not create new helpers" prevents the tool from building a mini framework you did not ask for.

Check that the tests can fail

This step separates real tests from decorations. A test that cannot fail proves nothing.

For each new test, temporarily break the production code in a way that the test should catch (for example, remove the expiry check), run the test and confirm it fails. Then restore the code. Report which tests did not fail when they should have.

This is a manual form of mutation testing. It takes a few minutes and often exposes weak tests. If your team uses a mutation testing tool, use it as well. If you try this, check afterwards with git diff that the production code is back to its original state.

Edge cases to ask about

  • Empty, null, very long and very short input.
  • Boundaries: the value just below, at and just above a limit.
  • Dates and time zones, month ends and leap years.
  • Duplicate requests and repeated calls.
  • Permissions: a user who must not be allowed.
  • External failures: timeout, error response, empty response.
Which edge cases are missing from this test file? Suggest up to 8, ordered by how likely they are to find a real bug. Do not write them yet.

Avoid these test smells

SmellWhy it is a problem
Test repeats the code under testIt passes when the code is wrong in the same way.
Heavy mocking of everythingIt tests the mocks, not the behaviour.
Asserting only that nothing throwsWrong results pass.
Tests that depend on order or the clockThey fail randomly later.
Dozens of tiny near-identical testsHigh maintenance, low value. Use data-driven tests.

Tests for legacy code

For code with no tests, begin with the most valuable and least risky ones: the public entry points and the code that handles money or permissions. Use the characterisation approach from the refactoring lesson.

Run them yourself too

The tool will usually run the tests and report the result. I still run them myself once, in a terminal, before committing. It takes seconds, and it protects me from an overly optimistic summary.

The common mistake

The common mistake is celebrating coverage numbers. The tool can raise coverage quickly with weak tests. Coverage tells you which lines ran, not whether anything was checked. Judge tests by whether they would catch a bug that matters.

Try it yourself

Choose a function with some logic, such as a discount calculator or a validator. Get the behaviour list first, edit it, then generate the tests. Break the code in two places on purpose and confirm that a test fails each time. Count how many generated tests did not catch anything, and improve them.

This course is free to read. Create a free account to enrol, save your progress and pick up where you left off.

Sign up free I already have an account