# 测试套件缩减

测试套件缩减（亦称测试套件最小化）是一种软件工程技术，它从现有测试套件中选取一个子集，同时保留某个覆盖准则或需求准则，从而使测试运行得更快、成本更低。其产出是一个永久性缩减后的套件：冗余测试用例被剔除，而不仅仅是重新排序或为某个版本暂时搁置。这使它区别于回归测试选择——后者为特定的代码变更挑选测试——也区别于测试优先级排序——后者对测试排序但不丢弃任何测试。<sup>[1](https://onlinelibrary.wiley.com/doi/10.1002/stvr.430)</sup> 经典表述要求得到一个缩减套件 R ⊆ O，使得 req(O) = req(R)，其中 req 将套件映射到它所满足的需求集合。<sup>[2](https://mir.cs.illinois.edu/marinov/publications/ShiETAL15ReductionSelection.pdf)</sup> 尽管已有数十年研究，最小化在工业实践中却很少使用，部分原因是它在复杂构建环境下难以实施。<sup>[3](https://teamscale.com/hubfs/26978363/Publications/2020-test-suite-minimization-swqd.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 产出 | 一个永久性缩减套件 R ⊆ O，满足 req(O) = req(R)<sup>[2](https://mir.cs.illinois.edu/marinov/publications/ShiETAL15ReductionSelection.pdf)</sup> |
| 复杂度 | 寻找最小基数命中集是 NP 完全问题，等价于集合覆盖<sup>[4](https://eprints.soton.ac.uk/396265/1/IJIM_testt_suite_reduction_framework_accepted.pdf)</sup> |
| 目前已知的最佳保证 | 贪心集合覆盖可达到对数因子近似；加权集合覆盖没有常数因子保证<sup>[5](https://link.springer.com/article/10.1007/s41109-020-00323-w)</sup> |
| 参考算法 | Harrold–Gupta–Soffa（HGS）启发式，最坏情况时间 \( O(\|T\| \cdot \max(\|T_{i}\|)) \)<sup>[6](https://www.cs.ucr.edu/~gupta/research/Publications/Comp/p270-harrold.pdf)</sup><sup> • </sup><sup>[1](https://onlinelibrary.wiley.com/doi/10.1002/stvr.430)</sup> |
| 典型缩减效果 | 充分的语句覆盖缩减平均将套件规模削减 62.9%，最多损失 20.5% 的被杀变异体<sup>[7](https://dl.acm.org/doi/pdf/10.1145/2635868.2635921)</sup> |
| 主要风险 | 当覆盖准则遗漏故障时，故障检测能力可能丧失<sup>[8](https://onlinelibrary.wiley.com/doi/10.1002/stvr.256)</sup> |

## 工作原理

缩减被表述为一个覆盖问题。给定一个 \( m \times n \) 的 0/1 矩阵 \( A \)，若测试 \( t_{j} \) 满足需求 \( R_{i} \)，则条目 \( (i, j) = 1 \)，此时选择满足每项需求的最少测试，就是一个零一整数规划问题，等价于 NP 完全的集合覆盖问题。<sup>[9](https://www.witpress.com/Secure/elibrary/papers/SQM95/SQM95037FU2.pdf)</sup> 等价地，人们寻求需求集合 \( T_{i} \) 的最小基数命中集；这两个问题互为对偶，且都是 NP 完全的。<sup>[1](https://onlinelibrary.wiley.com/doi/10.1002/stvr.430)</sup> 由于精确求解在大规模下不可行，实用算法都是启发式的。经典的贪心启发式每次选取覆盖最多未覆盖点的集合，丢弃已覆盖的点，重复此过程直到所有点被覆盖，若有多个集合并列，则任意选取其中一个。<sup>[10](https://llvm.org/pubs/2005-09-PASTE-GreedySuiteMinimization.pdf)</sup> 对于加权集合覆盖，贪心算法可达到对数因子近似，即对数误差界；加权变体没有常数因子近似保证。<sup>[5](https://link.springer.com/article/10.1007/s41109-020-00323-w)</sup>

充分缩减与不充分缩减区分了所保留的内容。充分方法产生的缩减套件保留原套件的全部测试需求；不充分方法只满足固定百分比 l% 的需求，以原套件 O 和需求函数 ρ 为输入。<sup>[11](https://iris.unitn.it/retrieve/797f72fb-46f1-46f0-b682-8ca7e043a6bf/IST_postprint_.pdf)</sup><sup> • </sup><sup>[7](https://dl.acm.org/doi/pdf/10.1145/2635868.2635921)</sup> 准则本身是灵活的：该技术只要求测试需求与满足它们的测试用例之间存在关联，因此语句覆盖、数据流覆盖、MC/DC 或基于变异的需求都适用。<sup>[6](https://www.cs.ucr.edu/~gupta/research/Publications/Comp/p270-harrold.pdf)</sup>

## 实施方法

HGS 启发式在原始论文中名为 ReduceTestSuite，其流程如下。首先，它纳入所有出现在基数为 1 的需求集 \( T_{i} \) 中的测试用例（即必要测试，没有其他测试能覆盖它们），并标记它们所满足的所有需求。然后它考虑基数递增的需求集——2、3，依此类推——每次选择出现在最多未标记集合中的测试用例，若有多个测试用例并列，则通过检查更高基数的集合来确定取舍，若仍并列则随机选择，直到所有需求被满足。<sup>[10](https://llvm.org/pubs/2005-09-PASTE-GreedySuiteMinimization.pdf)</sup><sup> • </sup><sup>[6](https://www.cs.ucr.edu/~gupta/research/Publications/Comp/p270-harrold.pdf)</sup>

实践者的流程遵循这一表述。首先，对代码进行插桩或以其他方式收集每个测试满足哪些需求；其次，构建测试到需求矩阵；第三，运行缩减算法，如贪心算法，它迭代选择满足最多先前未满足需求的测试，出现并列时随机选择；第四，验证缩减后的套件。已有实现使用 IBM CPLEX Optimizer 处理 ILP 变体，并使用修改版 PIT 变异测试工具生成的变异体杀死矩阵。<sup>[7](https://dl.acm.org/doi/pdf/10.1145/2635868.2635921)</sup> 基于覆盖的工具带来实际成本：插桩后的代码必须运行以收集覆盖信息，覆盖存储随程序规模增长，且随着软件演化，先前收集的覆盖信息会变得不一致。<sup>[4](https://eprints.soton.ac.uk/396265/1/IJIM_testt_suite_reduction_framework_accepted.pdf)</sup> 仅覆盖收集一项就可能造成高达 30% 的时间开销，这促使人们研究完全避免覆盖信息的方法。<sup>[12](https://robertoverdecchia.github.io/papers/ICSE_2019.pdf)</sup>

## 起源

[线性规划](https://www.edgepedia.org/zh/linear-programming)此前已被应用于数据流测试工具 ATAC 中的测试用例最小化问题。<sup>[1](https://onlinelibrary.wiley.com/doi/10.1002/stvr.430)</sup> 前驱缩减程序包括 Offutt、Pan 和 Voas 于 1995 年开展的基于覆盖的测试集缩减工作<sup>[8](https://onlinelibrary.wiley.com/doi/10.1002/stvr.256)</sup>，以及 T.Y. Chen 和 M.F. Lau 于 1998 年发表在《Information and Software Technology》上的启发式方法。<sup>[13](https://doi.org/10.1016/s0950-5849%2898%2900050-0)</sup> 后续扩展迅速跟进：Sriraman Tallam 和 Neelam Gupta 于 2005 年提出了基于形式概念分析的延迟贪心方法<sup>[14](https://doi.org/10.1145/1108768.1108802)</sup>，Dennis Jeffrey 和 Neelam Gupta 于 2007 年提出在缩减过程中选择性保留测试用例。<sup>[15](https://doi.org/10.1109/tse.2007.18)</sup>

## 变体

各种命名变体在算法和保留内容上有所不同。Chen 和 Lau 的 GE 启发式保留必要测试用例加上满足最多未满足需求的测试，GRE 则额外移除冗余测试用例；他们观察到 GE、GRE 以及 Harrold 等人的算法都不是始终最优的。<sup>[9](https://www.witpress.com/Secure/elibrary/papers/SQM95/SQM95037FU2.pdf)</sup> Tallam 和 Gupta 的延迟贪心在贪心步骤之前对概念格应用对象缩减、属性缩减和所有者缩减，并保证产生的套件规模不大于经典贪心。<sup>[10](https://llvm.org/pubs/2005-09-PASTE-GreedySuiteMinimization.pdf)</sup> Jeffrey 和 Gupta 对 HGS 的选择性冗余扩展保留部分冗余测试用例以改善故障检测。<sup>[15](https://doi.org/10.1109/tse.2007.18)</sup> Jones 和 Harrold 针对修正条件/判定覆盖定制了缩减和优先级排序，其动因是 FAA 要求高保证商用机载软件测试套件满足 MC/DC 充分性要求。<sup>[16](https://doi.org/10.1109/tse.2003.1183927)</sup> 基于变异的缩减将变异得分映射到集合覆盖，全集为变异体，每个测试贡献它所杀死的变异体。<sup>[17](https://tugraz.elsevierpure.com/ws/portalfiles/portal/70135366/An_Empirical_Study_of_Greedy_Test_Suite_Minimization_Techniques_Using_Mutation_Coverage.pdf)</sup> 基于求解器的方法包括计算最优最小化套件的二元 ILP 模型<sup>[11](https://iris.unitn.it/retrieve/797f72fb-46f1-46f0-b682-8ca7e043a6bf/IST_postprint_.pdf)</sup> 和 RZOLTAR，后者将最小化映射为最小命中集问题，并使用 MINION 约束求解器生成多个最小套件。<sup>[18](https://webarchive.di.uminho.pt/haslab.uminho.pt/ruimaranhao/files/paper.pdf)</sup> Yoo 和 Harman 为回归测试提出了帕累托最优的多目标算法。<sup>[7](https://dl.acm.org/doi/pdf/10.1145/2635868.2635921)</sup>

## 应用

报告的结果因准则和环境而异。在 18 个项目、261,235 个测试、3,590 次提交、跨越 35 年历史的规模上，传统的基于语句覆盖的充分缩减平均将测试套件规模削减 62.9%，但最多损失 20.5% 的被杀变异体。<sup>[7](https://dl.acm.org/doi/pdf/10.1145/2635868.2635921)</sup> 一项针对七个开源项目的研究使用贪心和 HGS 配合语句覆盖，发现所有对象的测试数量至少减少 50%，平均移除约 69% 的测试，但执行时间缩减仅在 5% 到 69% 之间，因此测试数量缩减是执行时间缩减的糟糕指标。<sup>[3](https://teamscale.com/hubfs/26978363/Publications/2020-test-suite-minimization-swqd.pdf)</sup> 在 [JavaScript](https://www.edgepedia.org/zh/javascript) 应用的变异覆盖下，基于贪心的算法平均将套件规模缩减至 70%，且不损害故障检测能力。<sup>[17](https://tugraz.elsevierpure.com/ws/portalfiles/portal/70135366/An_Empirical_Study_of_Greedy_Test_Suite_Minimization_Techniques_Using_Mutation_Coverage.pdf)</sup> REDUNET 将整数线性规划求解的加权集合覆盖与基于控制流图的优化相结合，实现了高达 90% 的缩减，在研究的全部十个系统上均超过 50%。<sup>[5](https://link.springer.com/article/10.1007/s41109-020-00323-w)</sup>

近期工作用学习到的表示替代覆盖矩阵。LTM 被描述为大语言模型在测试套件最小化中的应用：它评估五个预训练模型，使用余弦相似度和[欧几里得距离](https://www.edgepedia.org/zh/euclidean-distance)引导遗传算法，其最佳配置实现平均测试时间节省 41.72%，而 ATM 为 41.02%，故障检测率为 0.84 对 0.81，且套件最小化的速度接近 ATM 的五倍。<sup>[19](https://doi.org/10.1109/tse.2024.3469582)</sup> TestPrune 将基于问题的最小化形式化为加权最小命中集问题，使用 LLM 预测可疑方法加贪心选择；在 SWE-Bench-Lite 和 SWE-Bench-Verified 上，它将平均 9,012 和 11,769 个测试的套件削减至平均执行 9 个测试，运行时间从 23 分 49 秒降至 52 秒（27 倍）。<sup>[20](https://dl.acm.org/doi/pdf/10.1145/3808148)</sup> 在持续集成中，经典缩减的后继者是选择和优先级排序方法：RETECS 由 Spieker、Gotlieb、Marijan 和 Mossige 于 2018 年提出，使用神经网络强化学习，根据持续时间、上次执行时间和失败历史来选择和排序测试用例。<sup>[21](https://doi.org/10.48550/arxiv.1811.04122)</sup>

## 局限与替代方案

核心未解问题是缩减是否损害故障检测。Wong 等人发现测试套件缩减不会显著降低测试套件的故障检测能力，而 Rothermel 等人发现它可能严重降低；这一分歧在文献中尚未解决。<sup>[22](https://mir.cs.illinois.edu/marinov/publications/ZhangETAL11JUnitReduction.pdf)</sup><sup> • </sup><sup>[8](https://onlinelibrary.wiley.com/doi/10.1002/stvr.256)</sup> 损失背后的机制是：当覆盖准则不充分时，缩减可能丢弃那些能检测到准则所遗漏故障的测试；Rothermel 等人的研究在 [Aristotle](https://www.edgepedia.org/zh/aristotle) 系统中实现 HGS，使用边覆盖和全使用数据流准则，每个程序生成 1,000 个套件，表明故障检测能力可能受到严重损害。<sup>[8](https://onlinelibrary.wiley.com/doi/10.1002/stvr.256)</sup> 真实演化证据指向同一方向：在 Travis 上 32 个 GitHub 项目的 1,478 次失败构建中，失败构建检测损失达到 52.2%，高于传统缩减指标所暗示的水平，且这些指标不能很好地预测这一损失。<sup>[23](https://doi.org/10.1145/3213846.3213875)</sup>

最接近的替代方案是安全回归测试选择，它在明确定义的条件下不排除任何能揭示修改后软件中故障的测试，尽管其成本和收益差异很大。<sup>[24](https://dl.acm.org/doi/10.1109/32.689399)</sup> 在两者的首次实证比较中，针对 17 个开源 Java 项目、4,793 个修订版本，回归测试选择平均比缩减少运行 40.15 个百分点的测试，且安全选择没有故障检测损失，而缩减最多损失 5.93% 的被杀变异体；作者得出结论：如果只选择一种方法，测试工程师应选择回归测试选择，而两者结合节省更多（比单独选择少 5.34 个百分点的测试），代价是可能的损失。<sup>[2](https://mir.cs.illinois.edu/marinov/publications/ShiETAL15ReductionSelection.pdf)</sup> 这些权衡，加上在复杂构建环境下实施最小化的困难，解释了为何它在实践中很少使用。<sup>[3](https://teamscale.com/hubfs/26978363/Publications/2020-test-suite-minimization-swqd.pdf)</sup>

## References

1. [Regression testing minimization, selection and prioritization: a survey (Yoo & Harman, STVR 2012)](https://onlinelibrary.wiley.com/doi/10.1002/stvr.430)
2. [Comparing and Combining Test-Suite Reduction and Regression Test Selection (Shi et al., ESEC/FSE 2015)](https://mir.cs.illinois.edu/marinov/publications/ShiETAL15ReductionSelection.pdf)
3. [An Evaluation of Test Suite Minimization Techniques](https://teamscale.com/hubfs/26978363/Publications/2020-test-suite-minimization-swqd.pdf)
4. [A Survey on Test Suite Reduction Frameworks and Tools (Khan, Lee, Ahmad, Akhunzada, Chang)](https://eprints.soton.ac.uk/396265/1/IJIM_testt_suite_reduction_framework_accepted.pdf)
5. [REDUNET: reducing test suites by integrating set cover and network-based optimization (Applied Network Science, 2020)](https://link.springer.com/article/10.1007/s41109-020-00323-w)
6. [A methodology for controlling the size of a test suite (Harrold, Gupta, Soffa)](https://www.cs.ucr.edu/~gupta/research/Publications/Comp/p270-harrold.pdf)
7. [Balancing Trade-Offs in Test-Suite Reduction (Shi, Gyori, Gligoric, Zaytsev, Marinov; FSE 2014)](https://dl.acm.org/doi/pdf/10.1145/2635868.2635921)
8. [Empirical studies of test-suite reduction (Rothermel, Harrold, von Ronne, Hong; STVR 2002, expanded from ICSM 1998)](https://onlinelibrary.wiley.com/doi/10.1002/stvr.256)
9. [Heuristics towards the optimization of the size of a test suite (Chen & Lau)](https://www.witpress.com/Secure/elibrary/papers/SQM95/SQM95037FU2.pdf)
10. [A Concept Analysis Inspired Greedy Algorithm for Test Suite Minimization (Tallam & Gupta, PASTE 2005)](https://llvm.org/pubs/2005-09-PASTE-GreedySuiteMinimization.pdf)
11. [Adequate vs. Inadequate Test Suite Reduction Approaches (postprint)](https://iris.unitn.it/retrieve/797f72fb-46f1-46f0-b682-8ca7e043a6bf/IST_postprint_.pdf)
12. [Scalable Approaches for Test Suite Reduction (FAST-R, ICSE 2019)](https://robertoverdecchia.github.io/papers/ICSE_2019.pdf)
13. [A new heuristic for test suite reduction (Information and Software Technology, 1998)](https://doi.org/10.1016/s0950-5849%2898%2900050-0)
14. [Sriraman Tallam, Neelam Gupta (2005). A concept analysis inspired greedy algorithm for test suite minimization. ACM SIGSOFT Software Engineering Notes.](https://doi.org/10.1145/1108768.1108802)
15. [Dennis Jeffrey, Neelam Gupta (2007). Improving Fault Detection Capability by Selectively Retaining Test Cases during Test Suite Reduction. IEEE Transactions on Software Engineering.](https://doi.org/10.1109/tse.2007.18)
16. [J.A. Jones, M.J. Harrold (2003). Test-suite reduction and prioritization for modified condition/decision coverage. IEEE Transactions on Software Engineering.](https://doi.org/10.1109/tse.2003.1183927)
17. [An Empirical Study of Greedy Test Suite Minimization Techniques Using Mutation Coverage (Jehan & Wotawa)](https://tugraz.elsevierpure.com/ws/portalfiles/portal/70135366/An_Empirical_Study_of_Greedy_Test_Suite_Minimization_Techniques_Using_Mutation_Coverage.pdf)
18. [Leveraging a Constraint Solver for Minimizing Test Suites (RZOLTAR)](https://webarchive.di.uminho.pt/haslab.uminho.pt/ruimaranhao/files/paper.pdf)
19. [Rongqi Pan, Taher A. Ghaleb, Lionel C. Briand (2024). LTM: Scalable and Black-Box Similarity-Based Test Suite Minimization Based on Language Models. IEEE Transactions on Software Engineering.](https://doi.org/10.1109/tse.2024.3469582)
20. [Can Old Tests Do New Tricks for Resolving SWE Issues? (TestPrune, FSE 2026, Proc. ACM Softw. Eng.)](https://dl.acm.org/doi/pdf/10.1145/3808148)
21. [Spieker, Helge and colleagues (2018). Reinforcement Learning for Automatic Test Case Prioritization and Selection in Continuous Integration. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1811.04122)
22. [An Empirical Study of JUnit Test-Suite Reduction (Zhang, Marinov, Zhang, Khurshid; ISSRE 2011)](https://mir.cs.illinois.edu/marinov/publications/ZhangETAL11JUnitReduction.pdf)
23. [Evaluating test-suite reduction in real software evolution (Shi, Gyori, Mahmood, Zhao, Marinov; ISSTA 2018)](https://doi.org/10.1145/3213846.3213875)
24. [Empirical Studies of a Safe Regression Test Selection Technique (Rothermel & Harrold, IEEE TSE)](https://dl.acm.org/doi/10.1109/32.689399)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Software engineering and development process › Software testing and quality

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/test-suite-reduction

English original: https://www.edgepedia.org/test-suite-reduction
