Skip to main content
QUICK REVIEW

[论文解读] Unit Test Case Generation with Transformers and Focal Context

Michele Tufano, Dawn Drain|arXiv (Cornell University)|Sep 11, 2020
Software Engineering Research参考文献 29被引用 8
一句话总结

该论文提出AthenaTest,一种基于序列到序列的深度学习方法,通过在真实Java方法及其对应测试用例的大规模平行语料上微调基于BART的Transformer模型,生成人类可读的单元测试用例。该方法仅用30次尝试便实现了43.7%的焦点方法覆盖率,在测试用例正确性和开发者偏好方面优于GPT-3,同时在测试覆盖率方面与EvoSuite相当。

ABSTRACT

Automated unit test case generation tools facilitate test-driven development and support developers by suggesting tests intended to identify flaws in their code. Existing approaches are usually guided by the test coverage criteria, generating synthetic test cases that are often difficult for developers to read or understand. In this paper we propose AthenaTest, an approach that aims to generate unit test cases by learning from real-world focal methods and developer-written testcases. We formulate unit test case generation as a sequence-to-sequence learning task, adopting a two-step training procedure consisting of denoising pretraining on a large unsupervised Java corpus, and supervised finetuning for a downstream translation task of generating unit tests. We investigate the impact of natural language and source code pretraining, as well as the focal context information surrounding the focal method. Both techniques provide improvements in terms of validation loss, with pretraining yielding 25% relative improvement and focal context providing additional 11.1% improvement. We also introduce Methods2Test, the largest publicly available supervised parallel corpus of unit test case methods and corresponding focal methods in Java, which comprises 780K test cases mined from 91K open-source repositories from GitHub. We evaluate AthenaTest on five defects4j projects, generating 25K passing test cases covering 43.7% of the focal methods with only 30 attempts. We execute the test cases, collect test coverage information, and compare them with test cases generated by EvoSuite and GPT-3, finding that our approach outperforms GPT-3 and has comparable coverage w.r.t. EvoSuite. Finally, we survey professional developers on their preference in terms of readability, understandability, and testing effectiveness of the generated tests, showing overwhelmingly preference towards AthenaTest.

研究动机与目标

  • 解决现有自动化测试工具中机器生成的单元测试可读性和可理解性差的问题。
  • 通过学习真实开发者编写的测试用例和焦点方法,提升测试用例生成质量。
  • 探究在自然语言和源代码上进行预训练,以及引入焦点上下文,对测试生成质量的影响。
  • 构建并发布Methods2Test,这是目前公开可用的最大规模监督平行语料库,包含Java测试用例及其对应的焦点方法。
  • 与EvoSuite和GPT-3等最先进工具相比,评估生成测试用例的有效性、覆盖率和开发者偏好。

提出的方法

  • 将单元测试生成建模为基于Transformer模型的序列到序列学习任务。
  • 采用两阶段训练:首先在大规模无监督Java源代码和英文文本语料上进行去噪预训练,然后在测试生成任务上进行监督微调。
  • 引入焦点上下文——即焦点方法周围的上下文代码——以提升生成质量。
  • 利用Methods2Test数据集,该数据集包含从91K个GitHub仓库中挖掘出的78万对测试用例,用于监督微调。
  • 应用迁移学习技术,将预训练的BART模型适配到测试用例生成任务。
  • 通过执行测试、测量行覆盖率和条件覆盖率,并开展开发者调查,评估生成的测试用例的可读性和可理解性。

实验结果

研究问题

  • RQ1在自然语言和源代码语料上进行预训练,对生成的单元测试用例质量有何影响?
  • RQ2引入焦点上下文在多大程度上提升了生成测试用例的质量和覆盖率?
  • RQ3AthenaTest在测试覆盖率和正确性方面与GPT-3和EvoSuite相比表现如何?
  • RQ4专业开发者如何看待AthenaTest生成的测试用例与EvoSuite生成的测试用例在可读性和可理解性方面的差异?
  • RQ5微调后的Transformer模型能否生成既正确又与人工编写的代码难以区分的测试用例?

主要发现

  • 在自然语言和源代码语料上进行预训练,相比非预训练模型,验证损失降低了25%。
  • 在仅预训练的基础上引入焦点上下文,验证损失额外降低了11.1%。
  • AthenaTest在仅30次尝试内,成功为五项Defects4j项目中的43.7%的焦点方法生成了通过的测试用例。
  • AthenaTest生成的测试用例在测试覆盖率方面与EvoSuite相当,例如在isNumber方法上覆盖了51行代码和41个条件,且在正确性和覆盖率方面优于GPT-3。
  • 开发者调查结果显示,AthenaTest生成的测试用例在可读性和可理解性方面显著优于EvoSuite,开发者更偏好AthenaTest的输出。
  • AthenaTest成功生成了正确且符合习惯用法的测试用例,能够覆盖焦点方法中的所有条件,例如正确地断言空返回值和有效值的创建。

更好的研究,从现在开始

从阅读论文到最终审阅,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。