Skip to content
QAcademy — QAcademy

AI risks ·

Testing LLM Applications: Where to Start

By The QAcademy team

Illustration for the article "Testing LLM Applications: Where to Start"

Your team has just shipped an LLM-powered assistant inside the product, and someone asks: "Can you test it?" You reach for your usual testing habits and nothing fits: the same question gets two different answers, and neither matches an expected result word for word.

This article gives you a concrete starting point for testing LLM applications: what changes, what stays the same, and a first strategy you can put in place this week.

Testing LLM applications: what really changes

A classic test rests on one assumption: same input, same output. With an LLM, that assumption breaks. The answer can vary from one call to the next, even with identical data, and two different wordings can both be acceptable.

Three practical consequences:

  • The test oracle changes nature. You no longer compare an exact value, you check properties: "the answer cites a source", "the JSON matches the schema", "no contract number from another customer appears".
  • One test is no longer enough, you need statistics. A case that passes once proves little; what matters is the pass rate over several runs.
  • The scope widens. The model is only one part of the system: there is also the prompt, the data fed to it (for example through RAG), the tools the model can call, and the code that uses its answer.

What does not change is the approach. You identify risks, rank them, write tests where impact is highest, and automate what repeats.

Step 1: write down what "correct" means

Before writing a test, put your expectations in writing. For each feature that uses the LLM, answer four questions:

  1. What is a good answer? (accurate, useful, right tone, right format)
  2. What is an unacceptable answer? (invented facts, data leak, advice outside the scope)
  3. Which mistake costs the most? A style slip does not weigh the same as an invented commercial promise.
  4. What should the system do when it does not know? Decline, ask a question, hand over to a human.

Imagine an assistant that answers insurance customers' questions about reimbursements (illustrative scenario). A good answer cites the right coverage; an unacceptable one invents a reimbursement ceiling; and when in doubt, the assistant should route to an advisor. Those three sentences are already the start of a test plan.

Step 2: test properties, not sentences

Instead of comparing exact text, write checks on what the answer must or must not contain. They fall into three families:

Family Example check Typical tool
Structure The JSON matches the schema, required fields are present Schema validation, plain assertions
Content The answer mentions the right contract clause; no amount absent from the supplied context Rules, regular expressions, comparison with the context
Quality Tone is professional, the answer is consistent with its source Human review, or a second model used as a judge (to be validated)

Here is a short TypeScript example with Vitest. The askAssistant function is fictional: it stands for the call into your application.

import { describe, it, expect } from "vitest";
import { askAssistant } from "./assistant"; // hypothetical: calls your application

describe("reimbursement assistant", () => {
  it("only quotes amounts present in the supplied context", async () => {
    const context = "Optical coverage: €150 per year.";
    const answer = await askAssistant("How much do I get back for glasses?", context);

    // Extract every euro amount from the answer
    const amounts = [...answer.matchAll(/€\s?(\d+)|(\d+)\s?€/g)].map((m) => m[1] ?? m[2]);

    // Property: every amount quoted must exist in the context
    for (const amount of amounts) {
      expect(context).toContain(amount);
    }
  });
});

This test does not check that the answer is well written; it checks that it does not invent a figure. That is exactly the kind of property you can automate.

Step 3: measure over several runs

Because output varies, a single pass says almost nothing. Run the same case N times and track the pass rate. Then set a threshold per risk: you may tolerate zero failures on "no data from another customer" and accept less on style.

// Replays a case several times and returns the share of successful runs
async function passRate(runs: number, check: () => Promise<boolean>): Promise<number> {
  let passed = 0;
  for (let i = 0; i < runs; i++) {
    if (await check()) passed++;
  }
  return passed / runs;
}

Keep a small evaluation set: a few dozen real or realistic cases (frequent questions, edge cases, trick questions), each with its expected property. Rerun it whenever the prompt, the model or the data changes. It is your safety net, just like a classic regression test suite.

Step 4: cover the risks specific to LLMs

Beyond functionality, a few risks show up in almost every LLM application:

  • Hallucinations: the model confidently states something false. We covered how to spot and limit them.
  • Prompt injection: malicious text (typed by a user or hidden in a document) pushes the model to ignore its instructions. The OWASP project lists it among the major risks for LLM applications: see the OWASP Top 10 for LLM Applications.
  • Data leakage: the model reveals information it received in its context and should not have disclosed.
  • Unwanted actions: if the model can call tools (send an email, edit a record), test what happens when the request is ambiguous or hostile.
  • Drift over time: a new model version or new data can change behaviour without a single line of your code moving.

For an overview of managing these risks, the NIST Generative AI Profile (AI 600-1) is a good starting reference.

Step 5: keep an eye on production

Your evaluation set will never cover everything real users will type. So plan for:

  • logging of exchanges, in line with data protection rules (anonymise or limit what you keep);
  • a simple way for users to report a bad answer;
  • a regular review of a sample of real exchanges, from which you draw new cases for your evaluation set.

Key takeaways

  • With an LLM, you test properties and pass rates, not an exact output.
  • Start by describing what a correct answer is, what an unacceptable one is, and what the system should do when unsure.
  • A small evaluation set replayed on every change beats a big one-off campaign.
  • Test the whole system (prompt, data, tools, code), not only the model.
  • Pre-release tests are not enough: monitor and learn from real exchanges.

FAQ

How do I test an application that uses ChatGPT or another LLM?

First define the expected properties of each answer, write automated checks on those properties, then replay each case several times to measure a pass rate. Add a human review of a sample.

Can you automate testing of an AI chatbot?

Yes for structure, some content rules and regression on a fixed set of cases. Tone and relevance often still need a human eye, or a second model used as a judge whose verdicts you validate yourself.

Why do my tests fail every other run with an LLM?

Because the output varies. A test that compares exact text is too fragile: replace it with a property check and measure the pass rate across several runs.

Sources

What's next?

Want to know where you stand on testing fundamentals before diving into generative AI? Take the free diagnostic: 10 questions, about 5 minutes. And if you prefer to get new articles by email, QAcademy sends a weekly newsletter (at most one email a week, one-click unsubscribe).

Take the next step

You have just read. Now see where you stand: 10 questions, your score chapter by chapter, free and without signing up.

Take the free test (5 min)

Already decided to practise?

One-off payment, no subscription, refunded within 14 days if it is not for you.

Get the new articles by email

At most one email a week: the new QAcademy articles on software testing and quality. Free, unsubscribe in one click.

5 minutes · free · no sign-up

Before you go: where do you stand?

10 questions in the exam format, about 5 minutes: your score by chapter and your gap to the pass mark.

5 minutes · free · no sign-up