Writing tests with Claude Code
Course contents (20 lessons)
- Meet Claude Code
- What Claude Code is and how it differs from a chat window
- Installing Claude Code and signing in
- Your first session: explore, ask, change
- Asking well and staying in control
- Asking for changes: context, goal, constraints and acceptance criteria
- Reading and approving edits
- Permission modes: how much freedom to give
- Project memory and slash commands
- CLAUDE.md: teaching the tool about your project
- Slash commands and custom commands
- Everyday coding workflows
- The plan-first workflow
- Understanding unfamiliar code
- Debugging with Claude Code
- Refactoring safely
- Writing tests with Claude Code
- Git, small changes, cost and security
- Git workflows: branches, commits and pull request descriptions
- Keeping changes small and reviewable
- Context and cost awareness
- Security habits: secrets, commands and untrusted input
- Extending the tool, teamwork and a final project
- Hooks and MCP servers explained
- Team practices for AI-assisted coding
- 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
| Smell | Why it is a problem |
|---|---|
| Test repeats the code under test | It passes when the code is wrong in the same way. |
| Heavy mocking of everything | It tests the mocks, not the behaviour. |
| Asserting only that nothing throws | Wrong results pass. |
| Tests that depend on order or the clock | They fail randomly later. |
| Dozens of tiny near-identical tests | High 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