Writing tests is a time-consuming yet essential task during software development. We propose to leverage recent advances in deep learning for text and code generation to assist developers in writing tests. We formalize the novel task of test completion to automatically complete the next statement in a test method based on the context of prior statements and the code under test. We develop TeCo -- a deep learning model using code semantics for test completion. The key insight underlying TeCo is that predicting the next statement in a test method requires reasoning about code execution, which is hard to do with only syntax-level data that existing code completion models use. TeCo extracts and uses six kinds of code semantics data, including the execution result of prior statements and the execution context of the test method. To provide a testbed for this new task, as well as to evaluate TeCo, we collect a corpus of 130,934 test methods from 1,270 open-source Java projects. Our results show that TeCo achieves an exact-match accuracy of 18, which is 29% higher than the best baseline using syntax-level data only. When measuring functional correctness of generated next statement, TeCo can generate runnable code in 29% of the cases compared to 18% obtained by the best baseline. Moreover, TeCo is significantly better than prior work on test oracle generation.
翻译:在软件开发过程中,写作测试是一项耗时但必不可少的任务。 我们提议利用最近深入学习文本和代码生成的进展,协助开发者进行写作测试。 我们正式确定测试完成的新任务,以便根据先前的语句和正在测试的代码,以测试方法自动完成下一个语句。 我们开发了Teco -- -- 一个使用代码语义的深度学习模型,用于测试完成。 TeCo 的关键洞察力是,在测试方法中预测下一个语句要求对代码执行进行推理,而这只涉及现有代码完成模型所使用的语句级数据。 TeCo 提取并使用六种代码语义数据,包括先前语句的执行结果和测试方法的执行背景。为这一新任务提供一个测试台,以及评估TeCo,我们从1 270个开放源爪哇项目收集了130,934个测试方法。我们的结果显示, TeCo只使用语系级别数据比最佳基线高29%。 当测量生成下一个语句的语句的功能准确度时,TeCo可以比先前生成的18 % 测试案例要大大地进行。