Debuly

Guide · Coding

How to write unit tests with AI: a step-by-step guide

Generate unit tests with AI and check they are worth keeping: steps, seven tools compared on pricing and limits, coverage checks, and mistakes to avoid.

By the end of this guide you will have a set of unit tests for a real module, generated with AI, run in your own test runner, and checked to make sure they actually catch bugs. The job is done by an AI coding assistant or a dedicated test-generation tool: you point it at a function, class or file, it drafts the tests, and you review and run them.

If you want to write unit tests with AI, the real decision is how much the tool should do on its own: suggest tests in your editor one at a time, run as an agent across a project, or work in bulk on a language-specific engine. This guide covers each option and compares seven tools from our Coding category.

Key takeaways

  • Pick the code to cover, give the tool the source and your test framework conventions, generate tests, run them, and then check that they fail when the code is broken.
  • The main decision is the working mode: an editor assistant or agent, a bulk generator for Java and Python, or a traffic-based tool for API regression tests.
  • Free options exist but are small: for example Diffblue Cover's Community edition covers up to 25 methods a month, and Keploy's Playground is free forever.
  • Passing tests are not proof of good tests. A person must review what each test actually asserts.

What you need before you start

  • A codebase you can run. The tests need to execute locally, so make sure your build and your existing test command work first.
  • A target. Choose one module or a few functions, not the whole repository, for your first run.
  • An editor or terminal. Most options here live in VS Code, JetBrains, a terminal or a CI pipeline.
  • Your test conventions. Know your framework, naming style and where test files live, so you can tell the tool.
  • Time. A first set of reviewed tests for one module takes under an hour.
  • Difficulty. Easy with an editor assistant, moderate with a CLI agent, harder with traffic-based or mutation tools.
  • Code policy. Test generation sends source code to a language model in many tools, so check what your team permits.

Which kind of AI tool fits this job

Choose by language, by how much control you want, and by what you want to check.

An assistant inside your editor. GitHub Copilot suggests code as you type, answers questions in chat and lists test generation among its use cases. Cursor is an AI code editor built around an agent that plans and carries out multi-step tasks, which suits a job like covering a whole module. GoCodeo is a VS Code agent with a Test mode that creates unit tests, runs them in a panel and uses AI to fix failing ones.

A coding agent in your terminal. Claude Code takes a job such as writing tests and works through the project while you steer and review. It runs in a terminal, in VS Code and JetBrains extensions, and elsewhere.

A bulk generator for Java and Python. Diffblue Cover works on a class, a method or a whole repository, and checks that every generated test compiles, passes and adds coverage. It supports Java and Python only.

Tools that test the tests or the API. Keploy records real API traffic and turns it into regression tests and dependency mocks, and also has a unit test generator for VS Code and JetBrains. Mutahunter is an open-source mutation testing tool: it asks a language model to make small deliberate changes to your code and reports how many of those changes your tests catch.

Step 1: Choose what to cover first

Start with code that matters and is easy to run in isolation.

  1. Pick a module with clear inputs and outputs, such as a pricing calculation or a parser.
  2. Avoid code that depends on live databases or networks for your first attempt.
  3. Run the existing tests and note the baseline coverage if your project measures it.
  4. Write down the behaviors that must hold, in plain sentences.

Check it worked by being able to describe, in two or three sentences, what the code under test is supposed to do.

Step 2: Pick a tool that fits your language and workflow

You end this step with a tool installed or enabled and a plan for how it will run.

  1. If you want help as you type, enable an editor assistant such as GitHub Copilot or GoCodeo, or open the project in Cursor.
  2. If you want an agent to work through a module, try a terminal agent like Claude Code.
  3. If your code is Java or Python and you want tests in bulk, look at Diffblue Cover.
  4. If you test APIs, look at Keploy.
  5. Check the free plan against your project size using the comparison table below.

Check it worked by running a simple prompt, such as asking the tool to explain one function, to confirm it can see your code.

Step 3: Give the tool context

Tests are only as good as the context the tool has about your code and conventions.

  1. Open or select the source file and, if one exists, an existing test file as an example of your style.
  2. Name the test framework and the assertion style you use.
  3. List the behaviors from Step 1, and the edge cases you care about: empty input, boundaries, invalid values, errors.
  4. Ask for tests of one function at a time rather than a whole file in a single request.

Check it worked by confirming the tool references the right file and framework in its first reply.

Step 4: Generate the first batch of tests

Let the tool draft the tests, then keep them in a branch, not on your main line.

  1. Ask for tests that cover normal use, edge cases and expected failures.
  2. For bulk tools, point them at a class, a method or a repository. Diffblue Cover has a batch mode for entire repositories.
  3. Save the generated test files next to your existing tests.
  4. Do not edit the source code yet. You want the tests to describe current behavior first.

Check it worked by seeing new test files in your version control status.

Step 5: Run the tests and fix what fails

A generated test that does not run is not a test yet.

  1. Run the whole suite with your normal test command.
  2. Fix compile errors and wrong imports, or ask the tool to fix them. GoCodeo's Test mode, for instance, runs tests in a panel and fixes failing ones with AI.
  3. When a test fails, decide whether the test or the code is wrong. Do not delete the assertion just to make it pass.
  4. Re-run until the suite is stable, and run it a few times to catch flaky tests.

Check it worked by getting a clean run, repeated a few times, with no tests skipped or deleted.

Step 6: Review the assertions with a person

AI-written tests often check what the code does, not what it should do.

  1. Read each test and ask what bug would make it fail.
  2. Remove tests that only repeat the implementation line by line.
  3. Make sure expected values come from your behavior list, not from running the code and copying its output.
  4. Rename vague tests so a failure message explains itself.

Check it worked by having a teammate read a sample of the tests and confirm they describe the intended behavior.

Step 7: Measure whether the tests catch bugs

Coverage tells you which lines ran, not whether the tests would notice a mistake.

  1. Re-run your coverage report and compare it with the baseline from Step 1.
  2. Try mutation testing. Mutahunter needs a test command, the source file, the matching test file and a model name, and it reports how many mutants your tests killed or let survive. It needs Python 3.11 or newer and an API key for your chosen model.
  3. Add tests for any mutants that survive.
  4. Wire the suite into your CI pipeline so the tests run on every change.

Check it worked by seeing coverage rise and fewer surviving mutants than before.

Tools for this job compared

Tool Good fit for Pricing Stated limits
GitHub Copilot Editor chat and completion, including test generation Freemium, paid from $10/mo (checked 23 Oct 2026) Free plan is capped at 2,000 inline suggestions per month and excludes the cloud agent and code review
Cursor Agent-led work in an AI code editor, with CLI Freemium, paid from $8/mo (checked 24 Oct 2026) Free plan has limited agent and chat requests; third-party model usage billed separately
Claude Code Terminal, IDE and web agent that writes tests Paid Cloud sessions, the desktop app and Slack need a claude.ai subscription; API usage billed per token
Diffblue Cover Bulk tests for Java and Python Freemium, paid from $1,500, usage-based (checked 24 Oct 2026) Java and Python only; Community edition capped at 25 methods per month; licence checks need internet except offline Enterprise
Keploy API regression tests from recorded traffic, plus unit test plugins Freemium, paid from $19/mo (checked 24 Oct 2026) Playground allows 30 test suite generations, 100 test runs and 5 AI credits per month
Mutahunter Checking test quality with LLM-made mutants Open source Needs Python 3.11 or newer and a model API key; AGPL-3.0 licence
GoCodeo VS Code Build, Ask and Test modes Freemium, paid from $9/mo (checked 24 Oct 2026) VS Code only; Hobby plan has monthly caps, including 50 premium model requests

Prices and plan limits change, so confirm the current terms on each vendor's site before you subscribe.

Common mistakes to avoid

  • Accepting tests because they pass. A test can pass and still assert nothing useful. Fix it by reading every assertion and by running a mutation check as in Step 7.
  • Copying the code's current output as the expected value. If the code has a bug, the test then locks it in. Fix it by taking expected values from your written behavior list or from the specification.
  • Generating for the whole repository at once. Large batches produce more tests than you can review. Fix it by working one module at a time.
  • Ignoring where your code is sent. Some tools send source and test code to the language model you configure; Keploy's listing says this about its unit test generation. Fix it by checking your policy and the vendor's data terms before you start.
  • Skipping edge cases in the prompt. Assistants default to the happy path. Fix it by naming boundaries, empty values and error conditions in your request.

How we put this together

This is an evidence-based guide built from each tool's listing in our directory, which is compiled from the vendor's own site and pricing page. It is not a review based on using the tools. We don't rank tools, and nobody paid to be included. Some outbound links may be affiliate links; see our disclosure.

Frequently asked questions

Is there a free way to write unit tests with AI?

Yes, within limits. GitHub Copilot has a free plan with monthly limits, Diffblue Cover's Community edition is free for up to 25 methods a month, Keploy's Playground is free forever, and GoCodeo has a free Hobby plan. Mutahunter is free to use, but you pay your model provider for API calls.

Can AI write good unit tests?

It can draft useful ones quickly, but quality varies. Tests may check current behavior instead of intended behavior, so a person must read the assertions. A mutation check, as described in Step 7, shows whether the tests would notice a real bug.

Which languages are supported?

It depends on the tool. Diffblue Cover supports Java and Python. Mutahunter is language-agnostic because it uses a model to create mutants. For editor assistants and agents, check each vendor's listing for your stack.

Can I generate tests without sending my code to a third party?

Not always. Several tools rely on a language model that receives your code. Mutahunter and Keploy are open source, but Mutahunter calls the model you choose to create mutants, and Keploy's unit test generation sends source and test code to the model you configure. Check each vendor's data terms and your own policy.

How long does it take?

The vendors do not publish standard times. Reviewing, fixing and measuring the tests is the real work, so plan for it after the first batch is generated.

Compare more options in our Coding category. To find bugs the tests uncover, see how to debug code with AI.

Related guides

Related categories