返回顶部
热门问答 更多热门问答
技术文章 更多技术文章

8分钟打造一个DeepSeek API智能测试引擎:当咖啡还没凉,测试报告已出炉

[复制链接]
链载Ai 显示全部楼层 发表于 半小时前 |阅读模式 打印 上一主题 下一主题
最近忙完Jira cloud xray SDK的开发,稍微空了一丢丢的时间,趁下班时间,快速分享一个LLM全自动化API测试的场景,希望抛砖引玉,拿回家(厂)里能用到。相信很多人都有同感,过去很多花了几千块票钱(当然是公司的钱)去听的AI大会,多数时间是听了个寂寞,听的LLM赋能测试的讲座基本都是看看就好,很难落地到自己家。8分钟系列争取能做到开箱即用,所有的测试工程师能享用的LLM才是好的LLM,主打享受工作,保护发际线。
一、序章:当"人工智障"遇见"人工智勇"

ingFang SC", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, "Helvetica Neue", Helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", "Source Han Sans CN", sans-serif;font-optical-sizing: inherit;font-size-adjust: inherit;font-kerning: inherit;font-feature-settings: inherit;font-variation-settings: inherit;vertical-align: baseline;word-break: break-word;overflow-wrap: break-word;color: rgba(0, 0, 0, 0.9);letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(252, 252, 252);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">"小王,这个订单接口的异常测试用例写完了吗?"
"老大,我刚测到第38个场景,电脑就蓝屏了..."
"那你的测试覆盖率多少?"
"大概...覆盖了咖啡杯大小的区域?"

ingFang SC", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, "Helvetica Neue", Helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", "Source Han Sans CN", sans-serif;font-optical-sizing: inherit;font-size-adjust: inherit;font-kerning: inherit;font-feature-settings: inherit;font-variation-settings: inherit;vertical-align: baseline;word-break: break-word;overflow-wrap: break-word;color: rgba(0, 0, 0, 0.9);letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(252, 252, 252);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">这个经典对话揭示了传统API测试的三大痛点:测试工程师的头发越来越少、咖啡越喝越苦、而测试覆盖率永远像薛定谔的猫——测了但又没完全测。但今天,我要分享我的一个小实验:夸张地讲用DeepSeek-R1打造的智能测试引擎,能在你喝完一杯咖啡的时间里(甚至还没等到咖啡凉),完成从测试设计到执行的全流程自动化!

ingFang SC", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, "Helvetica Neue", Helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", "Source Han Sans CN", sans-serif;font-optical-sizing: inherit;font-size-adjust: inherit;font-kerning: inherit;font-feature-settings: inherit;font-variation-settings: inherit;vertical-align: baseline;word-break: break-word;overflow-wrap: break-word;color: rgba(0, 0, 0, 0.9);letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(252, 252, 252);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">先看Demo, 该平台对这样一个根据邮编返回城市的api进行全自动化测试设计和执行

ingFang SC", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, "Helvetica Neue", Helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", "Source Han Sans CN", sans-serif;font-optical-sizing: inherit;font-size-adjust: inherit;font-kerning: inherit;font-feature-settings: inherit;font-variation-settings: inherit;vertical-align: baseline;word-break: break-word;overflow-wrap: break-word;color: rgba(0, 0, 0, 0.9);letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(252, 252, 252);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">api.zippopotam.us,我们可以看到,几乎是0体力投入,最重要的是几乎不会引起脱发。


ingFang SC", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, "Helvetica Neue", Helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", "Source Han Sans CN", sans-serif;vertical-align: baseline;color: rgba(0, 0, 0, 0.9);letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(252, 252, 252);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">二、极速搭建:8分钟烹饪测试满汉全席

ingFang SC", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, "Helvetica Neue", Helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", "Source Han Sans CN", sans-serif;vertical-align: baseline;color: rgba(0, 0, 0, 0.9);letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(252, 252, 252);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">

ingFang SC", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, "Helvetica Neue", Helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", "Source Han Sans CN", sans-serif;vertical-align: baseline;color: rgba(0, 0, 0, 0.9);letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(252, 252, 252);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">2.1 食材准备(0-2分钟)

ingFang SC", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, "Helvetica Neue", Helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", "Source Han Sans CN", sans-serif;font-optical-sizing: inherit;font-size-adjust: inherit;font-kerning: inherit;font-feature-settings: inherit;font-variation-settings: inherit;vertical-align: baseline;word-break: break-word;overflow-wrap: break-word;color: rgba(0, 0, 0, 0.9);letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(252, 252, 252);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">就像煮泡面需要开水,我们的智能测试厨房需要:

之前很多人私信问我LLM返回的json有时候加载

不出来,今天分享个秘密武器给大家,

主打一用一个不吱声 - json_repair

# 魔法原料清单
importstreamlitasst # 可视化平底锅
importrequests # 网络电磁炉
fromjson_repairimportrepair_json # JSON修复铲

ingFang SC", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, "Helvetica Neue", Helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", "Source Han Sans CN", sans-serif;font-optical-sizing: inherit;font-size-adjust: inherit;font-kerning: inherit;font-feature-settings: inherit;font-variation-settings: inherit;vertical-align: baseline;word-break: break-word;overflow-wrap: break-word;color: rgba(0, 0, 0, 0.9);letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(252, 252, 252);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">这时候你的电脑会说:"主人,检测到您要拯救发际线,已自动屏蔽所有社交软件。"

ingFang SC", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, "Helvetica Neue", Helvetica, Arial, "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", "Source Han Sans CN", sans-serif;vertical-align: baseline;color: rgba(0, 0, 0, 0.9);letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;background-color: rgb(252, 252, 252);text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">2.2 核心配方(2-5分钟)

我们的秘密武器是这位"测试大厨":

classDeepSeekTestGenerator:
defgenerate_tests(self,api_desc:str)->dict:
# 此处隐藏着召唤测试神龙的咒语
payload={
"model":"deepseek-r1",
"messages":[
{"role":"system","content":self.system_prompt},
{"role":"user","content":api_desc}
],
"temperature":0.3# 控制大厨的"火候"
}


这个类就像《中华小当家》里的传说厨具,你给它一段API描述,它能吐出:

  • 正常场景测试(像老奶奶过马路般温柔)
  • 边界值测试(像甲方改需求般刁钻)
  • 异常参数测试(像程序员写bug般富有创造力)

2.3 自动化流水线(5-8分钟)

测试执行引擎是个不知疲倦的"测试狂人":

python

classTestExecutor:
defexecute_test(self,test_case:dict)->dict:
try:
# 这里上演着每秒百万级的请求风暴
response=requests.request(
method=test_case["method"],
url=f"{self.base_url}{test_case['path']}",
params=test_case.get("params"),
timeout=5
)
# 自动诊断报告生成系统
passed_assertions.append({
"type":assertion["type"],
"passed":passed,
"expected":assertion.get("expect"),
"actual":response.status_code
})

这个引擎有多强悍?它能在执行测试的同时:

  1. 监测响应时间(精确到毫秒级)
  2. 自动验证JSON数据结构(比丈母娘查户口还细致)
  3. 生成可视化报告(比美颜相机还懂怎么展示优点)

三、黑科技解剖室:代码的九阳神功


3.1 强化学习测试生成(图灵附体)

我们的prompt设计暗藏玄机:

python

system_prompt="""作为API测试专家,请按以下要求生成测试套件:
1. 包含正常/边界/异常场景
2. 使用JSON Path验证响应
3. 包含性能断言(响应时间<800ms)
4. 输出OpenAPI 3.0规范"""

这相当于给AI安装了一个"测试人格芯片",让它瞬间拥有:

  • 十年测试老兵的场景覆盖能力
  • 福尔摩斯的异常发现直觉
  • 奥运短跑运动员的性能要求

3.2 自愈型JSON处理(医学奇迹)

returnjson.loads(repair_json(response.json()["choices"][0]["message"]["content"]))

这个repair_json就像测试界的"急救医生",能:

  • 自动修复缺失的引号(治疗强迫症)
  • 矫正错位的括号(骨科手术级精准)
  • 处理编码问题(堪比同声传译)

3.3 智能断言引擎(审判者协议)

ifassertion["type"]=="json_path":
passed=self._validate_json_path(response.json(),assertion)

这个断言系统严格得就像高考阅卷老师:

  • 检查数据是否存在("你的id呢?拿出来看看!")
  • 验证数据类型("说好的字符串怎么变数字了?")
  • 核对数据范围("年龄200岁?你是吸血鬼吗?")

四、可视化战场:测试结果的时装秀


当传统测试报告还在用Word文档时,我们的系统已经办起了"数据时装周":

withst.expander("查看详情"):
col1,col2=st.columns(2)
withcol1:
st.metric("响应时间",f"{response_time:.2f}ms")
st.write("### 断言结果")
forassertioninassertions:
icon="✅"ifpassedelse"❌"
withcol2:
st.json(response_sample)


这个界面能:

  • 用红绿灯系统展示测试结果(比交通信号灯还直观)
  • 展示实时响应数据(像直播带货般即时)
  • 生成交互式图表(比股票K线图还专业)

五、未来展望:测试工程师的"摸鱼"计划


当这个系统完善后(目前只是个简单的POC):

  1. 测试工程师的日常将变为:喝咖啡 → 看仪表盘 → 点"重新测试"按钮 → 继续喝咖啡
  2. CI/CD管道将实现真正的"无人驾驶",Git提交即触发智能测试
  3. 人类终于可以从重复劳动中解放,专注于更重要的任务——给AI写的测试用例写注释

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

链载AI是专业的生成式人工智能教程平台。提供Stable Diffusion、Midjourney AI绘画教程,Suno AI音乐生成指南,以及Runway、Pika等AI视频制作与动画生成实战案例。从提示词编写到参数调整,手把手助您从入门到精通。
  • 官方手机版

  • 微信公众号

  • 商务合作

  • Powered by Discuz! X3.5 | Copyright © 2025-2025. | 链载Ai
  • 桂ICP备2024021734号 | 营业执照 | |广西笔趣文化传媒有限公司|| QQ