[论文解读] Unit Test Case Generation with Transformers
AthenaTest 提出了一种基于大规模真实世界 Java 方法-测试用例配对数据集微调的序列到序列 Transformer 模型,用于生成人类可读且语法正确的单元测试用例。该方法在测试覆盖率、可读性和测试有效性方面优于 EvoSuite 和 GPT-3,其有效性通过开发者调查和自动化评估指标得到验证。
Automated Unit Test Case generation has been the focus of extensive literature within the research community. Existing approaches are usually guided by the test coverage criteria, generating synthetic test cases that are often difficult to read or understand for developers. In this paper we propose AthenaTest, an approach that aims at generating unit test cases by learning from real-world, developer-written test cases. Our approach relies on a state-of-the-art sequence-to-sequence transformer model which is able to write useful test cases for a given method under test (i.e., focal method). We also introduce methods2test - the largest publicly available supervised parallel corpus of unit test case methods and corresponding focal methods in Java, which comprises 630k test cases mined from 70k open-source repositories hosted on GitHub. We use this dataset to train a transformer model to translate focal methods into the corresponding test cases. We evaluate the ability of our model in generating test cases using natural language processing as well as code-specific criteria. First, we assess the quality of the translation compared to the target test case, then we analyze properties of the test case such as syntactic correctness and number and variety of testing APIs (e.g., asserts). We execute the test cases, collect test coverage information, and compare them with test cases generated by EvoSuite and GPT-3. Finally, we survey professional developers on their preference in terms of readability, understandability, and testing effectiveness of the generated test cases.
研究动机与目标
- 为解决现有方法在自动化单元测试用例生成中缺乏真实感、可读性和有效性的问题。
- 通过使用真实开发者编写的测试用例进行训练,而非合成或基于覆盖率驱动的方法,以提升测试用例质量。
- 创建并发布 methods2test,这是目前公开可用的最大规模监督型 Java 方法-测试用例配对数据集,用于训练和评估。
- 使用自然语言处理和代码特定指标(包括语法正确性与 API 多样性)评估生成的测试用例。
- 从可读性、可理解性和测试有效性角度,评估开发者对生成测试用例的感知。
提出的方法
- 在包含 630k 个方法-测试用例配对示例的 methods2test 数据集上微调最先进的序列到序列 Transformer 模型。
- 通过将目标方法编码并以类似翻译的方式解码,使用训练好的模型生成测试用例。
- 使用 BLEU、ROUGE 和 BERTScore 评估生成测试用例与真实测试用例之间的语义相似度。
- 测量生成测试用例的语法正确性,以及所用测试 API(如断言)的数量和多样性。
- 执行生成的测试用例以测量代码覆盖率,并与 EvoSuite 和 GPT-3 的结果进行比较。
- 通过专业开发者调查,评估生成测试用例的可读性、可理解性和测试有效性。
实验结果
研究问题
- RQ1基于 Transformer 的模型在多大程度上能够生成在语义上与人工编写的测试用例相似的测试用例?
- RQ2与 EvoSuite 和 GPT-3 生成的测试用例相比,本方法生成的测试用例在语法正确性和 API 多样性方面表现如何?
- RQ3与基线工具相比,生成的测试用例实现了多高的代码覆盖率?
- RQ4专业开发者如何评价生成测试用例的可读性和可理解性?
- RQ5与现有方法相比,生成的测试用例在故障检测方面的有效性如何?
主要发现
- Transformer 模型与真实测试用例在语义上表现出高度相似性,BERTScore F1 分数表明其在语义层面具有良好的对齐性。
- 生成的测试用例展现出高语法正确性,并使用了多样化的测试 API,表明其具备鲁棒性和表达能力。
- 该模型在基准数据集上的代码覆盖率高于 EvoSuite,并与 GPT-3 相当或更优。
- 专业开发者在可读性和可理解性方面更偏好 AthenaTest 生成的测试用例,而非 EvoSuite 和 GPT-3 的结果。
- 根据开发者调查反馈,AthenaTest 生成的测试用例在故障检测方面被评价为更具有效性。
- methods2test 数据集显著提升了测试用例生成的质量,证明了真实世界训练数据的价值。
更好的研究,从现在开始
从阅读论文到最终审阅,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。