Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
FREEDOM251

Freedom251 › Blog › AI-generated code

How to Write Tests That Catch Bugs in AI-Generated Code

By Freedom251 Team · · 11 min read

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

AI-generated code is not verified because it compiles or because its first test run is green. The dependable way to find defects is to write tests from the required behavior, include valid and invalid cases at boundaries, and challenge the suite with deliberately wrong implementations. Every defect you confirm should become a regression test.

Start with the contract, not the generated implementation

Before reading the assistant’s comments or accepting its design, write down the behavior the software must provide. Use the task specification, API contract, issue description, acceptance criteria, or protocol definition as the authority. A test can check an agreed requirement; it cannot decide what an ambiguous requirement was supposed to mean.

As an Amazon Associate I earn from qualifying purchases.

  • Inputs: Identify required types, formats, ranges, optional fields, ordering rules, and permitted empty values.
  • Outputs: Specify the exact value, type, ordering, rounding, status code, or state change expected for each relevant case.
  • Failures: Define which invalid inputs raise which exceptions or return which error responses. Include whether the operation must leave state unchanged.
  • Side effects: Record files, database rows, messages, network calls, logs, cache entries, and cleanup that are part of the contract.
  • Timing and limits: Note timeouts, retry limits, size limits, rate limits, and concurrency requirements when they matter to users.

Keep this contract independent of the generated code. If the implementation calls a helper named normalize, that name is not evidence that normalization is correct. Derive expected results separately, with a formula, a hand-calculated example, a trusted fixture, or an independently implemented reference for small domains.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Turn requirements into a behavior matrix

A compact matrix prevents a happy-path example from standing in for a specification. Create one row for each meaningful behavior and vary every input dimension that can change the result.

#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter | 4Pack Type C Female to A Male Cable Converter for Apple Duo 17 16 15 14 Plus,iWatch Watch 12 11 10,iPad 9 8 Air,Airpods 5 4
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
Case class What to include Question the test must answer
Typical valid input Representative values used in ordinary operation Does the normal result match the contract?
Lower and upper boundaries Minimum, maximum, just below, and just above allowed values Are comparisons and inclusive limits correct?
Invalid input Wrong type, malformed format, missing field, null, empty value, and out-of-range value Is the documented error raised without an unintended side effect?
Combinations Interactions among options, flags, permissions, states, and input dimensions Does one condition change the behavior of another?
Repeated and ordered operations Second call, retry, duplicate event, out-of-order event, and concurrent request where relevant Is the operation idempotent or stateful as specified?
Dependency failure Timeout, unavailable service, malformed dependency response, and partial write Does the code fail safely and clean up correctly?

NIST’s 2021 verification guidance describes black-box cases for functional requirements, negative cases, overload, boundaries, and combinations. Apply that same breadth even when the generated function is short: a ten-line parser can still have dozens of observable behaviors.

Write assertions that can distinguish right from wrong

An assertion should fail for a plausible implementation that violates the contract. Assertions such as “the function returned something,” “the list is non-empty,” or “no exception occurred” rarely provide that discrimination. Compare the complete result when the contract defines it, including type, ordering, precision, and relevant metadata.

  • For a calculation, compare against an independently derived expected value and state the tolerance only when the specification permits rounding error.
  • For a parser, compare every extracted field and verify rejection of malformed input.
  • For an HTTP endpoint, check status, response body, headers that are contractual, and database or message side effects.
  • For an exception, assert the exception class and, when it is part of the public contract, a stable portion of the message. Avoid matching incidental wording that makes tests needlessly fragile.
  • For a mutation of state, assert both the new state and the absence of changes that must not occur on failure.

Mocks are useful for isolating an external dependency, but a mock can only verify the assumptions encoded in the mock. Retain higher-level tests that exercise the real serialization, authentication, database mapping, or service boundary in a supported environment.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use pytest parametrization to expose edge cases

In Python, parametrization runs one assertion body against a visible set of input and expected-output pairs. That makes boundary coverage reviewable and keeps failures tied to a specific case. The following example tests a discount rule whose contract is: valid totals are non-negative numbers; totals below 100 receive no discount; totals from 100 through 499 receive 10 percent; totals of 500 or more receive 20 percent; non-numeric and negative inputs raise ValueError.

import pytest

@pytest.mark.parametrize(
    'total, expected',
    [
        (0, 0),
        (99.99, 99.99),
        (100, 90),
        (499.99, 449.991),
        (500, 400),
    ],
)
def test_discount_boundaries(total, expected):
    assert calculate_discount(total) == pytest.approx(expected)

@pytest.mark.parametrize('bad_total', [-1, None, '500', object()])
def test_discount_rejects_invalid_totals(bad_total):
    with pytest.raises(ValueError):
        calculate_discount(bad_total)

The expected values come from the written rule, not from a call to calculate_discount inside the test. If the test uses the implementation to calculate its own expected result, the same bug can appear on both sides and remain invisible.

Rank #2
Sale
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter | USB C Dongle with 90W Max Power Delivery, 3×5Gbps USB A Data Ports, for MacBook, Dell, and More (Charger Not Included)
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

Test invalid input, boundaries, and combinations deliberately

Boundary errors

Off-by-one defects often survive ordinary examples. For an allowed range from 1 through 10, test 1, 10, 0, and 11. For a string limit of 255 characters, test 254, 255, and 256 characters. For time windows, test the exact start and end instants, adjacent timestamps, daylight-saving transitions when applicable, and different time zones if the contract exposes them.

Type and format errors

Exercise missing keys, additional keys, null values, booleans where integers are expected, Unicode text, invalid encodings, malformed dates, duplicate identifiers, and very long values. Confirm that an error is returned at the public boundary rather than an unrelated stack trace or a partially applied update.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Combinations

Pair conditions that interact: an authenticated user with an expired token, a valid file with insufficient permissions, a feature flag enabled in a read-only mode, or a retry after a timeout. You do not need every Cartesian-product combination. Choose combinations that represent distinct branches, risk boundaries, and historically fragile interactions.

Overload and security-relevant cases

Where the context warrants it, test large payloads, deeply nested input, high request counts, repeated authentication failures, path traversal strings, injection characters, and resource exhaustion. Verify bounded memory and time behavior, safe error handling, and that rejected input does not alter state. The appropriate limits depend on the product’s threat model and deployment environment; do not treat a unit-test limit as a universal security guarantee.

Include state, side effects, and dependency failures

Pure functions are only part of an application. For stateful code, specify the sequence as well as each individual call. Test a successful operation followed by a duplicate, a failed operation followed by a retry, and shutdown or cancellation during work. Check that transactions roll back, temporary files are removed, locks are released, and messages are not sent twice when the contract forbids duplicates.

Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter | 85W Max PD, 2 USB 3.0 & 1 USBC Data Ports, SD/TF Card Reader, for Laptops/Mac and Other Type C Devices (Charger Not Included)
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

Use dependency fakes or mocks to force timeout, connection refusal, malformed responses, and partial failures. Assert the recovery behavior: a bounded retry with the correct backoff, a useful error, a fallback value, or an intentional abort. Then run integration tests against the real dependency or a protocol-compatible environment so that a mock’s simplified behavior does not hide serialization, authentication, or schema mismatches.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Ask whether the suite rejects known-wrong code

A practical test of test quality is to change the implementation in a way that should be wrong and confirm that the suite fails. Consider these small mutations:

  • Change <= to < at a boundary.
  • Return the input list in its original order instead of sorting it.
  • Skip validation for one required field.
  • Return a success value after a dependency timeout.
  • Raise TypeError where the contract requires ValueError, or silently accept the invalid value.
  • Apply a side effect before validation and leave it in place when validation fails.

NIST’s Code Pilot evaluation plan, revised February 19, 2026, uses a similar model: generated tests are evaluated against correct code and two incorrect variants, one with a mutated error and one omitting TypeError/ValueError checks. Its evaluation bank contains 40 simple and 10 extended specifications. Those figures describe that benchmark, not the defect rate of production AI code.

If a mutation survives, first decide whether the mutation is actually forbidden by the contract. If it is, add the smallest test that demonstrates the violation. If it is not, improve the specification rather than forcing an arbitrary assertion.

Expand beyond hand-picked examples

Generated and property-based inputs

Hand-written cases provide clear explanations and stable regression coverage. Generated inputs explore combinations humans are unlikely to select. Use a property rather than a single expected value when the domain supports one: sorting should preserve the multiset of elements and produce a nondecreasing sequence; encoding followed by decoding should recover the original supported value; removing an item twice should be idempotent if the contract says so.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray | Data transfer & Fast charging for MacBook, iPhone 17/16/15, iPad, Samsung Galaxy, Apple Watch
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft

Constrain generators to the public input domain, record the random seed, and retain a minimized failing example. A random string outside the API’s allowed encoding may reveal only an unsupported precondition; a generated value that satisfies the contract can reveal a real defect.

Fuzzing

Fuzz parsers, file readers, protocol handlers, and boundary APIs with malformed, truncated, oversized, and unusual inputs. The useful result is not merely “no crash”: assert that the program rejects invalid data safely, does not hang, does not leak secrets, and does not corrupt state. Run fuzzing separately from fast unit tests when its runtime or corpus makes that necessary.

Mutation testing

Mutation tools alter operators, constants, branches, and return values to create plausible wrong programs. A mutation score is informative only when surviving mutations are reviewed: some are equivalent to the original behavior, while others expose a missing assertion. EvalPlus combines generated and mutation-based test-input strategies in its benchmark work. Its 2023 abstract reports an 80-fold expansion of HumanEval test cases; after expanded tests, reported pass@k reductions reached 19.3–28.9 percent, with an average 15.1 percent reduction across its 14-model evaluation. These are benchmark results for particular datasets and model evaluations, not estimates of how often real-world generated code is defective.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Make every fixed bug a permanent regression case

When a defect is found, preserve the smallest input and state sequence that reproduces it. The regression test should fail against the buggy behavior, pass after the repair, and remain understandable to someone who did not see the original incident. Include the requirement or issue identifier in the test name or nearby comment when your project uses traceability.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Do not delete a regression test because the bug seems unlikely to return. Generated code may be regenerated after a prompt or dependency change, and refactoring can reintroduce the same boundary mistake. A growing regression suite records the project’s actual failure history and becomes more valuable than a large collection of unexamined examples.

Best Value
Sale
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display | 85W Max PD, 1 Powered USB-C 5Gbps & 2 Powered USB-A 3.0 5Gbps Data Ports, for MacBook Pro, MacBook Air, Dell and More (Black)
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

NIST’s guidance states: “Test cases which have been created to specifically show the presence (and later, the absence) of a bug can be used to identify issues in the absence of more general ‘first principles’ assurance approaches for detecting bugs.” The practical implication is simple: the test belongs beside the fix, not in a temporary notebook.

Use coverage as a map, not a verdict

Statement and branch coverage show which code executed. They do not show whether the expected result was asserted or whether a wrong result would fail. A test that executes an error branch but only checks that the function returned can raise coverage without increasing defect sensitivity.

Review uncovered lines and branches to find missing behaviors, then review covered tests for meaningful assertions. Pair coverage with mutation results, known-wrong variants, and a behavior matrix. A lower coverage number can be acceptable when unreachable defensive code is documented; a high number with weak assertions is not evidence of correctness.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Run the suite in the environments that matter

  1. Fast feedback: Run unit and parametrized tests on every change.
  2. Broader checks: Run integration tests, dependency-failure tests, and supported database or runtime combinations in continuous integration.
  3. Generated-input jobs: Schedule property-based, fuzz, or mutation jobs at a cadence appropriate to their cost, and publish minimized failures as ordinary regression cases.
  4. Release gates: Require the behavior suite to pass, review new failures, and verify that the tested configuration matches the release configuration.

When a test fails, classify the failure before changing code: an implementation defect, an incorrect expected value, an ambiguous requirement, an unstable environment, or a test coupled to an incidental detail. Fixing the assertion merely to restore a green build can erase the evidence of a real bug.

Choose a testing mix by risk

Strategy Behavior covered Defect sensitivity Input breadth Maintenance and fit
Hand-written examples Clear normal cases and named requirements High when expected results are independent Narrow but deliberate Easy to diagnose; essential for public behavior
Boundary and negative tests Limits, invalid data, errors, and cleanup High for off-by-one and validation defects Targeted Low maintenance when the contract is explicit
Integration tests Serialization, real dependencies, and side effects Finds contract mismatches mocks miss Moderate Slower and environment-sensitive
Property-based or fuzz tests Invariants and unexpected combinations Good for parser, algorithm, and robustness defects Wide or generated Requires reproducible seeds and failure reduction
Mutation testing Whether assertions reject plausible wrong code Directly measures test weakness Mutated implementations rather than new inputs Computationally expensive; best for critical modules

Use more than one row when failure cost is high. A unit suite can be fast and precise, while integration, generated-input, and mutation checks answer different questions rather than competing for a single ranking.

A review checklist for AI-generated changes

  • Did the tests come from an explicit requirement rather than the assistant’s implementation?
  • Is every important output independently expected, including ordering, type, precision, and status?
  • Are normal, invalid, lower-bound, upper-bound, just-outside-bound, and combination cases present?
  • Are exceptions, error bodies, rollback, cleanup, retries, and external calls checked where contractual?
  • Would the suite fail for an off-by-one branch, skipped validation, wrong return value, or missing side effect?
  • Are mocks supplemented by at least one test of the real integration boundary?
  • Would generated or fuzzed input add meaningful breadth, and are failures reproducible?
  • Did every confirmed defect become a regression test?
  • Are coverage numbers interpreted alongside mutation or known-wrong-variant results?
  • Does continuous integration run the supported language, dependency, and deployment configurations?

Further reading for Python teams

The official pytest documentation covers parametrization, exception assertions, fixtures, and test organization. For a book-length pytest testing guide, Python Testing with pytest, Second Edition by Brian Okken is a 272-page paperback published in February 2022; its publisher describes chapters on pytest, parametrization, exceptions and corner cases, coverage, and continuous integration. It teaches general Python testing rather than AI-specific verification.

What a green run actually tells you

A green run tells you that the current tests passed in the current environment. Confidence comes from the tests’ ability to distinguish the specified behavior from plausible wrong behavior, their coverage of invalid and boundary conditions, their checks of side effects and integrations, and their history of catching and preventing regressions. Treat the AI output as an untrusted implementation until it survives that process.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

AI-generated codeFuzzingmutation testingpytestPythonsoftware testingtest automation

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.