EN

Language

Our editions

Written for each audience, not translated word for word.

How to write tests with a coding assistant: a workflow that catches real bugs

How-toBy Lucas Matías García TorresUpdated Data checked 5 min read

Reviewed on . How we work.

Leer en español

A dark mechanical keyboard with three edge-lit glass tiles floating above it

Short answer

To write tests with a coding assistant safely, describe the expected behaviour before any code, have the assistant write a test that fails first, and let it fix the code without editing the test. Then read every assertion, because generated tests often repeat easy cases and skip boundaries, ask for the command and output instead of a summary, and use coverage and mutation testing to find lines that run without being checked. Meta's research kept only generated tests that measurably improved the suite: on Instagram, 75% built, 57% passed reliably and 25% added coverage.

Prices and limits change often: check the vendor's page before you pay. How we make money.

You can write tests with a coding assistant in minutes, but a fast test is not the same as a useful one. When an assistant writes tests by reading the code they check, the tests describe what the code does today, bugs included, and a green test run tells you little if nobody read what the tests assert. This guide gives a five-step workflow built from Anthropic's best practices for Claude Code, GitHub's Copilot documentation, OpenAI's Codex documentation, Google's guidance on code coverage and two Meta research papers on generated tests, all read on October 29, 2026. If you are new to these tools, start with our guide on how to use Claude Code.

Step 1: write down the behaviour first

Before the assistant writes any test, state what the code should do: the inputs, the expected outputs and the errors. Take it from the bug report, the ticket or the specification, not from the current code. Anthropic's best practices for Claude Code show the pattern with a bug: instead of asking it to "fix the login bug", describe the symptom, point to the code, and ask it to write a failing test that reproduces the problem, then fix it.

Step 2: watch the test fail

Run the new test before you change the code. A test that already passes before the fix proves nothing about the fix, and a test that fails for the wrong reason, such as a missing import, does not reproduce the problem either. Anthropic's guide explains why the check matters: the assistant stops when the work looks done, and if it has nothing to run that comes back pass or fail, you become the test suite.

Step 3: let it fix the code, not the test

Now ask the assistant to make the test pass without editing the test, and check the diff: if a test file changed in the same change as the fix, ask why before you accept it. Give the assistant the exact commands it should run. Anthropic recommends listing the test, typecheck and lint commands, and how to run a single test file, in the project's instructions, and OpenAI's guide to AGENTS.md for Codex uses instructions such as running npm test after modifying JavaScript files. Our guide to AGENTS.md explains where those files go.

Step 4: read the assertions

Generated tests can look complete and still miss the cases that matter. GitHub's own documentation shows this without meaning to: its example asks Copilot Chat for unit tests for a validate_price function that rejects prices of 0 or less and above 1000, and to "include edge cases". GitHub notes that responses vary from one run to the next. Its example response, read case by case, looks like this:

Table: GitHub's example response for validate_price, case by case (GitHub Docs, checked October 29, 2026)

Input Expected result In the example response
0 Error Tested twice, in two tests with different names
-10 Error Tested
1500 Error Tested
1000, the upper limit Accepted Tested
0.01, just above zero Accepted Not tested
1000.01, just above the limit Error Not tested
500, a typical price Accepted Not tested

That is our reading of one published example, not a measurement of Copilot. The lesson applies to any assistant: check the boundaries on both sides, look for duplicate tests, make sure each test checks a result and not only that nothing crashed, and be wary of mocks that replace the very code you want to test.

Step 5: measure what the tests miss

Coverage tells you which lines your tests run, not whether they check anything. The Google Testing Blog post on code coverage best practices says a high coverage percentage does not guarantee high-quality tests, that chasing 100% creates a false sense of security, and that the lines not covered matter more than the ones covered. It offers 60% as acceptable, 75% as commendable and 90% as exemplary for a project, and calls per-commit goals of 99% reasonable, with 90% as a good lower threshold. For tests that run a line without checking it, the same post points to mutation testing, which plants small faults in the code to see whether any test fails, with tools such as Pitest for Java and Stryker Mutator for JavaScript.

Meta's research applies the same discipline at scale.

Table: two Meta studies of generated tests (arXiv papers, checked October 29, 2026)

Study What it did Result
TestGen-LLM, February 2024 Extended human-written tests and filtered its own so that only those that measurably improved the existing suite reached engineers On Instagram's Reels and Stories, 75% of its test cases built correctly, 57% passed reliably and 25% increased coverage; engineers accepted 73% of its recommendations in test-a-thons
ACH, January 2025 Planted faults about one concern, privacy, and generated tests that catch them 9,095 simulated faults and 571 tests across 10,795 Android Kotlin classes; engineers accepted 73% of its tests in Messenger and WhatsApp test-a-thons

The point is the filter, not the tool: a generated test earns its place only if it builds, passes every time you run it and checks something the suite did not check before.

Ask for evidence, not a summary

When the assistant says it is done, ask for the command it ran and the output. Anthropic's guide is blunt about it: "All tests pass" on its own tells you very little, and reading the output is faster than running the check again. A second opinion helps too, within limits. Anthropic suggests a review step that checks every requirement and that the listed edge cases have tests, and warns that a reviewer asked to find problems will always find something, so fixing every finding leaves you with tests for cases that can't happen. Our guide to automated code review tools compares the options, and our comparison of the best coding assistants helps you pick a tool.

How we checked

On October 29, 2026 we read Anthropic's best practices for Claude Code, GitHub's Copilot Chat guide to generating unit tests, OpenAI's guide to AGENTS.md for Codex, the Google Testing Blog post on code coverage best practices of August 7, 2020, and the arXiv abstracts of Meta's TestGen-LLM and ACH papers. The case-by-case table is our reading of GitHub's published example response; we did not run Copilot or measure any assistant ourselves.

What we checked

  • Anthropic's best practices for Claude Code include an example prompt that describes a login bug, points to the token refresh code and asks Claude to write a failing test that reproduces it, then fix it; the guide says Claude stops when the work looks done, that without a check it can run you become the test suite, and recommends giving it something it can run that comes back pass or fail; it recommends listing the exact test, typecheck and lint commands, and how to run a single test file, in CLAUDE.md. (Anthropic (Claude Code docs), )
  • The same guide recommends asking Claude for evidence, the command it ran and its output, because "All tests pass" on its own tells you very little and reading the output is faster than rerunning the check; it suggests a review step that checks every requirement is implemented and the listed edge cases have tests, and warns that a reviewer asked to find problems will always find something, so fixing every finding leads to extra abstraction layers and tests for cases that can't happen. (Anthropic (Claude Code docs), )
  • GitHub's Copilot Chat guide to generating unit tests uses a validate_price function that raises errors for prices of 0 or less and above 1000 and the prompt to generate unit tests that validate both success and failure and include edge cases; GitHub says responses are non-deterministic, and its example response tests the inputs 0 (in two tests), -10, 1500 and 1000. (GitHub Docs, )
  • OpenAI's guide to AGENTS.md for Codex shows instructions such as always running npm test after modifying JavaScript files and, in a nested file, using make test-payments instead of npm test, and explains that Codex reads instruction files from the project root down to the current directory. (OpenAI, )
  • The Google Testing Blog post Code Coverage Best Practices (August 7, 2020) says a high code coverage percentage does not guarantee high quality in the test coverage and that aiming for 100% leads to a false sense of security; that what is not covered is more meaningful than what is covered; offers general guidelines of 60% as acceptable, 75% as commendable and 90% as exemplary; calls per-commit coverage goals of 99% reasonable with 90% a good lower threshold; and says mutation testing, with tools such as Pitest for Java and Stryker Mutator for JavaScript, can detect false coverage. (Google Testing Blog, )
  • Meta's TestGen-LLM paper (arXiv 2402.09171, submitted February 14, 2024) says the tool improves existing human-written tests and verifies that its generated tests clear filters assuring measurable improvement over the original suite; in an evaluation on Instagram's Reels and Stories, 75% of its test cases built correctly, 57% passed reliably and 25% increased coverage; in Instagram and Facebook test-a-thons it improved 11.5% of the classes it was applied to, with 73% of its recommendations accepted for production deployment by Meta engineers. (arXiv, )
  • Meta's ACH paper (arXiv 2501.12862, submitted January 22, 2025) describes mutation-guided test generation that was applied to 10,795 Android Kotlin classes in 7 software platforms, generating 9,095 mutants and 571 privacy-hardening test cases; in Messenger and WhatsApp test-a-thons, engineers accepted 73% of its tests, judging 36% privacy relevant. (arXiv, )

What may change

  • Assistant features and documentation examples change often; the workflow and the studies cited here change less. This page is reviewed every quarter.

Frequently asked questions

Can a coding assistant write my unit tests?

Yes, but treat them as drafts. An assistant that reads the code it tests describes what the code does today, bugs included, so check every assertion against the behaviour you expect.

What is mutation testing?

A way to test your tests: a tool plants small faults in the code and checks whether any test fails. If every test stays green with a planted fault, no test is really checking that code.

How do I tell the assistant which tests to run?

Write the exact commands in the project's instruction file, such as CLAUDE.md for Claude Code or AGENTS.md for Codex, including how to run a single test file, so the assistant can check its own work.

Sources

  1. Best practices for Claude Code: checks that come back pass or fail, failing tests, evidence and review, Anthropic (Claude Code docs). Accessed October 29, 2026.
  2. Generating unit tests (GitHub Copilot Chat cookbook): example scenario, prompt and response, GitHub Docs. Accessed October 29, 2026.
  3. Custom instructions with AGENTS.md (Codex): discovery order, nested files and test commands, OpenAI. Accessed October 29, 2026.
  4. Code Coverage Best Practices (Arguelles, Ivanković and Bender, August 7, 2020), Google Testing Blog. Accessed October 29, 2026.
  5. Automated Unit Test Improvement using Large Language Models at Meta (Alshahwan and others, arXiv 2402.09171, 2024), arXiv. Accessed October 29, 2026.
  6. Mutation-Guided LLM-based Test Generation at Meta (Foster and others, arXiv 2501.12862, 2025), arXiv. Accessed October 29, 2026.

Spotted an error or an outdated price? Tell us and we will fix it.

Change history

  • : First published. Checked against the documentation of Claude Code, GitHub Copilot and Codex, the Google Testing Blog and two Meta papers on arXiv on October 29, 2026.

Next review: .

Part of our Coding Assistants guide.

Other topics