Skip to main content
testing AI generated codeAI test automationSDET

Testing AI Generated Code Before It Ships: A PR Gate

12 September 2026 · OpenCrevo

Testing AI generated code is not "run the tests the agent also wrote." Those tests often encode the same mistake. You need an independent contract on the PR, plus a cheap mutation that proves the new tests can fail. Use the validate checklist (/blog/validate-ai-generated-test-checklist/) for assertion strength and the generation vs reliability piece (/blog/ai-test-generation-vs-reliable-test-generation/) when the suite starts growing junk coverage.

The failure: agent tests the bug it introduced

Copilot adds a discount helper that applies 20% twice. It also adds a test that posts /checkout and expects 200. CI is green. Finance sees double discounts at month end. The test never asserted the money.

PR gate for testing AI generated code

import { test, expect } from "@playwright/test";

test("discount applies once on checkout total", async ({ request }) => {
  const res = await request.post("/api/checkout", {
    data: { sku: "MUG", qty: 1, discountCode: "SAVE20" },
  });
  expect(res.ok()).toBeTruthy();
  const body = await res.json();
  expect(body.total).toBe(800);
  expect(body.discountApplied).toBe(200);
});
# Cheap mutation: break the helper, tests must go red
# If they stay green, the agent tests are not testing the helper
sed -i 's/applyOnce/applyTwice/' src/pricing/discount.ts
npx playwright test tests/checkout-contract.spec.ts
# expect non-zero exit
git checkout -- src/pricing/discount.ts
  • Require a contract test that asserts a business number (total, status enum, email sent), not only HTTP 200.
  • Run one targeted mutation on the changed file. If tests stay green, reject the PR.
  • Do not let the same agent merge its own tests without a human on the contract file.
  • Not sure where your test quality baseline stands? Take the free QA maturity assessment first, then escalate through AI quality engineering services or the contact form.

Pass, fail, cleanup

  • Pass: Double-apply mutation turns the contract test red; reverting the mutation turns it green.
  • Fail: The only new tests are snapshots of the full HTML or a 200 status.
  • Cleanup: Revert the mutation file. Never leave applyTwice in the branch.
START YOUR QUALITY JOURNEY

Your next chapter starts with a conversation.

Book a free quality audit. We'll review your AI system, identify the highest-risk failure modes, and map a quality roadmap tailored to your stack.