GitHub Copilot Course · Testing strategy

Testing AI-Generated Code: Unit Tests and Review Strategy

Test AI-generated code with contract-first unit tests, negative cases and diff review; follow a quantity-parser example from failure to fix.

Learn the concept, work through an example, and check the result.

COPILOT LESSON PREVIEW
Testing strategyLearn → test

Core idea

Tests check behavior; review checks everything around it.

Specify → propose
Test → inspect
Decide → commit
Check the output and reasoning.Apply the example, then check a changed case.

The direct answer

Verify generated code against a contract you own.

AI-generated code needs the same scrutiny as other code because a plausible implementation can violate a product rule.

AI-generated code can look polished while implementing a different rule. The first defense is a contract written before the proposed code. Unit tests then check representative inputs and failures; review examines dependencies, data handling and maintainability that tests may miss.

The synced course pricing block follows this overview. The example is a fictional quantity parser, not a benchmark of any model.

Course access

Choose your next step in GitHub Copilot.

Review the current course options and access details here.

Heycademy Courses · Coming Soon

GitHub Copilot & AI Coding Productivity

We’re preparing an interactive learning experience with practical exercises, skill assessments and personalized learning to help you build confidence and master new skills.

Launching soon!
We’re putting the finishing touches on this course. Please check back for updates.

Topic in practice

AI-generated code in a real decision.

A parseInt shortcut accepts a decimal prefix and may return NaN for invalid input. Tests expose those failures; review checks scope, dependencies and error compatibility.

As you work through the case, identify the next action, the evidence required and what remains uncertain. The GitHub documentation provides background for checking the decision.

Contract first

Define valid input and error behavior.

For quantity(value), accept a trimmed positive whole-number string from 1 through 99. Reject blank input, signs, decimals, zero and 100. Preserve the function export and the error message Invalid quantity. The developer should ask the product owner if leading zeros are valid; here they are rejected.

Contract fixture
CaseExpected
“1”1
“99”99
“0”Error
“3.5”Error
“100”Error
” 2 “2

A plausible wrong solution

Spot the permissive conversion.

function quantity(value) { const n = parseInt(value, 10); if (n < 1 || n > 99) throw Error("Invalid quantity"); return n; }

The function accepts “3.5” as 3 and can return NaN for nonnumeric text because both comparisons with NaN are false. A happy-path test would miss both defects. The code also accepts leading zero; whether that is wrong depends on the contract.

Keep building this skill

Turn the example into a skill you can use.

Return to the course options whenever you are ready.

Explore course options →

Tests as evidence

Run normal and negative cases.

import assert from "node:assert/strict";
assert.equal(quantity("1"), 1);
assert.equal(quantity(" 2 "), 2);
for (const x of ["0", "3.5", "100", "abc", ""]) { assert.throws(() => quantity(x), /Invalid quantity/); }

Run these against the actual repository implementation. The test output is evidence about these inputs only; it does not prove all possible behavior.

Beyond unit tests

Read the diff for hidden cost.

Review checklist
Review areaQuestion
ScopeWere unrelated files changed?
DependencyWas a package added without need or approval?
SecurityCould input reach a dangerous sink?
ErrorsAre failures clear and compatible?
MaintainabilityCan another engineer explain it?

The GitHub inline-suggestion responsible-use guidance supports treating proposed code as untrusted until checked.

Changed requirement

Keep tests aligned with the rule.

The product now permits 100. Update the contract and the 100 boundary test first; then revise the implementation. Do not change only the code and leave a stale test, or change the test only to match an accidental behavior.

Transfer answer

A new upper bound needs tests at 99, 100 and 101, with the same invalid-input cases preserved.

Failure investigation

Make a red test explain the mismatch.

Run the flawed parseInt implementation against “3.5”. The returned value is 3 when the contract requires an error. That failure identifies a conversion problem, not a need to loosen the test. For “abc”, parseInt returns NaN and comparisons such as NaN < 1 are false. A function may therefore return NaN even though it appears to check a numeric range. These two cases fail for different reasons, so keep both tests.

After a proposed fix, run the whole existing suite as well as the new tests. Inspect the diff for altered errors and exported behavior. If production code accepts a special legacy input, a new strict parser may be a breaking change; document that finding and get a product decision before merging. Tests reveal behavior, while review decides whether the behavior is appropriate.

FAQ

Questions about this skill.

Do tests replace review?

No. Tests cover asserted behavior; review covers scope, security and maintainability.

Should AI write all expected outputs?

It can propose them, but the developer validates them against the requirement.

Is a passing suite enough for production?

Use the repository’s other gates and human review as appropriate.

Continue with the course

Build reliable AI-assisted coding skills.

Review the course options and choose your next learning step.

Take the next step →
Scroll to Top