Journal of Computer Applications ›› 2026, Vol. 46 ›› Issue (11): 3598-3606.DOI: 10.11772/j.issn.1001-9081.2025091177

• Computer software technology • Previous Articles    

Unit test case generation method based on path guidance and iterative optimization

Yukun DONG1,2,3(), Xiaoshan LIU1,2,3, Shuai LIU1,2,3, Xinran MENG1,2,3, Yunhao WEN4   

  1. 1.Qingdao Institute of Software and College of Computer Science and Technology,China University of Petroleum (East China),Qingdao Shandong 266580,China
    2.Shandong Key Laboratory of Intelligent Oil and Gas Industrial Software (China University of Petroleum (East China)),Qingdao Shandong,266580,China
    3.State Key Laboratory for Novel Software Technology (Nanjing University),Nanjing Jiangsu 210023,China
    4.PetroChina Planning and Engineering Institute,Beijing 100089,China
  • Received:2025-10-11 Revised:2025-12-10 Accepted:2025-12-15 Online:2025-12-26 Published:2026-11-10
  • Contact: Yukun DONG
  • About author:LIU Xiaoshan, born in 2001, M. S. candidate. Her research interests include intelligent software engineering.
    LIU Shuai, born in 2000, M. S. candidate. His research interests include intelligent software engineering.
    MENG Xinran, born in 2002, M. S. candidate. Her research interests include intelligent software engineering.
    WEN Yunhao, born in 1985, M. S. His research interests include intelligent software engineering.
  • Supported by:
    Shandong Provincial Natural Science Foundation(ZR2024MF129);Open Project of State Key Laboratory of Novel Software Technology at Nanjing University(KFKT2025B80);Graduate Course Development Project of China University of Petroleum (East China)(UPCYZH-2025-13)

基于路径引导与迭代优化的单元测试用例生成方法

董玉坤1,2,3(), 刘笑杉1,2,3, 刘帅1,2,3, 孟欣然1,2,3, 文韵豪4   

  1. 1.中国石油大学(华东) 青岛软件学院、计算机科学与技术学院,山东 青岛 266580
    2.山东省智能油气工业软件重点实验室(中国石油大学(华东)),山东 青岛 266580
    3.计算机软件新技术全国重点实验室(南京大学),南京 210023
    4.中国石油规划总院,北京 100089
  • 通讯作者: 董玉坤
  • 作者简介:刘笑杉(2001—),女,山东德州人,硕士研究生,CCF会员,主要研究方向:智能化软件工程
    刘帅(2000—),男,山东枣庄人,硕士研究生,主要研究方向:智能化软件工程
    孟欣然(2002—),女,山东济南人,硕士研究生,主要研究方向:智能化软件工程
    文韵豪(1985—),男,四川德阳人,硕士,主要研究方向:智能化软件工程。
  • 基金资助:
    山东省自然科学基金资助项目(ZR2024MF129);南京大学计算机软件新技术全国重点实验室开放课题(KFKT2025B80);中国石油大学(华东)研究生课程建设项目(UPCYZH-2025-13)

Abstract:

Large Language Models (LLMs) perform well at automatically generating unit test cases, but exhibit insufficient coverage of complex structures and critical paths, resulting in gaps in both coverage and accuracy. To address this issue, a path guidance and iterative optimization strategy was proposed by integrating Control Flow Graph (CFG) analysis with C-P-R (Context-Prompt-Response) template to enhance path guidance during test case generation. In addition, a dual-verification mechanism was introduced to improve the reliability of the generated test cases, while iterative optimization was employed to progressively fill coverage gaps. Experiments were conducted across multiple models and task scenarios, with systematic evaluation on key metrics, including statement coverage, branch coverage, and assertion accuracy. The results showed that the proposed strategy achieved an average statement coverage of 97% and a branch coverage of 94%, with strong adaptability and stability across different models. These findings indicate that combining path guidance with iterative optimization effectively enhances the coverage and reliability of test case generation, offering a feasible approach for automated testing and verification of complex software systems.

Key words: Large Language Model (LLM), unit test case generation, path guidance, iterative optimization, Control Flow Graph (CFG) analysis

摘要:

大语言模型(LLM)自动生成单元测试用例的效果较好,但对复杂结构和关键路径覆盖不足,生成结果的覆盖率与准确性仍有待提升。针对该问题,本文提出一种路径引导与迭代优化策略,通过结合控制流图(CFG)分析与C-P-R(Context-Prompt-Response)模板,强化测试用例生成过程中的路径引导能力;在此基础上,引入双重验证机制提升测试用例的可靠性,并通过迭代优化逐步弥补覆盖盲区。在多种模型与任务场景下进行实验,并评估语句覆盖率、分支覆盖率及断言准确率等关键指标。结果显示,本文策略的平均语句覆盖率达到97%,分支覆盖率达到94%,且在不同模型中均表现出较好的适应性与稳定性。路径引导与迭代优化的结合能有效提升测试用例生成的覆盖水平与可靠性,为复杂软件系统的自动化测试验证提供可行的思路。

关键词: 大语言模型, 单元测试用例生成, 路径引导, 迭代优化, 控制流图分析

CLC Number: