> ## Content Index
> Fetch the complete content index at: https://locallabs-llm.org/llms.txt
> Use this file to discover other available public pages before exploring further.

# Ask an AI coding assistant for tests that catch the bug
- URL: https://locallabs-llm.org/ai-tests-for-bug-fix/
- Published: 2026-11-26T11:16:47.000Z
- Updated: 2026-11-26T11:16:47.000Z
- Description: Define the expected behavior, review the assertions, and check a regression test against the faulty version before accepting an AI-assisted fix.
- Author: Albert Curry
- Tags: Code & build

Give the AI assistant the reported bug, the expected behavior from an approved requirement, and an existing test it can follow. Ask for test cases first, then a tests-only change. Before accepting a fix, check that the new regression test fails on the faulty version for the intended reason and passes after the correction with its assertions intact.

[Coding with AI](https://locallabs-llm.org/tag/coding/)

## Write the expected result before showing the implementation

I would begin with the smallest input that exposes the reported defect. A broad request to “improve test coverage” can produce plenty of tests while leaving that input unchecked.

Describe what the application should do, where that rule comes from, and what it currently does instead. The requirement might be an approved acceptance criterion, a documented API contract, or a decision from the person responsible for the behavior. If the report and the requirement disagree, settle that disagreement before generating assertions.

Keep the expected result independent of the code being checked. [VS Code's guidance for testing existing code](https://code.visualstudio.com/docs/agents/guides/test-code-with-ai?ref=locallabs-llm.org) warns that inferring every expectation from the implementation can preserve an existing bug. A test that calculates its expected value by calling the same faulty function can agree with the bug.

Consider this constructed example: an upload validator must accept files from 1 through 4,096 bytes, inclusive. An empty file and a file larger than 4,096 bytes must be rejected. The reported defect is rejection at exactly 4,096 bytes. Those rules define this example; they aren't suggested limits for your application.

| Example input   | Required assertion                      |
| --------------- | --------------------------------------- |
| 4,096-byte file | Accepted, including the upper boundary. |
| 4,097-byte file | Rejected for exceeding the limit.       |
| 1-byte file     | Accepted at the lower boundary.         |
| Empty file      | Rejected under the stated contract.     |

The first case targets the defect. The others protect nearby behavior from a careless correction. Use the real assertion your function or API supports: a returned value, an error type, a response field, or an observable state change. “The function was called” doesn't establish that the upload was accepted correctly.

## Give the assistant the existing test setup

Use a workspace and assistant approved for this repository. Remove credentials, production records, and sensitive log values from the material you share. Run checks in the project's permitted development or test environment with disposable data; review commands that could write records, contact services, or incur charges before allowing them.

In VS Code's Chat view, start a new chat and select **[Copilot](https://github.com/features/copilot?ref=locallabs-llm.org)** as the **Session Target** and **Agent** in the agent picker. Attach the relevant source file, the requirement, and a representative test file. [Inspect the framework and test commands first](https://code.visualstudio.com/docs/agents/guides/test-code-with-ai?ref=locallabs-llm.org#%5Fidentify-the-testing-setup), including the runtime or interpreter, working directory, environment setup, and runner options.

Reuse the project's test framework, fixtures, and helpers. When no test setup exists, ask for a setup proposal and review it separately before installing anything. Don't let a request for one regression test silently introduce a second runner.

Run the relevant existing tests as a baseline once you've reviewed the command and its environment. Record pre-existing failures and skipped tests. An old failure shouldn't be presented as a consequence of the new test.

Providing the right test file has a concrete research basis. In [SWT-Bench's test-context experiment](https://arxiv.org/html/2406.12952v3?ref=locallabs-llm.org#S5.SS3), [ZeroShotPlus](https://arxiv.org/html/2406.12952v3?ref=locallabs-llm.org#S5.SS3) with [GPT-4](https://openai.com/index/gpt-4-research/?ref=locallabs-llm.org) achieved an 8.1% success rate when the test file was unspecified and 15.1% when it was supplied. Both conditions used retrieved source files without a supplied fix patch. The 172-case subset excluded cases where an incorrect applicable patch couldn't be generated after up to five resamples.

Success in that experiment required at least one generated test to fail before the reference fix and pass afterward, with none failing after the fix. This is a result from one study's configuration, rather than a ranking of current assistants. My practical choice is to supply a relevant existing test so the assistant can follow the real fixtures and assertions.

## Review the cases before asking for test code

Replace the bracketed fields in this prompt with the real files, behavior, and permissions. Keep the approved expected result visible beside the proposal.

```
Propose regression test cases for [one reported defect]. Don't edit files yet.

Requirement and its authority: [approved rule and source].
Faulty behavior: [minimal input or steps, actual result].
Expected behavior: [explicit result and relevant side effects].
Relevant source: [real path and symbol].
Representative existing test: [real path].
Known baseline failures or skips: [actual record, or not yet run].

For each case, give the input or precondition, the observable assertion,
and the incorrect behavior it should detect. Include the reported case
and useful nearby valid and invalid cases. Keep expected values independent
of the implementation. Identify unspecified behavior and duplicate coverage.
Explain which cases belong at unit, integration, or UI level and why.
Stop for review before generating code or running commands.
```

Ask the assistant to point to the assertion that would distinguish the faulty behavior from the agreed behavior. A test that merely finishes without throwing, checks a mock's configured response, or accepts any nonempty result may miss the reported defect.

For the upload example, checking only that the return value exists would miss an incorrect rejection. The assertion must distinguish acceptance from rejection at the exact limit. If the contract doesn't define a missing file, a negative size, or a storage failure, ask the maintainer to decide; the assistant shouldn't invent those expectations.

I would keep a unit test when the defect is entirely in the size comparison. If the defect occurs while one component passes the size to another, retain a test across that boundary. Mocking away the broken interaction would remove the very behavior you need to check.

## Add only the agreed tests and inspect the failure

After approving the cases, ask for tests using the existing framework, with implementation files unchanged. Review the diff before allowing the focused run. Instructions guide an agent, but [review checkpoints still require checking its edits and output](https://code.visualstudio.com/docs/agents/guides/test-driven-development-guide?ref=locallabs-llm.org#%5Fkeep-a-review-checkpoint-between-phases); a tests-only sentence doesn't enforce a file permission boundary.

```
Add only the agreed regression tests using the existing framework and helpers.
Keep expected values explicit. Don't change implementation code, install
dependencies, skip tests, or refactor unrelated files.

Show the diff and the focused test command for review. After that command
is approved, run it in the agreed test environment. Report the exact command,
selected tests, actual counts, skips, and failure output. Explain whether
the failure reaches the intended assertion and exposes the reported behavior.
Don't fix the implementation or weaken assertions. Stop for review.
```

Read the actual failure output. [The failing-test checkpoint](https://code.visualstudio.com/docs/agents/guides/test-driven-development-guide?ref=locallabs-llm.org#%5Fred-add-and-run-a-failing-test) calls for a failure caused by the missing behavior, rather than an unrelated import, dependency, or configuration error. Confirm the test imports the intended implementation and reaches the relevant operation.

For the constructed upload case, the useful failure would show that the 4,096-byte input was rejected where acceptance was required. “Module not found” would leave that assertion untested. Fix the setup problem separately, then rerun before assessing the defect.

Keep the faulty-version result attached to the test change. If the bug was already fixed, a passing test alone won't show that it detects the original defect. Where feasible, apply the same test to an isolated checkout of the affected revision, or recreate the documented precondition in disposable test data. Preserve your working changes and keep production out of this check. If the original condition can't be reproduced, record that gap instead of claiming a demonstrated regression test.

## Investigate a surprising result before changing the assertion

A new test can pass because the behavior is already correct, because the input misses the defect, or because the assertion checks too little. Check which version and code path ran. Don't break correct code to manufacture a red result.

A failure also needs a diagnosis. Compare the expected value with the approved requirement, then inspect imports, fixtures, environment, and the relevant operation. Keep an implementation defect separate from an incorrect expectation or a test setup problem. An unavailable service doesn't establish that the validator's comparison is wrong.

For a browser test, [Playwright recommends checking user-visible behavior](https://playwright.dev/docs/best-practices?ref=locallabs-llm.org#test-user-visible-behavior) and controlling test data and dependencies. Use awaited, retrying assertions for the expected UI state. Repeatedly adding fixed delays or letting the assistant repair a locator won't establish whether the business behavior is correct.

In VS Code, Test Explorer needs an appropriate testing extension; terminal execution doesn't require that UI integration. Open the Testing view from the beaker icon. If a new test is missing, check discovery and the filter, then use **Refresh Tests**. [The filter can restrict the displayed tests by name or status](https://code.visualstudio.com/docs/debugtest/testing?ref=locallabs-llm.org#%5Fautomatic-test-discovery-in-testing-view), so the visible list alone doesn't tell you what the last command selected.

[![VS Code Test Explorer toolbar and highlighted test filter](https://storage.ghost.io/c/e0/70/e070c514-431e-4a20-aa04-a641625f58e7/content/images/2026/11/a078-vscode-test-filter.webp)](https://storage.ghost.io/c/e0/70/e070c514-431e-4a20-aa04-a641625f58e7/content/images/2026/11/a078-vscode-test-filter.webp?ref=locallabs-llm.org)

Check the filter before deciding a new test is missing. Use Refresh Tests to update discovery after adding tests. [Open the full-size controls](https://storage.ghost.io/c/e0/70/e070c514-431e-4a20-aa04-a641625f58e7/content/images/2026/11/a078-vscode-test-filter.webp?ref=locallabs-llm.org).

If Test Explorer passes while the terminal fails, compare their runtime, working directory, environment, runner options, and selected tests before editing assertions. Start with the known working command and the actual failure output.

## Keep the assertion stable while reviewing the fix

Once the regression test exposes the intended defect, request the smallest implementation correction as a separate step. Preserve the agreed expected values, existing assertions, and nearby behavior. If an assertion conflicts with the approved requirement, resolve that conflict explicitly before changing it.

Rerun the targeted test, then the related suite using the project's normal configuration. Keep required security, integrity, and other release checks in place. A quick focused pass helps isolate the correction; it doesn't replace the checks your project requires.

Review test-file changes alongside implementation changes. Deleted assertions, new skips, an expected value changed to match the bug, or a mock that replaces the behavior under test can explain a green result without correcting the defect. Check the actual counts and selected tests, including a command that reports no tests.

## Use coverage to find gaps, then inspect what the tests detect

Line coverage shows executed code. It doesn't establish that the assertions would catch an incorrect result. [PIT's explanation of mutation testing](https://pitest.org/?ref=locallabs-llm.org) makes the distinction explicit: it inserts changes into the implementation and checks whether tests detect them by failing.

If mutation testing is already part of your project, inspect a relevant surviving mutation for a possible missing assertion or case. A survivor needs investigation; it doesn't automatically identify a real application bug. There's no useful universal percentage that proves this particular defect is covered.

For this task, I would prioritize the defect-specific evidence: the approved requirement, the exact input, an assertion that distinguishes the faulty result, and the observed before-and-after runs. Coverage can then help choose the next behavior to examine.

## Leave a record another developer can check

Keep the requirement, affected version and local changes, test path and name, reviewed assertion, and before-and-after output with the change. Record exact commands, environments, selected tests, pass/fail counts, skips, and anything that couldn't run. Separate the assistant's explanation from the evidence in the runner's output.

A useful handoff makes the remaining gap visible: perhaps the unit case passes but the integration environment was unavailable. Keep that gap in the review decision. For the implementation request that follows, [write a bounded coding brief with the expected behavior and constraints](https://locallabs-llm.org/brief-an-ai-coding-assistant/) so the correction stays tied to the test you've approved.