一句话:对 Go 表驱动测试开展实证研究,并提出自动化重构为缺少子测试的表驱动测试引入子测试。
表驱动测试是 Go 中广泛采用的测试风格:多个场景被编码在同一数据结构中并通过循环执行。然而,业界对其真实使用方式的理解仍然有限。本文对五个流行开源 Go 项目中的 8000 多个测试进行了试点研究,发现 31.7% 至 83.1% 的测试为表驱动,整体占比接近一半。基于检测到的模式,作者归纳出表驱动测试的常规结构,并识别出会损害可读性与执行控制的偏离形式。为解决这些问题,论文提出一种自动化重构技术,为缺少子测试的表驱动测试集成子测试。将该方法应用于 1100 多个符合条件的测试,成功率达到 69%,且除一例外均保持了原有行为。结果表明表驱动测试在 Go 中极为普遍,同时自动化改进以提升测试可读性、可维护性与标准化程度是可行的。
Table-driven testing is a widely adopted style in Go, where multiple scenarios are encoded in a single data structure and executed in a loop. Despite its popularity, there is a limited understanding of how these tests are implemented in the real world. This paper presents a pilot study of table-driven tests across five popular open-source Go projects, comprising over 8,000 total tests. Our analysis indicates that between 31.7% and 83.1% of tests in these projects are table-driven, with an overall prevalence of nearly half. Based on detected patterns, we also identify a conventional structure for table-driven tests and observe deviations that hinder readability and execution control. To address these issues, we introduce an automated refactoring technique that integrates subtests into table-driven tests that lack them. Applied to over 1,100 eligible tests, our approach achieved a 69% success rate while preserving behavior in all but one case. These results highlight both the prevalence of table-driven testing in Go and the feasibility of automated improvements that make tests more readable, maintainable, and standardized.