大模型应用之基于Langchain的测试用例生成
本文作者通过对AI大模型以及Langchain开源框架的了解,讲述了利用其智能生成测试用例的解决方案,达到提高测试用例编写效率的效果。通过本文可以了解大模型技术在测试用例设计过程中的应用方法以及技术实现方案。
【本文目录】
用例生成实践效果
细节介绍
效果展示
待解决问题及后续计划
在组内的日常工作安排中,持续优化测试技术、提高测试效率始终是重点任务。近期,我们在探索实践使用大模型生成测试用例,期望能够借助其强大的自然语言处理能力,自动化地生成更全面和高质量的测试用例。
(什么是LangChain? 它是一个开源框架,用于构建基于大型语言模型(LLM)的应用程序。LLM 是基于大量数据预先训练的大型深度学习模型,可以生成对用户查询的响应,例如回答问题或根据基于文本的提示创建图像。LangChain 提供各种工具和抽象,以提高模型生成的信息的定制性、准确性和相关性。例如,开发人员可以使用 LangChain 组件来构建新的提示链或自定义现有模板。LangChain 还包括一些组件,可让 LLM 无需重新训练即可访问新的数据集。)
def case_gen(prd_file_path, tdd_file_path, input_prompt, case_name):"""用例生成的方法参数:prd_file_path - prd文档路径tdd_file_path - 技术设计文档路径case_name - 待生成的测试用例名称"""# 解析需求、设计相关文档, 输出的是document列表prd_file = PDFParse(prd_file_path).load_pymupdf_split()tdd_file = PDFParse(tdd_file_path).load_pymupdf_split()empty_case = FilePath.read_file(FilePath.empty_case)# 将需求、设计相关文档设置给memory作为llm的记忆信息prompt = ChatPromptTemplate.from_messages([SystemMessage(content="You are a chatbot having a conversation with a human."), # The persistent system promptMessagesPlaceholder(variable_name="chat_history"), # Where the memory will be stored.HumanMessagePromptTemplate.from_template("{human_input}"), # Where the human input will injected])memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)for prd in prd_file:memory.save_context({"input": prd.page_content}, {"output": "这是一段需求文档,后续输出测试用例需要"})for tdd in tdd_file:memory.save_context({"input": tdd.page_content}, {"output": "这是一段技术设计文档,后续输出测试用例需要"})# 调大模型生成测试用例llm = LLMFactory.get_openai_factory().get_chat_llm()human_input = "作为软件测试开发专家,请根据以上的产品需求及技术设计信息," + input_prompt + ",以markdown格式输出测试用例,用例模版是" + empty_casechain = LLMChain(llm=llm,prompt=prompt,verbose=True,memory=memory,)output_raw = chain.invoke({'human_input': human_input})# 保存输出的用例内容,markdown格式file_path = FilePath.out_file + case_name + ".md"with open(file_path, 'w') as file:file.write(output_raw.get('text'))
def case_gen_by_vector(prd_file_path, tdd_file_path, input_prompt, table_name, case_name):"""!!!当文本超级大时,防止token不够,通过向量数据库,搜出某一部分的内容,生成局部的测试用例,细节更准确一些!!!参数:prd_file_path - prd文档路径tdd_file_path - 技术设计文档路径table_name - 向量数据库的表名,分业务存储,一般使用业务英文唯一标识的简称case_name - 待生成的测试用例名称"""# 解析需求、设计相关文档, 输出的是document列表prd_file = PDFParse(prd_file_path).load_pymupdf_split()tdd_file = PDFParse(tdd_file_path).load_pymupdf_split()empty_case = FilePath.read_file(FilePath.empty_case)# 把文档存入向量数据库docs = prd_file + tdd_fileembedding_model = LLMFactory.get_openai_factory().get_embedding()router_url = ConfigParse(FilePath.config_file_path).get_vearch_router_server()vearch_cluster = Vearch.from_documents(docs,embedding_model,path_or_url=router_url,db_name="y_test_qa",table_name=table_name,flag=1,)# 从向量数据库搜索相关内容docs = vearch_cluster.similarity_search(query=input_prompt, k=1)content = docs[0].page_content# 使用向量查询的相关信息给大模型生成用例prompt_template = "作为软件测试开发专家,请根据产品需求技术设计中{input_prompt}的相关信息:{content},以markdown格式输出测试用例,用例模版是:{empty_case}"prompt = PromptTemplate(input_variables=["input_prompt", "content", "empty_case"],template=prompt_template)llm = LLMFactory.get_openai_factory().get_chat_llm()chain = LLMChain(llm=llm,prompt=prompt,verbose=True)output_raw = chain.invoke({'input_prompt': input_prompt, 'content': content, 'empty_case': empty_case})# 保存输出的用例内容,markdown格式file_path = FilePath.out_file + case_name + ".md"with open(file_path, 'w') as file:file.write(output_raw.get('text'))
- END -