《计算机应用》唯一官方网站 ›› 2026, Vol. 46 ›› Issue (10): 3298-3306.DOI: 10.11772/j.issn.1001-9081.2025091085

• 计算机软件技术 • 上一篇    

基于路径约束分片驱动大语言模型的单元测试生成方法

徐晓龙1, 王俊峰1,2(), 吴鹏3, 曹先省4   

  1. 1.四川大学 计算机学院,成都 610065
    2.视觉合成图形图像技术国防重点学科实验室(四川大学),成都 610065
    3.四川旅游学院 人工智能学院,成都 610100
    4.四川大学 网络空间安全学院,成都 610207
  • 收稿日期:2025-09-19 修回日期:2025-09-30 接受日期:2025-10-11 发布日期:2025-10-30 出版日期:2026-10-10
  • 通讯作者: 王俊峰
  • 作者简介:徐晓龙(2000—),男,新疆乌鲁木齐人,硕士研究生,主要研究方向:大语言模型、测试用例自动生成
    吴鹏(1982—),男,四川成都人,讲师,博士,主要研究方向:软件供应链安全、软件测试
    曹先省(1991—),男,山东菏泽人,博士研究生,主要研究方向:源代码脆弱性分析、深度学习。
  • 基金资助:
    国家自然科学基金资助项目(U24B20147);国家自然科学基金资助项目(U2133208);四川省重点研发计划项目(2024ZHCG0195);四川省重点研发计划项目(2024ZDZX0044);四川省重点研发计划项目(2024ZYD0269)

Unit test generation method based on path constraint sharding driven large language model

Xiaolong XU1, Junfeng WANG1,2(), Peng WU3, Xiansheng CAO4   

  1. 1.College of Computer Science,Sichuan University,Chengdu Sichuan 610065,China
    2.National Key Laboratory of Fundamental Science on Synthetic Vision (Sichuan University),Chengdu Sichuan 610065,China
    3.School of Artificial Intelligence,Sichuan Tourism University,Chengdu Sichuan 610100,China
    4.School of Cyber Science and Engineering,Sichuan University,Chengdu Sichuan 610207,China
  • Received:2025-09-19 Revised:2025-09-30 Accepted:2025-10-11 Online:2025-10-30 Published:2026-10-10
  • Contact: Junfeng WANG
  • About author:XU Xiaolong, born in 2000, M. S. candidate. His research interests include large language models, automated test case generation.
    WU Peng, born in 1982, Ph. D., lecturer. His research interests include software supply chain security, software testing.
    CAO Xiansheng, born in 1991, Ph. D. candidate. His research interests include source code vulnerability analysis, deep learning.
  • Supported by:
    National Natural Science Foundation of China(U24B20147);Key Research and Development Program of Sichuan Province(2024ZHCG0195)

摘要:

自动化单元测试生成是现代化软件开发提升开发效率、确保软件质量保障的关键,大语言模型(LLM)因具备良好的代码理解能力被应用于测试用例自动生成,但在处理复杂函数时面临深层分支路径难覆盖等问题。因此,本文提出PyULLM方法,将路径约束分片与LLM的生成能力有机结合以解决上述难题。具体地:通过前序遍历抽象语法树,系统化收集所有的路径约束;基于此,建立细粒度的代码行?路径约束映射关系;根据得到的映射关系,对路径约束集合进行智能化分片。这种分片处理机制使LLM能聚焦于特定路径约束,可以显著提升复杂场景下生成单元测试用例的覆盖率。实验结果表明,相较于SOTA(State Of The Arts)工具Pynguin,PyULLM的行覆盖率(LC)和分支覆盖率(BC)分别提升了24.16和26.61个百分点;相较于当前先进的CodaMosa方法,PyULLM的LC和BC分别提升了19.06和21.72个百分点。可见,PyULLM能为复杂函数有效生成单元测试用例。

关键词: 单元测试生成, 大语言模型, 路径约束, 测试与分析, 测试覆盖率

Abstract:

Automated unit test generation is the key to modern software development to improve development efficiency and ensure software quality. Large Language Models (LLMs) are applied to automated test case generation because of their good code understanding ability. However, when dealing with complex functions, it is difficult for LLMs to cover deep branch paths. Therefore, PyULLM method was proposed, which combined path constraint sharding with LLM generation ability to solve the above problems. Specifically, all the path constraints were collected systematically by preorder traversal of the abstract syntax tree. On this basis, a fine-grained relationship between code lines and path constraints was established. According to the obtained relationship, intelligent partitioning of path constraint set was performed. LLM was able to focus on specific path constraints by this partitioning mechanism, which improved the coverage of generated unit test cases in complex scenarios significantly. Experimental results show that compared with the SOTA(State Of The Arts) tool Pynguin, PyULLM method improves the Line Coverage ratio (LC) by 24.16 percentage points and the Branch Coverage ratio (BC) by 26.61 percentage points; compared with the current SOTA method CodaMosa, PyULLM method has the LC improved by 19.06 percentage points, and the BC increased by 21.72 percentage points. It can be seen that PyULLM method can generate unit test cases for complex functions effectively.

Key words: unit test generation, Large Language Model (LLM), path constraint, testing and analysis, test coverage

中图分类号: