Skip to content

业务测试用例智能生成



业务用例生成的 5 种策略

  • L1 初级:基于提示词生成
  • L2 中级:基于 RAG+提示词生成
  • L3 高级:基于混合搜索+提示词生成
  • L4 资深:基于探索学习的生成方法
  • L5 专家:基于图谱生成

基本概念


文档加载器

\

DocumentLoaders 将数据加载为标准 LangChain Document 格式。


向量存储

\

向量存储是专门的数据存储,能够基于向量表示进行索引和检索信息。


向量检索

\

计算用户的 query 与每段文本的相似度,阈值使用 0-1 表示相似度


工作流编排框架 LangGraph


LLM 结构化输出

class TestCaseGeneratorModel(BaseModel):
    # 文件来源 网址 本地文件
    paths: List[str] = Field(default_factory=list)
    # 任务
    task: Optional[str] = None

    # 上下文
    context: Optional[str] = None

    suite: SuiteModel = Field(
        default=None,
        description='带有测试用例列表的测试套件'
    )

class SuiteModel(BaseModel):
    name: Optional[str] = Field(None, description='测试套件的名字')
    testcases: List[TestCaseModel] = Field(
        default_factory=list,
        description='测试用例列表,一个测试套件可以包含多个测试用例'
    )

class TestCaseModel(BaseModel):
    name: str
    description: str
    steps: List[Step] = Field(
        default_factory=list,
        description='测试用例的详细步骤,一般包含多个详细的动作步骤。'
    )

class Step(BaseModel):
    action: str = Field(description='动作包含执行动作与断言动作。')

提示词参考

rag_prompt_template = ChatPromptTemplate(
    [
        (
            'system',
            dedent(
                """
                你是一名软件测试专家,你非常擅长分析需求,并编写测试用例。
                你擅长分析用户的需求文档,并根据需求进行测试用例编写。
                你可以识别出不同的需求场景,并根据情况给出相应的正向测试用例和反向测试用例。
                你可以识别复杂流程中的关键输入项,并根据数据类型进行等价类和边界值测试。
                如果测试用例的步骤中存在多个选项的输入数据,为每种选项生成对应的测试用例。
                所有的测试用例彼此之间相互独立。
                如果需要测试数据,请提前给出准备说明。
                尽量给出能完整覆盖需求流程的测试用例集合。

                始终以中文回复。
                """
            ).strip()
        ),
        (
            'user',
            dedent(
                """
                任务: {task},
                根据用户的任务进行检索,并给出相应的参考内容。
                """
            ).strip()
        ),
        (
            'ai',
            '检索内容如下:'
            '{context}'
        ),
        (
            'user',
            dedent(
                """
                {task}
                """
            ).strip()
        )

    ]

)

基于提示词生成

uml diagram


代码示例

def test_case_gen_by_prompt():
    """
    基于提示词的测试用例生成
    :return:
    """
    graph = StateGraph(RAGTestCaseModel)

    graph.add_sequence(nodes=[load_file, llm_structured])
    graph.set_entry_point('load_file')
    workflow = graph.compile()
    r = workflow.invoke(
        RAGTestCaseModel(
            task='针对过滤议题列表需求,编写完整的测试用例集。',
            paths=[
                'https://gitlab.cn/docs/jh/user/project/issues/managing_issues.html'
            ]

        )
    )

    rag_testcase_model = RAGTestCaseModel()
    for key, value in r.items():
        setattr(rag_testcase_model, key, value)
    debug(rag_testcase_model.model_dump_json(indent=2))

提示词方案的特点

  • 优点:
    • 大模型可以完全理解文档的上下文
  • 缺点:
    • 受 LLM 上下文限制,无法应对多更大文档
    • 不相关的 token 太多会导致输出不可控。

基于 RAG 生成用例

uml diagram


基于 RAG 的用例生成代码示例

    graph = StateGraph(TestCaseGeneratorModel)

    graph.add_sequence(nodes=[rag_add, rag_search, llm_structured])
    graph.set_entry_point('rag_add')
    workflow = graph.compile()
    r = workflow.invoke(
        TestCaseGeneratorModel(
            task='针对过滤议题需求,编写完整的测试用例套件。',
            paths=[
                'https://gitlab.cn/docs/jh/user/project/issues/managing_issues.html'
            ]
        )
    )

    rag_testcase_model = TestCaseGeneratorModel()
    for key, value in r.items():
        setattr(rag_testcase_model, key, value)
    debug(rag_testcase_model.model_dump_json(indent=2))

RAG 方案缺点

  • 优点:可以处理更大的文档
  • 缺点:RAG 的召回率依赖非常多的因素
    • 文档分段
    • 检索参数
    • 分词
    • 。。。

基于混合搜索+提示词生成


混合检索流程

uml diagram

\

利用更多搜索方法去完善补充 RAG 的不足


基于探索学习的生成方法 腾讯 AppAgent

\

参考 app 智能体相关章节


自动生成的文档示例

{.!grow-2}

{
    'tap': 'The UI element allows the user to select a city from the search results to add it to the world clock list.',
    'text': '', 'v_swipe': '', 'h_swipe': '', 'long_press': ''
}

基于图谱生成


基于图谱生成流程

uml diagram


图查询示例

    graph = StateGraph(TestCaseGeneratorModel)
    graph.add_sequence(nodes=[load_file, graph_document, graph_ask, llm_structured])
    graph.set_entry_point('load_file')
    workflow = graph.compile()
    r = workflow.invoke(
        TestCaseGeneratorModel(
            task='针对过滤议题需求,编写完整的测试用例集。',
            paths=[
                'https://gitlab.cn/docs/jh/user/project/issues/managing_issues.html'
            ]
        )
    )

    rag_testcase_model = TestCaseGeneratorModel()
    for key, value in r.items():
        setattr(rag_testcase_model, key, value)
    debug(rag_testcase_model.model_dump_json(indent=2))

相关技术

  • GraphRAG 基于图谱的检索增强生成
  • KG 知识图谱 Neo4J 图数据库
  • Crawler 智能遍历 爬虫
  • MBT 模型驱动测试

总结