核心内容摘要
Qwen3-ASR-1.7B语音识别模型:从零开始快速入门
在前面的章节中分别介绍了 Web、App、接口自动化测试用例的生成。
但是在前文中实现的效果均为在控制台打印自动化测试的用例。
用例需要手动粘贴调整之后再执行。
那么其实这个手动粘贴、执行的过程也是可以直接通过人工智能完成的。
应用价值通过人工智能代替人工操作的部分节省时间提升效率。
通过封装更多的 Tools让 Agent 更为智能。
实践演练
实现原理实现思路在理解需求之后我们可以了解到我们需要让 Agent 具备两个功能
输入源码信息生成 python 文件。
输入文件名执行 pytest 测试文件功能。
如此可以通过如下两个步骤实现需求
工具包封装。
实现 Agent。
工具包封装为了让工具包更易被大模型理解我们将注释调整为英文提升准确率。
同时为了传参的时候不出现格式错误问题通过args_schema限制参数结构与格式tools 章节有具体讲解。
from langchain_core.tools import tool from pydantic.v1 import BaseModel, Field class PythonFileInput(BaseModel): # 定义参数的描述 filename: str Field(descriptionfilename) source_code: str Field(descriptionsource code data) class PytestFileName(BaseModel): # 定义参数的描述 filename: str Field(descriptionThe name of the file to be executed) tool(args_schemaPythonFileInput) def write_file(filename, source_code): Generate python files based on input source code with open(filename, w) as f: f.write(source_code) tool(args_schemaPytestFileName) def execute_test_file(filename): Pass in the file name, execute the test case and return the execution result import subprocess # 使用subprocess模块执行pytest命令 result subprocess.run([pytest, filename], capture_outputTrue, textTrue) # 检查pytest的执行结果 if result.returncode 0: print(测试运行成功) else: print(测试运行失败) print(result.stdout) return result.stdout通过 AGENT 实现需求
首先封装 Agent绑定工具输入提示词。
在示例中是在 LangChain 官方提供的 structured-chat-agent提示词基础之上修改的提示词添加了一个code变量。
目的是为了后面 code 可以由其他的 chain 的执行结果而来。
# 注意需要再原提示词的基础上添加 {code} 变量 # prompt hub.pull(hwchase17/structured-chat-agent) llm ChatOpenAI() agent1 create_structured_chat_agent(llm, tools_all, prompt) agent_executor AgentExecutor( agentagent1, toolstools_all, verboseTrue, return_intermediate_stepsTrue, handle_parsing_errorsTrue) if __name__ __main__: agent_executor.invoke({input: 请根据以上源码生成文件, code: def test_demo(): return True})由以上的步骤即可生成一个源码文件
在生成源码文件后可以继续补充提示词要求Agent 执行对应的测试用例if __name__ __main__: agent_executor.invoke({input: 请根据以下步骤完成我让你完成操作没有完成所有步骤不能停止:
先根据以上源码生成文件。
根据上一步生成的源码文件进行执行测试用例操作并返回终的执行结果 , code: def test_demo(): return True})到这里通过 Agent 就能自动生成测试用例文件执行测试用例了。
与其他的场景结合在前面的章节中已经实现了自动生成接口自动化测试用例的操作。
可以直接与前面的操作结合自动生成接口自动化测试用例并执行测试用用例。
注意load_case 如何实现在前面章节《基于LangChain手工测试用例转接口自动化测试生成工具》已有对应讲解# load_case 的返回结果是接口的自动化测试用例 chain ( RunnablePassthrough.assign(codeload_case) | agent1 ) agent_executor AgentExecutor( agentchain, toolstools_all, verboseTrue, return_intermediate_stepsTrue, handle_parsing_errorsTrue) if __name__ __main__: agent_executor.invoke({input: 请根据以下步骤完成我让你完成操作没有完成所有步骤不能停止:
先根据以上源码生成文件。
根据上一步生成的源码文件进行执行测试用例操作并返回终的执行结果 })执行之后即可在控制台看到生成的接口自动化测试用例的执行记录。
总结自动化测试用例的生成与执行的
实现原理。
自动化测试用例的生成与执行的实现思路。
利用 Agent 实现自动化测试用例的生成与执行。
感谢每一个认真阅读我文章的人礼尚往来总是要有的虽然不是什么很值钱的东西如果你用得到的话可以直接拿走这些资料对于【软件测试】的朋友来说应该是最全面最完整的备战仓库这个仓库也陪伴上万个测试工程师们走过最艰难的路程希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取