Testing features built on language models
By Denis Bojkovski, Test Automation Architect · Published November 25, 2026
What is different
A conventional feature given the same input returns the same output. A language model may not, and many answers may be acceptable. So the question "is it correct?" becomes "how often is it acceptable, for which inputs, and what happens when it is not?". The rest of testing still applies: most of the feature is ordinary software around a model.
Separate the parts
- Ordinary code: building the prompt, retrieving documents, calling the model, parsing, displaying, permissions, limits. Test it deterministically with a stubbed model.
- Model behaviour: quality, safety, tone. Evaluate it statistically.
Many reported "AI problems" are in the first part: the wrong document retrieved, a truncated prompt, a timeout shown as an empty answer.
Build an evaluation set
Collect 50 to 200 realistic inputs. Include:
- common requests, in real users' words;
- ambiguous ones, where asking back is right;
- ones it should refuse, or say it does not know;
- other languages, misspellings, very long texts;
- every failure reported so far.
For each, write what an acceptable answer must and must not contain. This is careful test design, and the hardest part. Keep it under version control, and free of personal data.
Check from cheap to expensive
| Check | Example |
|---|---|
| Structure | Valid JSON, required fields, length |
| Rules | Cites a supplied document, contains no email address |
| Exact facts | The amount equals the one in the source |
| Comparison with a reference | Same meaning as an approved answer |
| Model as judge | A second model scores against a rubric |
| Human review | A sample, by somebody who knows the subject |
A judging model makes mistakes too. Compare its verdicts with people's on a sample before relying on it, and recheck after changing either model.
Repeat, and count
Run each case several times. One failure in ten is a 10% failure rate, not a flaky test. Report rates by category rather than one average: 95% overall can hide 60% on refunds.
import { test } from 'node:test';
import assert from 'node:assert/strict';
// Share of runs of one case that satisfy a check.
export async function passRate(run, check, times = 10) {
let passed = 0;
for (let i = 0; i < times; i++) if (check(await run())) passed++;
return passed / times;
}
test('passRate counts acceptable answers', async () => {
const answers = ['30 days', '30 days', 'two weeks', '30 days'];
const rate = await passRate(async () => answers.shift(), (a) => a === '30 days', 4);
assert.equal(rate, 0.75);
});
Security risks
- Prompt injection: text in a document or message instructs the model. Put such instructions in test documents and check that they are not followed.
- Excess permission: what could the feature do at worst? It must not reach data or actions the user could not.
- Leakage: other customers' data, the system prompt, secrets.
- Unsafe output: generated markup or code rendered or executed without treatment.
OWASP publishes a Top 10 for applications using large language models; use it as a checklist. Follow the limits in security testing basics.
Regression
Run the evaluation when the prompt, model version, documents or settings change. Pin the model version where the provider allows it. Record all of these with each result, or you cannot explain a drop. Track cost and response time too: an accurate answer after twenty seconds may be a failure.
Deciding to release
Agree acceptable rates per category beforehand, and zero tolerance for a few outcomes such as exposing another customer's data. Plan for failure: can the user see the source, correct the result, or reach a person? Sample real use after release, with consent and privacy respected, and feed failures back into the set.