DeepEval: Write Unit Tests for LLMs Like You Write Tests for Code
DeepEval integrates with pytest to give LLM responses the same test coverage discipline as regular code - hallucination checks, bias detection, and CI-gated quality gates.
When you ship a software change, tests catch regressions. When you change a prompt or swap a model, there is typically nothing to catch quality regressions. DeepEval fixes this by turning LLM quality checks into pytest tests that run in CI exactly like unit tests.
Installation
pip install deepeval
deepeval login # Optional: connect to Confident AI dashboard
Team workspace
Ship faster with chat, meetings, and projects in one place — Zlyqor.
import pytest
from deepeval import assert_test
from deepeval.test_case import LLMTestCase
from deepeval.metrics import AnswerRelevancyMetric, HallucinationMetric
def test_rag_answer_quality():
test_case = LLMTestCase(
input="What causes hallucinations in LLMs?",
actual_output="LLMs hallucinate because they predict likely tokens without a fact-checking mechanism.",
retrieval_context=[
"Hallucinations in LLMs occur due to the probabilistic nature of token prediction...",
"Unlike databases, LLMs have no retrieval fallback when they lack information...",
],
)
relevancy_metric = AnswerRelevancyMetric(threshold=0.8, model="gpt-4o-mini")
hallucination_metric = HallucinationMetric(threshold=0.5, model="gpt-4o-mini")
assert_test(test_case, [relevancy_metric, hallucination_metric])
Run with:
deepeval test run test_llm.py
This integrates seamlessly with pytest - you get pass/fail output and the full pytest ecosystem (fixtures, parametrize, markers).
G-Eval: Custom LLM-as-Judge Metric
G-Eval lets you define a custom evaluation criterion in natural language:
from deepeval.metrics import GEval
from deepeval.test_case import LLMTestCaseParams
conciseness = GEval(
name="Conciseness",
criteria="The response answers the question without unnecessary padding or repetition.",
evaluation_params=[LLMTestCaseParams.INPUT, LLMTestCaseParams.ACTUAL_OUTPUT],
threshold=0.7,
)
G-Eval uses chain-of-thought prompting to produce a calibrated 0 - 1 score for any custom criterion.
DeepEval generates adversarial inputs, runs them against your model, and reports which attacks succeeded. Use this before launching a new model version.
If any metric drops below its threshold, the CI step fails and the PR is blocked - the same discipline that prevents software regressions applied to LLM quality.
Confident AI Dashboard
Sync results to Confident AI cloud for historical metric trending, regression alerts, and team-level dashboards without running your own infrastructure.
Practical deep-dives on LLMs, developer tools, and AI engineering. No filler. Unsubscribe any time.
// written byFIG. AUTH-01
530
Mahmudul Haque Qudrati
CEO & ML Engineer
CEO and ML Engineer at Pristren. Builds AI-powered software for teams and writes about machine learning, LLMs, developer tools, and practical AI applications.
SWE-Bench: The Gold Standard for Evaluating LLM Software Engineering
SWE-Bench tests LLMs on 2,294 real GitHub issues from popular Python repositories, evaluating whether the model can write code that passes the existing test suite - a far harder and more realistic evaluation than HumanEval.