7. 质量与测试
7.1 提示词单元测试
测试框架
提示词单元测试是确保AI系统质量的关键环节。我们使用专门的测试框架来测试Prompt的效果。
测试框架架构
测试框架实现
# Prompt测试框架
class PromptTestFramework:
def __init__(self):
self.test_cases = []
self.results = []
def add_test_case(self, test_case):
"""添加测试用例"""
self.test_cases.append(test_case)
def run_tests(self, prompt_template):
"""运行测试"""
for test_case in self.test_cases:
# 执行Prompt
result = self._execute_prompt(prompt_template, test_case)
# 评估结果
evaluation = self._evaluate_result(result, test_case)
# 记录结果
self.results.append({
"test_case": test_case,
"result": result,
"evaluation": evaluation
})
return self.results
def _execute_prompt(self, prompt_template, test_case):
"""执行Prompt"""
# 填 充Prompt模板
prompt = prompt_template.format(**test_case["input"])
# 调用模型
response = llm_client.generate(prompt)
return response
def _evaluate_result(self, result, test_case):
"""评估结果"""
# 使用评估器评估结果
evaluator = test_case.get("evaluator", "default")
if evaluator == "exact_match":
return result == test_case["expected_output"]
elif evaluator == "semantic_similarity":
return self._semantic_similarity(result, test_case["expected_output"])
elif evaluator == "custom":
return test_case["custom_evaluator"](result)
return False
测试用例设计
测试用例类型
- 功能测试:测试Prompt的基本功能是否正常
- 边界测试:测试边界情况,如空输入、超长输入等
- 异常测试:测试异常情况的处理
- 性能测试:测试Prompt的执行性能
测试用例示例
# 测试用例定义
test_cases = [
{
"name": "简单问答测试",
"type": "functional",
"input": {
"question": "什么是光合作用?",
"grade": "初中",
"subject": "生物"
},
"expected_output": {
"contains": ["光合作用", "植物", "光能"],
"length_range": [100, 500]
},
"evaluator": "semantic_similarity",
"threshold": 0.8
},
{
"name": "边界测试-空问题",
"type": "boundary",
"input": {
"question": "",
"grade": "初中",
"subject": "生物"
},
"expected_output": {
"error_handled": True,
"error_message": "问题不能为空"
},
"evaluator": "custom"
},
{
"name": "性能测试",
"type": "performance",
"input": {
"question": "什么是光合作用?",
"grade": "初中",
"subject": "生物"
},
"expected_output": {
"max_response_time": 2.0 # 秒
},
"evaluator": "performance"
}
]
自动化测试
自动化测试流程
CI/CD集成
# GitHub Actions配置示例
name: Prompt Tests
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.11'
- name: Install dependencies
run: |
pip install -r requirements.txt
- name: Run prompt tests
run: |
python -m pytest tests/prompt_tests/ -v
- name: Generate test report
run: |
pytest --html=report.html --self-contained-html
7.2 端到端自动化
E2E测试流程
E2E测试场景
- 学习路径推荐流程:从学习者登录到获得学习路径的完整流程
- 智能答疑流程:从提问到获得回答的完整流程
- 作业批改流程:从提交作业到获得批改结果的完整流程
- 学习效果评估流程:从学习数据收集到生成评估报告的完整流程
E2E测试实现
# E2E测试示例
import pytest
from playwright.sync_api import Page, expect
def test_qa_flow(page: Page):
"""测试智能答疑流程"""
# 1. 登录
page.goto("https://education.example.com/login")
page.fill("#username", "student_001")
page.fill("#password", "password123")
page.click("#login-button")
# 2. 进入答疑页面
page.click("#qa-menu")
# 3. 输入问题
page.fill("#question-input", "什么是光合作用?")
page.click("#submit-button")
# 4. 等待回答
answer = page.wait_for_selector("#answer-content", timeout=10000)
# 5. 验证回答
expect(answer).to_contain_text("光合作用")
expect(answer).to_contain_text("植物")
# 6. 验证回答质量
answer_text = answer.inner_text()
assert len(answer_text) > 100, "回答太短"
assert len(answer_text) < 1000, "回答太长"