惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
I
InfoQ
U
Unit 42
WordPress大学
WordPress大学
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
J
Java Code Geeks
月光博客
月光博客
D
Docker
Stack Overflow Blog
Stack Overflow Blog
D
DataBreaches.Net
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
V
Visual Studio Blog
博客园 - 聂微东
A
About on SuperTechFans
腾讯CDC
Jina AI
Jina AI
Microsoft Azure Blog
Microsoft Azure Blog
GbyAI
GbyAI
博客园 - 【当耐特】
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
M
MIT News - Artificial intelligence

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
How to Test LLM-Powered Applications Effectively
Yash Pandey · 2026-04-26 · via DEV Community

How to Test LLM-Powered Applications Effectively


Testing a CRUD app is deterministic. You input X, you expect Y, you assert equality. Testing an LLM-powered application is different in a way that breaks most of your existing instincts.

The model's output is probabilistic. The same prompt can return different phrasing across runs. "Correct" is often subjective. Traditional assertEqual doesn't work here.

Here's how to think about testing LLM apps properly.


The Three Layers of an LLM App

Before writing a single test, map out what you're actually testing:

[ User Input ]
     ↓
[ Prompt Construction ]   ← Layer 1: Deterministic. Testable normally.
     ↓
[ LLM API Call ]          ← Layer 2: Non-deterministic. Mock in unit tests.
     ↓
[ Output Parsing ]        ← Layer 3: Deterministic. Testable normally.
     ↓
[ App Response ]

Enter fullscreen mode Exit fullscreen mode

Most bugs aren't in the LLM — they're in layers 1 and 3. Start there.


Layer 1: Test Prompt Construction

Your prompt builder is plain code. Test it like code.

def build_prompt(user_query: str, context: str) -> str:
    return f"""You are a helpful assistant.
Context: {context}
User: {user_query}
Answer concisely."""

def test_prompt_includes_context():
    prompt = build_prompt("What is the policy?", "Refund window is 30 days.")
    assert "Refund window is 30 days." in prompt

def test_prompt_has_system_instruction():
    prompt = build_prompt("Hi", "")
    assert "You are a helpful assistant" in prompt

Enter fullscreen mode Exit fullscreen mode

These are fast, free, and catch the majority of regressions.


Layer 2: Test Output Parsing

If your app parses structured data from LLM output, test the parser independently with canned responses:

def parse_llm_json_response(raw: str) -> dict:
    import json, re
    match = re.search(r'\{.*\}', raw, re.DOTALL)
    if not match:
        raise ValueError("No JSON found in response")
    return json.loads(match.group())

def test_parser_extracts_json():
    raw = "Here is the result: {\"score\": 8, \"reason\": \"Clear\"}"
    result = parse_llm_json_response(raw)
    assert result["score"] == 8

def test_parser_raises_on_no_json():
    with pytest.raises(ValueError):
        parse_llm_json_response("Sorry, I cannot help with that.")

Enter fullscreen mode Exit fullscreen mode


Layer 3: Evaluating LLM Output Quality

For actual model output, shift from assertion-based testing to evaluation-based testing. Three practical approaches:

1. Rubric Scoring (LLM-as-Judge)

def evaluate_response(question, answer, criteria):
    eval_prompt = f"""
Rate the following answer on a scale of 1-5 for each criterion.
Question: {question}
Answer: {answer}
Criteria: {criteria}
Return JSON: {{"score": int, "reason": str}}
"""
    # Call your LLM here and parse response
    ...

Enter fullscreen mode Exit fullscreen mode

2. Semantic Similarity (for factual tasks)

from sentence_transformers import SentenceTransformer, util

model = SentenceTransformer('all-MiniLM-L6-v2')

def is_semantically_similar(expected, actual, threshold=0.85):
    emb1 = model.encode(expected, convert_to_tensor=True)
    emb2 = model.encode(actual, convert_to_tensor=True)
    score = util.cos_sim(emb1, emb2).item()
    return score >= threshold

Enter fullscreen mode Exit fullscreen mode

3. Behavioral Testing (what should never happen)

FORBIDDEN_PHRASES = ["I cannot", "As an AI", "I don't have access"]

def test_no_refusals_on_valid_queries(llm_client):
    response = llm_client.ask("What is the return policy?")
    for phrase in FORBIDDEN_PHRASES:
        assert phrase not in response, f"Got refusal: {phrase}"

Enter fullscreen mode Exit fullscreen mode


Testing for Regressions: Golden Datasets

Build a golden dataset — a curated set of input/expected-output pairs — and run evaluations on every model or prompt change:

Input Min Score Pass?
"Summarize this in 3 points" 4/5
"Translate to French" 4/5
"What's 2+2?" (sanity check) 5/5

This won't catch everything, but it will catch regressions — which is the main goal.


Tools Worth Knowing

  • Promptfoo — open-source LLM eval framework, define test cases in YAML
  • LangSmith — tracing + eval if you're on LangChain

- DeepEval — pytest-style assertions for LLM metrics

Written by Yash| Senior SDET catching failures other layers miss — cross-validating UI, API, DB simultaneously and test infrastructure.