Home · Business · IT, Data & Security · Software development

YES

As of 13 August 2026, AI can write unit tests for your code.

This still needs a person who signs their name to it.

Can you do it?

5 minutesto a draft.

30 minutesto something you’d act on.

Cost, all in£0

Skill neededdeveloper

Who has to check ita colleague

What the alternative costsA human developer or an existing test framework is the alternative; the supplied tool data gives no price for either.

If this goes wrong: the tests pass while missing an important failure mode, and a later code change reaches users without the protection you thought you had.

What to actually do

  1. Hand it to a person

    The route this page recommends

    A person who owns the outcome does this end to end, worth it when the failure is dear.

  2. Use a tool built for this

    Second choice
  3. Do it yourself

    The distant third

    A chat interface, developer skill, and roughly 30 minutes until you can act on the result.

    How to actually do it

    1. 1. Open the repository in your code editor and identify the source file, public functions, language, test framework, test command and existing test-file conventions.
    2. 2. Gather the relevant source code, types, fixtures, configuration and dependency interfaces, removing secrets and unrelated files before pasting them into the chatbot.
    3. 3. Write down the required behaviour, valid and invalid inputs, boundary cases, expected errors and any external dependency that must be mocked.
    4. 4. Paste the gathered material into the prompt and ask the model to produce a test file that follows the repository's existing conventions.
    5. 5. Save the generated tests in the correct test directory, replace invented imports or fixtures with the interfaces that actually exist, and run the project's test command.
    6. 6. Compare each assertion with the source code and written requirements, then add a test for every important failure mode the generated file missed.
    7. 7. Run the tests with coverage and have a colleague review whether the tests would fail if the intended production behaviour were deliberately broken.
    8. 8. Commit the tests with the production change or open a pull request, including the test command and any cases that still need integration or end-to-end coverage.

    Prompt

    You are helping me add unit tests to an existing codebase.
    
    Language and framework: [for example, Python with pytest or TypeScript with Jest]
    Test command: [command]
    Relevant source file or function:
    [PASTE CODE]
    
    Related types, interfaces, fixtures, configuration or dependencies:
    [PASTE ONLY WHAT IS RELEVANT]
    
    Required behaviour:
    [DESCRIBE WHAT THE CODE MUST DO]
    
    Known edge cases and failure cases:
    [LIST THEM]
    
    Write a complete unit-test file that matches the existing project conventions. Use the real public interface where possible and isolate external services rather than calling them. Do not invent functions, imports, fixtures, return values or requirements. If information is missing, state the assumption before the code.
    
    For each test, briefly state the behaviour it protects and why the assertion is meaningful. Include normal cases, boundary cases, invalid inputs and dependency failures where the supplied code supports them. Keep tests deterministic and independent. Do not change production code unless you identify a specific testability problem and show the smallest proposed change.
    
    After the test file, give the exact command to run it, explain any setup needed, and list the cases that still require a human decision or an integration test.

    Open it prefilled in ChatGPT or Claude, or copy it into Gemini, which takes no prefill link.

What it gets wrong

  • AI cannot know undocumented business rules that are absent from the code and prompt.
  • AI writes assertions that can repeat the implementation's mistake instead of checking the intended behaviour.
  • AI cannot reliably identify every hidden dependency, state leak or environment-specific failure in your repository.
  • AI does not decide whether a unit test, integration test or end-to-end test is the right protection for a particular risk.
  • AI cannot take responsibility for a defect that reaches users because the generated tests were incomplete.

Even on a YES, the friction has a name: judgement under ambiguity, verification cost and context depth.

How we scored this

Five axes, each scored nought to two by hand: ten means AI carries the task cleanly, and the thresholds that turn a total into YES, PARTLY or NO are published in the methodology. Each axis name links to its definition.

AxisScore (0–2)
Output2
Inputs2
Verification1
Liability1
Effort delta2
Total8 / 10

FAQ

Can ChatGPT write unit tests for my code?
Yes. It can draft test files, fixtures, mocks and ordinary edge-case tests when you provide the code, framework and expected behaviour. Run the tests and have a developer check that the assertions test the requirement rather than merely repeating the implementation.
Are AI-generated unit tests reliable?
They are useful drafts, not proof that the code is correct. They can miss hidden requirements, use imports that do not exist or pass while asserting the wrong result, so a developer must run and review them.
What should I give AI to write unit tests?
Give it the relevant source code, language, test framework, test command, existing test examples, dependency interfaces and a plain description of the required behaviour. Include boundary cases and expected errors, and remove secrets and unrelated private data.
Can AI write unit tests for a large codebase?
It works best on a small, well-defined module with its dependencies and conventions supplied. For a large codebase, split the work by component and use code review, test runs and coverage to find gaps that a single generated batch will miss.

Nearby answers

Assessed by gpt-5.6-luna (gpt-5.6-luna) on 2026-08-13, second-checked by an independent model. Wrong somewhere? Email [email protected] and it gets re-checked.

The newsletter

AI news, new answers and product picks, straight to your inbox.