
Catch LLM Regressions Before Your Users Do: Unit Testing AI Outputs with deepeval
Chris Harper
2 min read
Aug 4, 2026 · 20:05 UTC
deepeval brings pytest-style unit testing to LLM outputs — write a threshold assertion, run it in CI, catch regressions before they ship.
What you'll be able to do after this:
- Write LLM unit tests that assert on answer quality, faithfulness, and hallucination — not just string equality
- Apply 50+ research-backed metrics (GEval, answer relevancy, contextual faithfulness) to any Claude or OpenAI output
- Wire deepeval into CI so quality regressions fail the build before reaching production
Why testing LLM outputs matters
Once you've built a RAG system or agent, the hardest part isn't getting it to work — it's knowing when it stopped. A model upgrade, a prompt tweak, or a retrieval change can silently degrade answers. deepeval catches those regressions with the same discipline you apply to unit tests.
Install
pip install -U deepeval
Write your first test (test_agent.py)
from deepeval import assert_test
from deepeval.test_case import LLMTestCase, SingleTurnParams
from deepeval.metrics import GEval, AnswerRelevancyMetric
def test_answer_quality():
# Call your LLM/agent here and capture the output
output = my_agent.answer("What is the capital of France?")
test_case = LLMTestCase(
input="What is the capital of France?",
actual_output=output,
expected_output="Paris"
)
correctness = GEval(
name="Correctness",
criteria="Is the actual output factually correct based on the expected output?",
evaluation_params=[SingleTurnParams.ACTUAL_OUTPUT, SingleTurnParams.EXPECTED_OUTPUT],
threshold=0.8
)
relevancy = AnswerRelevancyMetric(threshold=0.7)
assert_test(test_case, [correctness, relevancy])
Run it
deepeval test run test_agent.py
The output shows per-metric scores, reasons for failures, and a pass/fail verdict. If actual_output scores below the threshold, the test fails — exactly like a pytest assertion.
Add RAG-specific metrics
from deepeval.metrics import FaithfulnessMetric, ContextualRelevancyMetric
test_case = LLMTestCase(
input="What's our return policy?",
actual_output=rag_output,
retrieval_context=retrieved_chunks # the passages from your vector store
)
assert_test(test_case, [
FaithfulnessMetric(threshold=0.8), # output doesn't hallucinate beyond context
ContextualRelevancyMetric(threshold=0.7) # retrieved context was relevant to the question
])
Wire into CI
# .github/workflows/eval.yml
- name: Run LLM eval
run: deepeval test run tests/
Any test falling below its threshold breaks the build. Add deepeval login to push results to Confident AI for trend tracking across runs — useful for catching slow quality drift that no single test-run exposes.
Sources: deepeval Getting Started · deepeval — The LLM Evaluation Framework · Using DeepEval for LLM Evaluation in Python — Codecademy