arXivDaily arXiv每日学术速递 周一至周五更新
arXiv周末暂无论文更新,休息一下吧,周末愉快~~
arXiv 2407.08138cs.SE
收藏

开发者如何构建单元测试用例?基于“AAA”视角的实证研究

How Do Developers Structure Unit Test Cases? An Empirical Study from the "AAA" Perspective

Chenhao Wei, Lu Xiao, Tingting Yu, Sunny Wong, Abigail Clune

AI总结:

本研究基于435个真实单元测试用例,实证分析了开发者遵循AAA模式的情况,发现了三种反模式和四种设计缺陷,并通过提交18项重构提案获得78%正面反馈,揭示了投资回报率是开发者重构的关键考量。

摘要:

AAA模式,即arrange、act和assert,为单元测试用例提供了统一的结构,这有助于理解和维护。然而,对于现实中的开发者在实践中是否以及如何遵循AAA来构建单元测试用例,目前缺乏了解。具体而言:是否存在偏离AAA结构且值得重构的反复出现的反模式?如果测试用例遵循AAA结构,它们是否可能在A块中包含设计缺陷?如果我们提出重构以修复遵循AAA的测试用例设计,开发者会如何看待这些提案?他们是否倾向于重构?如果不倾向,他们的考量是什么?本研究对从四个开源项目中随机选取的435个现实单元测试用例进行了实证研究。总体而言,大多数(71.5%)测试用例遵循AAA结构。此外,我们观察到三种偏离AAA结构的反复出现的反模式,以及四种可能存在于A块内部的设计缺陷。每种问题类型都有其缺点,并对应相应的重构解决方案。我们共提交了18项重构提案作为issue工单以修复这些问题。我们收到了78%的正面反馈支持重构。从被拒绝的提案中,我们了解到投资回报率是开发者的一个关键考量因素。这些发现为实践者在构建单元测试用例时考虑AAA提供了见解,也为研究人员开发在测试用例中强制执行AAA的相关技术提供了启示。

英文摘要:

The AAA pattern, i.e. arrange, act, and assert, provides a unified structure for unit test cases, which benefits comprehension and maintenance. However, there is little understanding regarding whether and how common real-life developers structure unit test cases following AAA in practice. In particular, are there recurring anti-patterns that deviate from the AAA structure and merit refactoring? And, if test cases follow the AAA structure, could they contain design flaws in the A blocks? If we propose refactoring to fix the design of test cases following the AAA, how do developers receive the proposals? Do they favor refactoring? If not, what are their considerations? This study presents an empirical study on 435 real-life unit test cases randomly selected from four open-source projects. Overall, the majority (71.5%) of test cases follow the AAA structure. And, we observed three recurring anti-patterns that deviate from the AAA structure, as well as four design flaws that may reside inside of the A blocks. Each issue type has its drawbacks and merits corresponding refactoring resolutions. We sent a total of 18 refactoring proposals as issue tickets for fixing these problems. We received 78% positive feedback favoring the refactoring. From the rejections, we learned that return-on-investment is a key consideration for developers. The findings provide insights for practitioners to structure unit test cases with AAA in mind, and for researchers to develop related techniques for enforcing AAA in test cases.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑