在前面的章節(jié)中悼嫉,分別介紹了 Web、App回还、接口自動化測試用例的生成裆泳。但是在前文中實現(xiàn)的效果均為在控制臺打印自動化測試的用例。用例需要手動粘貼柠硕,調(diào)整之后再執(zhí)行工禾。
那么其實這個手動粘貼、執(zhí)行的過程蝗柔,也是可以直接通過人工智能完成的闻葵。
應(yīng)用價值
- 通過人工智能代替人工操作的部分,節(jié)省時間癣丧,提升效率槽畔。
- 通過封裝更多的 Tools,讓 Agent 更為智能坎缭。
實踐演練
實現(xiàn)原理
image.png
實現(xiàn)思路
在理解需求之后竟痰,我們可以了解到我們需要讓 Agent 具備兩個功能:
- 輸入源碼信息,生成 python 文件掏呼。
- 輸入文件名坏快,執(zhí)行 pytest 測試文件功能。
如此憎夷,可以通過如下兩個步驟實現(xiàn)需求:
- 工具包封裝莽鸿。
- 實現(xiàn) Agent。
工具包封裝
為了讓工具包更易被大模型理解拾给,我們將注釋調(diào)整為英文祥得,提升準(zhǔn)確率。同時為了傳參的時候不出現(xiàn)格式錯誤問題蒋得,通過args_schema
限制參數(shù)結(jié)構(gòu)與格式(tools 章節(jié)有具體講解)级及。
from langchain_core.tools import tool
from pydantic.v1 import BaseModel, Field
class PythonFileInput(BaseModel):
# 定義參數(shù)的描述
filename: str = Field(description="filename")
source_code: str = Field(description="source code data")
class PytestFileName(BaseModel):
# 定義參數(shù)的描述
filename: str = Field(description="The name of the file to be executed")
@tool(args_schema=PythonFileInput)
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_schema=PytestFileName)
def execute_test_file(filename):
"""
Pass in the file name, execute the test case and return the execution result
"""
import subprocess
# 使用subprocess模塊執(zhí)行pytest命令
result = subprocess.run(['pytest', filename], capture_output=True, text=True)
# 檢查pytest的執(zhí)行結(jié)果
if result.returncode == 0:
print("測試運行成功!")
else:
print("測試運行失敹钛谩:")
print(result.stdout)
return result.stdout
通過 AGENT 實現(xiàn)需求
- 首先封裝 Agent饮焦,綁定工具,輸入提示詞窍侧。在示例中县踢,是在 LangChain 官方提供的
structured-chat-agent
提示詞基礎(chǔ)之上修改的提示詞,添加了一個code
變量伟件。目的是為了后面 code 可以由其他的 chain 的執(zhí)行結(jié)果而來硼啤。
# 注意:需要再原提示詞的基礎(chǔ)上添加 {code} 變量
# prompt = hub.pull("hwchase17/structured-chat-agent")
llm = ChatOpenAI()
agent1 = create_structured_chat_agent(llm, tools_all, prompt)
agent_executor = AgentExecutor(
agent=agent1, tools=tools_all,
verbose=True,
return_intermediate_steps=True,
handle_parsing_errors=True)
if __name__ == '__main__':
agent_executor.invoke({"input": "請根據(jù)以上源碼生成文件", "code": """def test_demo(): return True"""})
由以上的步驟,即可生成一個源碼文件:
image.png
- 在生成源碼文件后斧账,可以繼續(xù)補充提示詞谴返,要求Agent 執(zhí)行對應(yīng)的測試用例:
if __name__ == '__main__':
agent_executor.invoke({"input": """
請根據(jù)以下步驟完成我讓你完成操作煞肾,沒有完成所有步驟不能停止:
1. 先根據(jù)以上源碼生成文件。
2. 根據(jù)上一步生成的源碼文件嗓袱,進(jìn)行執(zhí)行測試用例操作扯旷,并返回終的執(zhí)行結(jié)果
""",
"code": """def test_demo(): return True"""})
image.png
到這里,通過 Agent 就能自動生成測試用例文件索抓,執(zhí)行測試用例了。
與其他的場景結(jié)合
在前面的章節(jié)中毯炮,已經(jīng)實現(xiàn)了自動生成接口自動化測試用例的操作逼肯。可以直接與前面的操作結(jié)合桃煎,自動生成接口自動化測試用例篮幢,并執(zhí)行測試用用例。
注意:load_case 如何實現(xiàn)在前面章節(jié):《基于LangChain手工測試用例轉(zhuǎn)接口自動化測試生成工具》为迈,已有對應(yīng)講解
# load_case 的返回結(jié)果是接口的自動化測試用例
chain = (
RunnablePassthrough.assign(code=load_case) | agent1
)
agent_executor = AgentExecutor(
agent=chain, tools=tools_all,
verbose=True,
return_intermediate_steps=True,
handle_parsing_errors=True)
if __name__ == '__main__':
agent_executor.invoke({"input": """
請根據(jù)以下步驟完成我讓你完成操作三椿,沒有完成所有步驟不能停止:
1. 先根據(jù)以上源碼生成文件。
2. 根據(jù)上一步生成的源碼文件葫辐,進(jìn)行執(zhí)行測試用例操作搜锰,并返回終的執(zhí)行結(jié)果
"""})
執(zhí)行之后,即可在控制臺看到生成的接口自動化測試用例的執(zhí)行記錄耿战。
image.png
總結(jié)
- 自動化測試用例的生成與執(zhí)行的實現(xiàn)原理蛋叼。
- 自動化測試用例的生成與執(zhí)行的實現(xiàn)思路。
- 利用 Agent 實現(xiàn)自動化測試用例的生成與執(zhí)行剂陡。