# 基于模型的测试

基于模型的测试（MBT）是一种软件测试方法，它从系统的形式化模型或行为模型中自动推导出测试用例，并针对被测系统执行这些用例。<sup>[1](https://eprints.qut.edu.au/57853/1/master_pdflatex.pdf)</sup> 它生成的测试用例是带有预期输出的输入对、输入序列或输入树，既可以生成为抽象的高层用例，也可以生成为具体的可执行脚本。<sup>[1](https://eprints.qut.edu.au/57853/1/master_pdflatex.pdf)</sup><sup> • </sup><sup>[2](https://www.gasq.org/files/content/gasq/downloads/certification/istqb_ctfl-mbt_-_syllabus_-_2015_-_final.pdf)</sup> 在对从业者的调查中，MBT 据报告平均减少 59% 的逃逸缺陷，降低 17% 的测试成本。<sup>[3](https://api.pageplace.de/preview/DT0400.9781351792639_A29964369/preview-9781351792639_A29964369.pdf)</sup>

| 方面 | 关键事实 |
|---|---|
| 产出物 | 抽象或具体的测试用例（输入及预期输出），以及脚本和测试数据<sup>[1](https://eprints.qut.edu.au/57853/1/master_pdflatex.pdf)</sup><sup> • </sup><sup>[2](https://www.gasq.org/files/content/gasq/downloads/certification/istqb_ctfl-mbt_-_syllabus_-_2015_-_final.pdf)</sup> |
| 核心一致性关系 | 输入/输出一致性（ioco/uioco）：实现对于任何规范迹的输出必须是规范输出的子集<sup>[4](https://docbox.etsi.org/Workshop/2023/11_UCAAT/TUTORIALS/Tutorial_tretmans_Principles%20of%20Model-Based%20Testing.pdf)</sup> |
| 基础性有限状态机成果 | 周 1978 年的自动机理论策略：n-切换集覆盖能在最小 n-可区分有限状态机中检测所有操作错误、转移错误和多余状态<sup>[5](https://doi.org/10.1109/tse.1978.231496)</sup> |
| 模型范式 | 七大类，从前/后置条件记法到随机马尔可夫链使用剖面<sup>[1](https://eprints.qut.edu.au/57853/1/master_pdflatex.pdf)</sup> |
| 实测有效性 | 在一项安全关键列车软件研究中，相比同类测试套件获得最高的 MC/DC 覆盖率（88%）<sup>[6](https://www.scitepress.org/Papers/2023/117568/117568.pdf)</sup> |
| 工业生产力 | 将 MBT 应用于 125 个协议，投入 50 人年，相比人工测试获得 42% 的效率提升<sup>[7](https://user.it.uu.se/~bengt/Papers/Full/amost16.pdf)</sup> |
| 近期发展 | 大语言模型构建的模型在 DNS、BGP 和 SMTP 实现中发现 33 个独特缺陷，其中 16 个此前未被发现<sup>[8](https://www.usenix.org/system/files/nsdi26-mondal.pdf)</sup> |

## 工作原理

MBT 将行为模型的运行视为被测系统（SUT）的测试用例：输入与预期输出。<sup>[9](https://mediatum.ub.tum.de/doc/1246349/1246349.pdf)</sup> [一致性测试](https://www.edgepedia.org/zh/conformance-testing)理论建立在测试假设之上——即每个真实实现都有一个假定的形式化模型——以及实现模型与规范之间的实现关系之上。<sup>[10](https://ris.utwente.nl/ws/files/6144406/276_Tre96.pdf)</sup> 在该框架中，测试用例是一个具有有限行为的确定性标记转移系统，在每个状态要么提供某一特定输入，要么接受所有可能的输出，并在状态上附有通过与失败判定。<sup>[10](https://ris.utwente.nl/ws/files/6144406/276_Tre96.pdf)</sup>

最广泛使用的实现关系是输入/输出一致性。对于输入使能的实现 \( i \) 和规范 \( s \)，

\[ i \; \mathrm{uioco} \; s \; \equiv_{\mathrm{def}} \; \forall \sigma \in \mathrm{Utraces}(s): \mathrm{out}(i \; \mathrm{after} \; \sigma) \subseteq \mathrm{out}(s \; \mathrm{after} \; \sigma) \]

因此实现永远不得产生规范所禁止的输出。<sup>[4](https://docbox.etsi.org/Workshop/2023/11_UCAAT/TUTORIALS/Tutorial_tretmans_Principles%20of%20Model-Based%20Testing.pdf)</sup> 当 \( \forall i: i \; \mathrm{ioconf} \; s \Rightarrow i \; \mathrm{passes} \; T \) 时，测试套件 \( T \) 是可靠的；从带有 uioco 的 LTS 生成测试被证明是可靠且完备的：\( \forall i \in \mathrm{IOTS}: ( \forall t \in \mathrm{gen}(s): i \; \mathrm{passes} \; t ) \Leftrightarrow i \; \mathrm{uioco} \; s \)。<sup>[4](https://docbox.etsi.org/Workshop/2023/11_UCAAT/TUTORIALS/Tutorial_tretmans_Principles%20of%20Model-Based%20Testing.pdf)</sup><sup> • </sup><sup>[10](https://ris.utwente.nl/ws/files/6144406/276_Tre96.pdf)</sup> 生成的测试用例是树形结构、有限且确定性的，带有汇点通过和失败状态，以及静止（即超时）标签 \( \delta \)。<sup>[4](https://docbox.etsi.org/Workshop/2023/11_UCAAT/TUTORIALS/Tutorial_tretmans_Principles%20of%20Model-Based%20Testing.pdf)</sup>

**生成算法**借鉴图论和形式化方法。中国邮递员算法至少覆盖模型的每条弧一次。<sup>[1](https://eprints.qut.edu.au/57853/1/master_pdflatex.pdf)</sup> 生成最短测试序列是 NP 完全问题。<sup>[11](http://inf.mit.bme.hu/sites/default/files/materials/category/kateg%C3%B3ria/education/software-verification-and-validation/20/SWVV-2020_L18_Model-based_testing_%28part1%29.pdf)</sup> 测试选择准则分为结构性、功能性、基于故障的和随机性准则，生成器使用模型检查器、符号执行、可满足性检查器或演绎定理证明器。<sup>[9](https://mediatum.ub.tum.de/doc/1246349/1246349.pdf)</sup> 在离线 MBT 中，脚本在执行前生成；在在线 MBT（也称即时测试）中，生成与执行同时实现。<sup>[2](https://www.gasq.org/files/content/gasq/downloads/certification/istqb_ctfl-mbt_-_syllabus_-_2015_-_final.pdf)</sup>

## 实施方法

通用流程包括五个步骤：构建测试模型、选择测试选择准则、将其转化为测试用例规范、生成测试套件，以及使用适配器执行该套件——适配器在得出判定之前将输入具体化并将输出抽象化。<sup>[1](https://eprints.qut.edu.au/57853/1/master_pdflatex.pdf)</sup> 适配器负责将抽象测试数据适配到具体的 SUT 接口，在自动化 MBT 中通常占工作量的相当比例。<sup>[1](https://eprints.qut.edu.au/57853/1/master_pdflatex.pdf)</sup>

**选择准则**是就模型元素来陈述的：状态图中的状态、转移和判定；业务流程模型中的活动和网关；判定表中的条件和动作；以及数据相关准则，如成对和 n-组合生成。<sup>[2](https://www.gasq.org/files/content/gasq/downloads/certification/istqb_ctfl-mbt_-_syllabus_-_2015_-_final.pdf)</sup> 有三种方法管理从抽象用例到自动化执行的转换：适配方法，即由测试适配层代码弥合抽象鸿沟；转换方法，即将用例直接转换为脚本；以及混合方法。<sup>[2](https://www.gasq.org/files/content/gasq/downloads/certification/istqb_ctfl-mbt_-_syllabus_-_2015_-_final.pdf)</sup> 围绕模型本身的工具支持往往薄弱：大多数 MBT 工具依赖第三方工具创建模型，只有 ParTeG 作为原型能生成完整的测试适配器。<sup>[12](https://carleton.ca/squall/wp-content/uploads/TR_SCE-10-04.pdf)</sup>

## 起源

截至 2000 年代中期，MBT 的思想——当时被称为“基于规范的测试”——已经存在了大约三十年。<sup>[9](https://mediatum.ub.tum.de/doc/1246349/1246349.pdf)</sup> 周 1978 年发表在《IEEE 软件工程汇刊》上的论文提出了一种针对控制结构可用有限状态机建模的软件的测试策略，作者称之为“自动机理论的”。<sup>[5](https://doi.org/10.1109/tse.1978.231496)</sup> 它将 n-切换定义为程序图中长度为 \( n + 1 \) 的连续分支序列，并证明 n-切换集覆盖能在 n-可区分的最小有限状态机中检测所有操作错误、转移错误和多余状态。<sup>[13](https://archiv.infsec.ethz.ch/intranet_secured/r/1/chow-testingFSMs.pdf)</sup> 为测试协议而开发的同构检查方法——W 方法、Wp 方法和 D 方法——也基于 FSM 模型的结构覆盖。<sup>[14](https://ethz.ch/content/dam/ethz/special-interest/infk/inst-infsec/information-security-group-dam/research/publications/pub2006/uow-cs-wp-2006-04.pdf)</sup> D. Lee 和 M. Yannakakis 于 1996 年发表在《IEEE 会刊》上的有限状态机测试原理与方法综述汇集了这一研究路线。<sup>[15](https://doi.org/10.1109/5.533956)</sup>

后来的参照点包括 Mark Utting、Alexander Pretschner 和 Bruno Legeard 于 2012 年发表的基于模型的测试方法分类学，其中列出了 48 种不同的 MBT 建模记法，归为七大范式，<sup>[1](https://eprints.qut.edu.au/57853/1/master_pdflatex.pdf)</sup> Margus Veanes 及其同事于 2005 年描述的即时测试方法，<sup>[16](https://www.cs.helsinki.fi/u/paakki/lindholm.pdf)</sup> 以及 Muhammad Nouman Zafar 及其同事于 2025 年在《SN 计算机科学》上报告的基于 GraphWalker 的 TIGER 测试脚本生成框架。<sup>[17](https://doi.org/10.1007/s42979-025-03823-7)</sup>

## 变体

七大建模范式是：前/后置条件记法（Z、B、VDM、OCL、JML 以及输入域记法）、基于转移的模型（FSM、状态图、LTS、I/O 自动机）、基于历史的模型（时序逻辑、消息序列图）、函数代数规范、操作模型（CSP/CCS、Petri 网）、随机模型（用于使用剖面的马尔可夫链）以及数据流模型（Lustre、Simulink）。<sup>[1](https://eprints.qut.edu.au/57853/1/master_pdflatex.pdf)</sup> 有限状态机在形式上是一个五元组 \( (Q, \Sigma, \delta, q_{0}, F) \)，它是许多其他 MBT 模型所基于的基础模型。<sup>[16](https://www.cs.helsinki.fi/u/paakki/lindholm.pdf)</sup>

**在线测试**在测试生成器与 SUT 之间建立更紧密、更快速的连接，允许更好的错误报告和更大测试套件的快速执行。<sup>[18](https://eprints.qut.edu.au/56821/20/modeljunit_with_figures.pdf)</sup> Spec Explorer 是微软的一款基于模型的测试工具，使用以 C# 编写的 EFSM 模型。<sup>[18](https://eprints.qut.edu.au/56821/20/modeljunit_with_figures.pdf)</sup> 另一种变体通过对面向模型的形式化规范执行符号执行来生成测试用例，并从中获得 Java 程序，从而使测试周期完全自动化。<sup>[19](https://dl.acm.org/doi/10.5555/1776119.1776125)</sup>

近期工作增加了基于学习和基于大语言模型的生成。EYWA 能在数分钟内以最少的用户输入构建编码 RFC 意图的模型，而此前针对 DNS 和 BGP 的 MBT 方法需要数月的工作；应用测试在广泛使用的 DNS、BGP 和 SMTP 实现中发现了 33 个独特缺陷，其中 16 个此前未被发现。<sup>[8](https://www.usenix.org/system/files/nsdi26-mondal.pdf)</sup>

## 应用

Spec Explorer 在微软内部被广泛日常使用。<sup>[1](https://eprints.qut.edu.au/57853/1/master_pdflatex.pdf)</sup> 一项被引用的微软研究报告称，将 MBT 应用于 125 个协议，投入 50 人年，相比传统人工测试获得了 42% 的生产力提升。<sup>[7](https://user.it.uu.se/~bengt/Papers/Full/amost16.pdf)</sup> 工业应用也延伸到汽车测试：工业级 MBT 综述中描述的工具被用于测试支持戴姆勒梅赛德斯车辆转向指示功能的汽车控制器网络。<sup>[20](https://ar5iv.labs.arxiv.org/html/1303.1006)</sup>

**定量评估**给出了喜忧参半但总体有利的图景。在一项汽车网络控制器研究中，自动和手动推导的基于模型的测试套件都比直接从需求推导的手工套件检测到显著更多的需求错误。<sup>[21](https://arxiv.org/pdf/1701.06815v1.pdf)</sup> 在一项安全关键列车软件研究中，MBT 生成的套件提供了最高的 MC/DC 覆盖率，达 88%，而组合测试获得了最高的变异得分。<sup>[6](https://www.scitepress.org/Papers/2023/117568/117568.pdf)</sup> 在一项 Tizen 智能电视 GUI 研究中，使用 MBT 的总测试时间低于脚本编写，且 MBT 的行覆盖率超过脚本编写，平均提升 13.92%。<sup>[22](https://dl.acm.org/doi/10.1145/3715789)</sup> 与这些结果相对的是，一项含有真实且非人为植入的故障的电信案例研究发现，基于覆盖的模型测试套件与随机生成的模型测试套件之间没有明显差异。<sup>[7](https://user.it.uu.se/~bengt/Papers/Full/amost16.pdf)</sup>

## 局限与替代方案

只有在模型比 SUT 更抽象、并由驱动器弥合具体化与抽象化之间的鸿沟时，MBT 在方法论上才成立；模型中未捕获的行为不会被测试。<sup>[9](https://mediatum.ub.tum.de/doc/1246349/1246349.pdf)</sup> 对大型模型的[探索](https://www.edgepedia.org/zh/exploration)可能导致状态爆炸问题，即具体状态过多而无法处理。<sup>[12](https://carleton.ca/squall/wp-content/uploads/TR_SCE-10-04.pdf)</sup> 一项关于生成与优先级排序方法的系统综述指出，最常见的局限是依赖规范、需要人工干预以及可扩展性。<sup>[23](https://link.springer.com/article/10.1007/s10270-021-00924-8)</sup> 截至 2000 年代中期，综述者指出没有已发表的证据表明 MBT 的承诺得到了兑现，且与随机测试或人工测试的对比研究基本不存在；上述定量研究此后部分填补了这一空白。<sup>[9](https://mediatum.ub.tum.de/doc/1246349/1246349.pdf)</sup>

**预言精确度**是一个具体的权衡。在一项包含 26 个真实故障的工业基于状态的测试研究中，精确度较低的测试预言实现了 67% 的故障检测率，但 13% 的总体成本降低被判定为不可接受的权衡。<sup>[24](https://torkar.github.io/pdfs/1-s2.0-S0950584914000536-main.pdf)</sup> 同一研究发现，针对潜行路径的测试杀死了一致性测试策略无法杀死的其余十一个变异体。<sup>[24](https://torkar.github.io/pdfs/1-s2.0-S0950584914000536-main.pdf)</sup> 与替代方案相比，MBT 在汽车研究中在需求错误方面胜过手工套件<sup>[21](https://arxiv.org/pdf/1701.06815v1.pdf)</sup>，在 GUI 研究中在时间和覆盖率方面胜过脚本编写<sup>[22](https://dl.acm.org/doi/10.1145/3715789)</sup>，但在一项电信案例中未显示出优于随机生成的优势<sup>[7](https://user.it.uu.se/~bengt/Papers/Full/amost16.pdf)</sup>，在列车软件研究中在变异得分上落后于[组合测试](https://www.edgepedia.org/zh/combinatorial-testing)。<sup>[6](https://www.scitepress.org/Papers/2023/117568/117568.pdf)</sup> 与模糊测试、[基于属性的测试](https://www.edgepedia.org/zh/property-based-testing)和基于搜索的软件测试的正面对比尚未由已发表文献解决。

## References

1. [A taxonomy of model-based testing (Utting, Pretschner & Legeard, STVR 2012; includes mirrored copy at mediatum.ub.tum.de/doc/1246357/1246357.pdf)](https://eprints.qut.edu.au/57853/1/master_pdflatex.pdf)
2. [ISTQB CTFL Model-Based Testing Syllabus 2015](https://www.gasq.org/files/content/gasq/downloads/certification/istqb_ctfl-mbt_-_syllabus_-_2015_-_final.pdf)
3. [The Craft of Model-Based Testing (Jorgensen, CRC Press preview)](https://api.pageplace.de/preview/DT0400.9781351792639_A29964369/preview-9781351792639_A29964369.pdf)
4. [Principles of Model-Based Testing (Tretmans, UCAAT 2023 tutorial)](https://docbox.etsi.org/Workshop/2023/11_UCAAT/TUTORIALS/Tutorial_tretmans_Principles%20of%20Model-Based%20Testing.pdf)
5. [T.S. Chow (1978). Testing Software Design Modeled by Finite-State Machines. IEEE Transactions on Software Engineering.](https://doi.org/10.1109/tse.1978.231496)
6. [An Empirical Evaluation of System-Level Test Effectiveness for Safety-Critical Software (2023)](https://www.scitepress.org/Papers/2023/117568/117568.pdf)
7. [Industrial Evaluation of Test Suite Generation Strategies for Model-Based Testing (Blom, Jönsson, Nyström, 2016)](https://user.it.uu.se/~bengt/Papers/Full/amost16.pdf)
8. [Eywa: Automating Model-Based Testing using LLMs (NSDI '26)](https://www.usenix.org/system/files/nsdi26-mondal.pdf)
9. [Model-Based Testing in Practice (Pretschner et al.)](https://mediatum.ub.tum.de/doc/1246349/1246349.pdf)
10. [Test Generation with Inputs, Outputs and Repetitions (Tretmans)](https://ris.utwente.nl/ws/files/6144406/276_Tre96.pdf)
11. [SWVV 2020 L18 Model based testing (part1) (inf.mit.bme.hu)](http://inf.mit.bme.hu/sites/default/files/materials/category/kateg%C3%B3ria/education/software-verification-and-validation/20/SWVV-2020_L18_Model-based_testing_%28part1%29.pdf)
12. [A Systematic Review of Model Based Testing Tool Support (Shafique & Labiche, Carleton TR SCE-10-04)](https://carleton.ca/squall/wp-content/uploads/TR_SCE-10-04.pdf)
13. [Testing Software Design Modeled by Finite-State Machines (Chow)](https://archiv.infsec.ethz.ch/intranet_secured/r/1/chow-testingFSMs.pdf)
14. [A taxonomy of model-based testing (2006 working paper, Pretschner/Utting line)](https://ethz.ch/content/dam/ethz/special-interest/infk/inst-infsec/information-security-group-dam/research/publications/pub2006/uow-cs-wp-2006-04.pdf)
15. [D. Lee, M. Yannakakis (1996). Principles and methods of testing finite state machines-a survey. Proceedings of the IEEE.](https://doi.org/10.1109/5.533956)
16. [Model-Based Testing of Software (Helsinki MSc thesis)](https://www.cs.helsinki.fi/u/paakki/lindholm.pdf)
17. [Muhammad Nouman Zafar and colleagues (2025). A Model-Based Test Script Generation Framework and Industrial Insight. SN Computer Science.](https://doi.org/10.1007/s42979-025-03823-7)
18. [EFSM-based testing with ModelJUnit (Utting et al., practical chapter)](https://eprints.qut.edu.au/56821/20/modeljunit_with_figures.pdf)
19. [Automatic testing from formal specifications (Satpathy, Butler, Leuschel, Ramesh, TAP'07)](https://dl.acm.org/doi/10.5555/1776119.1776125)
20. [Industrial-Strength Model-Based Testing, State of the Art and Current Challenges (Peleska)](https://ar5iv.labs.arxiv.org/html/1303.1006)
21. [One Evaluation of Model-Based Testing and its Automation (Pretschner et al., ICSE 2005)](https://arxiv.org/pdf/1701.06815v1.pdf)
22. [A Mixed-Methods Study of Model-Based GUI Testing in Real-World Industrial Settings (PACMSE)](https://dl.acm.org/doi/10.1145/3715789)
23. [Model-based test case generation and prioritization: a systematic literature review (Software and Systems Modeling)](https://link.springer.com/article/10.1007/s10270-021-00924-8)
24. [Empirical evaluations on the cost-effectiveness of state-based testing: An industrial case study](https://torkar.github.io/pdfs/1-s2.0-S0950584914000536-main.pdf)

---
*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/model-based-testing

English original: https://www.edgepedia.org/model-based-testing
