For the complete documentation index, see llms.txt. This page is also available as Markdown.

Test AI-Generated Code

Validate AI-generated code against independent acceptance rules, detect deliberate defects, and check changed application journeys with ContextQA MCP.

Quick answer

Test AI-generated code against a behavior contract written independently of the implementation. Check the changed rule, denied paths, and boundary values; then prove a deliberate defect makes the relevant assertion fail. Run repository tests locally or in CI, and verify the deployed application separately. ContextQA MCP can support that application check from your coding agent, while your existing runner remains responsible for native unit tests.

This guide is for developers reviewing changes from Claude Code, Cursor, Codex, or another coding assistant. It concerns ordinary application code authored by AI. Evaluating an LLM application's generated answers requires additional checks for output quality, variability, and misuse; a passing JavaScript suite does not establish those properties.

Why generated tests can agree with a bug

An implementation and its generated tests can share the same mistaken assumption. For example, an export endpoint might check that a user has download permission but forget that the report must belong to the same tenant. A test that merely repeats the permission expression can pass while preserving that defect.

Separate the source of expected behavior from the code under review. Use an approved acceptance criterion, API contract, incident reproduction, or stakeholder decision. If the rule is ambiguous, record the unresolved question instead of letting the assistant silently choose a policy. Asking a second model to review the same code helps generate ideas, but does not by itself supply an independent expected result.

1. Turn the requirement into explicit decisions

Consider an illustrative report-download feature. Its contract is:

  • The authenticated principal has a non-empty tenant identifier and explicit export permission.

  • The report belongs to that tenant and has status ready.

  • Access expires at the specified time: a request at the exact expiration is denied.

  • Missing or malformed inputs are denied.

Write these decisions before inspecting a proposed implementation:

Scenario
Expected decision
Defect this can expose

Authorized principal, same tenant, ready, before expiry

Allow

Accidental denial of valid downloads

Authorized principal, different tenant

Deny

Missing tenant boundary

Same tenant, exactly at expiry

Deny

Incorrect inclusive boundary

Same tenant, still building

Deny

Download before report completion

Same tenant, no export permission

Deny

Missing permission check

Keep a successful case as well as denied cases. A function that always returns false is safe from these particular unauthorized downloads but still violates the feature contract.

2. Review the agent's plan before the patch

Give the agent the requirement, changed files, existing runner command, and a focused task:

Review the diff for deleted assertions, broad mocks, swallowed errors, and changed fixtures. A test that replaces the permission function with a mock returning true cannot establish that the real permission function works. Keep a record of failures found before any retry or repair.

3. Run a small, independently specified example

Create an empty local folder using Node.js 24 or later. No packages, account, or repository download are required. Save this implementation as download-policy.mjs:

Save the following as download-policy.test.mjs in the same folder. Its literal expected decisions come from the contract above:

Run from that folder:

The suite evaluates 11 contract decisions. To check its sensitivity, make a backup of download-policy.mjs, temporarily remove principal.tenantId !== report.tenantId || from its fourth condition, and rerun. The cross-tenant assertion should fail. Restore the backup; separately change nowMs < report.expiresAt to nowMs <= report.expiresAt and confirm the exact-expiry assertion fails. Restore the original file again and confirm all tests pass. Keep these defects in a disposable local example, never a shared deployment.

Check the actual assertion failure rather than accepting any nonzero exit code: a syntax error does not prove fault detection. Node's built-in test runner provides the execution mechanism. Node test runner documentation.

This fixture checks a pure decision function. It does not test authentication, database lookup, HTTP authorization, or an actual download. In production, derive the principal from trusted authentication and report ownership from trusted storage, rather than accepting those fields from a request body.

4. Use coverage and mutation testing for different questions

Coverage helps locate code the suite did not exercise. Executing a branch does not prove its result was meaningfully checked. Mutation testing changes production logic temporarily and asks whether tests reject that change. Stryker automates this process for supported language and runner combinations. Stryker mutation testing introduction.

Start with the changed permission or calculation module. Review survivors individually: a missing boundary assertion, an unreachable case, and a behaviorally equivalent change require different responses. An equivalent mutant cannot be distinguished by an input that preserves the program's observable behavior; chasing an arbitrary 100% score can waste effort. Stryker equivalent mutants.

Record the file scope, runner, exclusions, and outcomes. Keep killed mutants, survivors, timeouts, and invalid runs separate when reviewing evidence; a process error is not an assertion catching the intended defect. The two deliberate checks above are useful demonstrations, not a Stryker run or a measured mutation score. Stryker states and metrics.

5. Check whether the application actually enforces the rule

Add an integration test that exercises the real endpoint with controlled authenticated identities. Prove that a direct request for another tenant's report returns the documented denial response and no report bytes. A hidden Download button alone does not establish server-side authorization.

Then check the visible workflow: an authorized user downloads the correct report, while an unauthorized user receives the expected explanation. Verify downloaded content or a controlled backend record where supported; a success toast is insufficient evidence. See the end-to-end testing guide for separating browser assertions from backend outcomes.

6. Add the ContextQA MCP application handoff

Follow MCP installation and setup, authenticate, and inspect the tools available to your workspace. Use a disposable staging report and approved test identities. The hosted MCP server does not automatically see a local checkout or execute its Node suite.

After discovery, adapt this prompt to supported tools:

Keep repository-test results and application-run results separate. If a requested check is unavailable through the connected tools, retain it in your API or Playwright suite. Before moving a managed test into CI, follow the export and portability checklist and verify the exported assertions independently.

Acceptance record for the pull request

Attach the contract, changed behavior, exact test commands, detected defects, application build/run identifiers, and unresolved checks. Distinguish passed, failed, skipped, and not run. This gives a reviewer evidence to assess instead of a blanket claim that the agent tested everything.

Frequently asked questions

Is high coverage enough to approve AI-generated code?

No. Review what assertions establish, especially denied paths and boundaries. Coverage and a focused defect-detection check answer complementary questions.

Should a different agent write the tests?

That can provide another perspective. Independence comes primarily from the expected behavior and evidence source, not the assistant's name. Review the contract before either agent changes code.

Does ContextQA MCP replace my unit-test runner?

No. Keep native tests in their established runner. Use MCP for supported application testing tasks and retrieve actual run results before claiming those checks passed.

Try this on one change

Choose one risky rule in your next AI-assisted pull request. Establish its expected decisions, demonstrate one detected defect, and connect the affected application flow using the ContextQA MCP setup guide. For help designing that evaluation, book a ContextQA demo.

Sources reviewed: September 25, 2026. The examples are teaching fixtures; they do not represent a live ContextQA execution or an audit of your application.

Last updated

Was this helpful?