Ask a coding assistant for unit tests and you get them in seconds: a dozen cases, all green.

Green says that the tests agree with the code. It does not say that the code agrees with the requirement.

When the tests are written by reading the implementation, a bug in the implementation can be copied straight into the expected values. The suite then defends the bug.

AI-generated unit tests are still worth having. They are good at volume, structure and edge cases a person forgets. This article covers where they help, the patterns that make them mislead, and a short routine for checking that a test can actually fail.

Where AI-generated tests help

An assistant is useful for the parts of testing that are mechanical once the intent is clear:

  • Enumerating cases. Empty input, zero, negative numbers, very long strings, missing fields, duplicate entries. Asking “which inputs could break this?” often surfaces a case you had not considered.
  • Boilerplate. Setup, fixtures, factories and the structure of a test file in your framework’s style.
  • Table-driven tests. Turning a list of input and expected-output pairs into one readable parameterized test.
  • Tests from a written specification. When the requirement is stated precisely, translating it into assertions is exactly the kind of work an assistant does well.
  • Characterization tests for legacy code. Recording what old code currently does before you refactor it. This is useful as long as everyone knows the tests record current behavior, not correct behavior.

The common thread: the assistant does the typing, and a person or a written requirement decides what is correct.

A small example

Consider a hypothetical checkout function with three requirements:

  1. The code SAVE10 takes 10% off the total.
  2. Codes are not case-sensitive: save10 works too.
  3. A discount never makes the total negative.

The implementation has a bug. It compares codes exactly, so save10 is ignored.

const DISCOUNTS = { SAVE10: 0.1 };

export function applyDiscount(total, code) {
  const rate = DISCOUNTS[code] ?? 0;      // bug: case-sensitive lookup
  return Math.max(0, total - total * rate);
}

Ask an assistant to “write unit tests for applyDiscount” with only this file in view, and a plausible result looks like this:

test('applies SAVE10', () => {
  expect(applyDiscount(100, 'SAVE10')).toBe(100 - 100 * 0.1);
});

test('ignores unknown codes', () => {
  expect(applyDiscount(100, 'save10')).toBe(100);
});

Both tests pass. The second one asserts the bug as the intended behavior, and the first one computes its expected value with the same formula as the code. Neither would ever catch a mistake in that formula.

Nothing here is unusual for a model. Given only the implementation, the implementation is the only evidence of intent it has.

Five ways they mislead

Patterns that make a passing test prove little
PatternWhat it looks likeWhy it misleads
Bug as expectedThe assertion matches what the code returns today.The test now fails when someone fixes the bug.
Mirrored logicThe expected value is calculated with the same formula as the code.A wrong formula is wrong in both places, so the test still passes.
Mocked awayThe unit under test is replaced by a mock, or the test only checks that a mock was called.The real logic never runs.
Weak assertiontoBeDefined(), “does not throw”, or a check on type only.Almost any output passes.
Snapshot acceptedA failing snapshot is regenerated without reading the change.The new output, bug included, becomes the reference.

The snapshot case has explicit guidance. Jest’s documentation says snapshot files should be reviewed as part of code review, and warns against regenerating snapshots when tests fail instead of finding the cause. The update flag rewrites every failing snapshot at once, which makes it easy to accept a bug along with an intended change.

A large number of tests or a high coverage figure does not offset any of these. Stryker’s documentation notes that code coverage does not tell you everything about how effective your tests are. Coverage shows which code ran during the tests, not whether the tests would notice if that code were wrong.

Spot the problem

Each test below passes against the buggy applyDiscount. Decide what is wrong before opening the answer.

Try it
expect(applyDiscount(80, 'SAVE10')).toBeGreaterThan(0)

Weak assertion. Any positive result passes, including no discount at all. Assert the exact value from the requirement: 72.

expect(applyDiscount(50, 'SAVE10')).toBe(50 * (1 - DISCOUNTS.SAVE10))

Mirrored logic. The expected value reads the same table and repeats the same formula as the code. Write the literal: 45.

expect(applyDiscount(100, 'Save10')).toBe(100)

Bug as expected. Requirement 2 says codes are not case-sensitive, so the correct value is 90. This test fails as soon as the bug is fixed, which is backwards.

Give it the requirement, not just the code

The most effective change happens before any test is generated: give the assistant the requirement and ask it to test that.

Use in your coding assistant
Write unit tests for applyDiscount from these requirements, not from
the current implementation:

1. SAVE10 takes 10% off the total.
2. Codes are not case-sensitive.
3. The result is never negative.

Use literal expected values. Do not compute them with the code's
own formula or constants. Do not mock applyDiscount.
If the current code fails a requirement, keep the test, report the
failure, and do not change the test to match the code.

With that prompt, the case-sensitivity test fails against the buggy code, which is the result you want. The failing test is a finding, not a problem to be silenced.

The last instruction matters most. An assistant asked to make the suite pass will sometimes change the test instead of the code. Saying in advance which one is the authority prevents that.

This order also suits bug fixing. When a bug is reported, write a test that reproduces it and watch it fail before changing the code. AI-assisted debugging: reproduce, diagnose, verify covers that loop in more detail.

Make each test fail once

A test you have never seen fail is unproven. The quickest check is manual: break the code on purpose and run the test.

  • Change 0.1 to 0.2. The SAVE10 test should fail.
  • Remove Math.max(0, …). The never-negative test should fail.
  • Return total unchanged. Every discount test should fail.

Undo each change afterwards. If a test still passes after you have broken the behavior it claims to check, it is not checking that behavior.

Google’s code review guidance asks reviewers the same question about every test: will it fail when the code is broken?

Mutation testing automates this. A tool makes many small changes to the code, called mutants, and runs the tests against each one. According to Stryker’s documentation, a mutant is killed if at least one test fails and survives if all of them pass. Surviving mutants point to behavior no test checks. Stryker covers JavaScript and TypeScript, C# and Scala; other ecosystems have their own tools.

Mutation runs can be slow on a large codebase. Running them on the files a change touches, or on critical modules only, keeps them practical.

A review routine

Generated tests are part of the change and need the same review as the code. How to review AI-generated code before you merge it covers the wider review. For the tests themselves, a short routine catches most of the patterns above:

  1. Map tests to requirements. Each requirement should have at least one test. Tests that match no requirement are either missing a requirement or testing incidental behavior.
  2. Read every expected value. It should be a literal from the requirement, not a calculation that repeats the code.
  3. Check what is mocked. Mocks should replace slow or external things, such as networks, clocks and payment providers, not the logic under test.
  4. Look for weak assertions. Replace “is defined” and “does not throw” with the specific result.
  5. Break the code once per important rule, or run a mutation tool on the changed files.
  6. Read snapshot diffs before accepting them.
  7. Label characterization tests so nobody mistakes recorded behavior for correct behavior.

None of this makes AI-generated tests less useful. It moves the assistant to the part of the work it does well, writing many precise tests quickly, and leaves the decision about what is correct with the requirement.