Testing RAG Applications: A Practical Guide
A retrieval-augmented generation system has two places to fail — retrieval and generation. A layered test strategy that isolates each, from unit tests to end-to-end evaluation.
- Published
- Reading time
- 2 min read
- Categories
- Testing · LLM Evaluation
On this page
Retrieval-augmented generation (RAG) combines a search step with a generation step: relevant context is retrieved from a knowledge source and passed to a model that writes the answer. When an answer is wrong, the first question is always where it went wrong. A good test strategy makes that question easy to answer.
Test the layers separately
A RAG pipeline is usually a chain of stages, each of which can be tested on its own:
- Ingestion — parsing, cleaning, and chunking documents.
- Indexing — embedding chunks and storing them with metadata.
- Retrieval — finding the most relevant chunks for a query.
- Generation — producing an answer grounded in the retrieved context.
Testing only the final answer hides which stage failed. Testing each stage localises problems and keeps most tests fast and deterministic.
Ingestion and chunking: ordinary unit tests
Chunking logic is plain code and deserves plain unit tests:
def test_chunks_respect_max_tokens():
chunks = chunk_document(SAMPLE_DOC, max_tokens=200, overlap=20)
assert all(count_tokens(c.text) <= 200 for c in chunks)
def test_chunks_keep_source_metadata():
chunks = chunk_document(SAMPLE_DOC, max_tokens=200, overlap=20)
assert all(c.metadata["source"] == SAMPLE_DOC.path for c in chunks)Also check the unglamorous cases: empty documents, tables, very long lines, and non-English text.
Retrieval: measure before you generate
Retrieval quality can be evaluated without calling a generation model at all. Build a small set of queries, each labelled with the chunks or documents that should be retrieved, then measure:
- Recall@k — how often the relevant chunk appears in the top k results.
- Precision@k — how much of the top k is actually relevant.
- Rank of the first relevant result — whether the best evidence is near the top.
These metrics make retrieval changes — a new embedding model, a different chunk size, hybrid search — comparable against a baseline.
Generation: grounded and faithful
With retrieval held fixed, evaluate whether answers use the provided context correctly:
- Faithfulness: every claim in the answer is supported by the retrieved context.
- Answer relevance: the answer addresses the question that was asked.
- Abstention: when the context does not contain the answer, the system says so instead of guessing.
The abstention case is easy to forget and important to test. Include questions whose answers are deliberately absent from the knowledge source.
End-to-end tests: few, focused, representative
End-to-end evaluations run the full pipeline on realistic questions. They are slower and noisier, so keep the set focused: core user journeys, known past failures, and high-risk topics. Record the retrieved context alongside each answer so failures can be traced back to a stage.
Make it part of CI
- Run unit tests for ingestion and chunking on every commit.
- Run retrieval metrics when indexing or retrieval code changes.
- Run generation and end-to-end evaluations when prompts, models, or retrieval settings change, and compare against the current baseline.
Summary
Treat RAG as a pipeline, not a black box. Deterministic stages get conventional tests, retrieval gets ranking metrics, generation gets faithfulness and abstention checks, and a small end-to-end suite ties it together. When something breaks, the layered results tell you where to look.