# 单元测试框架列表

单元测试框架是一种软件库，它提供了一套结构化的方式来编写、组织和运行针对源代码中各个单元的自动化测试，通常提供断言、测试发现、用于初始化和清理的测试夹具以及报告功能。如今使用的大多数框架都源自 xUnit 架构，该架构始于 [Kent Beck](https://www.edgepedia.org/zh/kent-beck) 的 [Smalltalk](https://www.edgepedia.org/zh/smalltalk) 框架，并通过 JUnit 等移植版本传播到其他语言。<sup>[1](https://www.oreilly.com/library/view/unit-test-frameworks/0596006896/ch03.html)</sup>

| 框架 | 语言 | 显著特征 |
|---|---|---|
| SUnit | Smalltalk | 最早的 xUnit 框架，由 Kent Beck 发布；区分失败与错误，支持挂载到类上的测试套件<sup>[1](https://www.oreilly.com/library/view/unit-test-frameworks/0596006896/ch03.html)</sup><sup> • </sup><sup>[2](https://dionatanmoura.com/wp-content/uploads/2024/06/simple-smalltalk-testing-with-patterns-kent-beck.pdf)</sup> |
| JUnit | Java | xUnit 的参考实现；JUnit 5 由 Platform、Jupiter 和 Vintage 三个子项目组成，其中 Vintage 用于运行 JUnit 3 和 4 的测试<sup>[1](https://www.oreilly.com/library/view/unit-test-frameworks/0596006896/ch03.html)</sup><sup> • </sup><sup>[3](https://docs.junit.org/5.0.0/user-guide/)</sup> |
| pytest | Python | 使用普通 `assert` 语句并提供详细的失败内省、自动发现测试、模块化夹具以及超过 1300 个外部插件；可直接运行 unittest 测试套件<sup>[4](https://docs.pytest.org/en/stable/index.html)</sup> |
| unittest | Python | 标准库框架，最初受 JUnit 启发，包含 TestCase、夹具、套件和运行器<sup>[5](https://docs.python.org/3/library/unittest.html?highlight=testcase)</sup> |
| Jest | JavaScript、TypeScript | 零配置测试运行器，支持快照测试——首次运行时保存序列化快照文件，后续运行将输出与之比对<sup>[6](https://jestjs.io/docs/getting-started)</sup><sup> • </sup><sup>[7](https://jestjs.io/docs/30.0/snapshot-testing)</sup> |
| GoogleTest | C++ | 基于 xUnit 的框架，提供 `ASSERT_*`/`EXPECT_*` 成对断言、`TEST()` 和 `TEST_F()` 宏、死亡测试以及值和类型参数化测试<sup>[8](https://github.com/google/googletest/blob/main/docs/primer.md)</sup> |
| NUnit、xUnit.net、MSTest、TUnit | .NET 语言 | 流行的 .NET 框架；NUnit 最初从 JUnit 移植而来，xUnit.net 由 NUnit v2 的原作者编写<sup>[9](https://learn.microsoft.com/en-us/dotnet/core/testing/)</sup> |
| Go testing、Rust libtest | Go、Rust | 内置于语言工具链的测试支持：`go test` 运行 `_test.go` 文件中以 `Test` 开头的函数，`cargo test` 将代码与 libtest（rustc 内置的单元测试与微基准测试框架）链接<sup>[10](https://go.dev/doc/tutorial/add-a-test)</sup><sup> • </sup><sup>[11](https://doc.rust-lang.org/cargo/commands/cargo-test.html)</sup> |

## xUnit 的起源

xUnit 家族可追溯至 Kent Beck 为 Smalltalk 编写的框架，其描述见于他的论文《Simple Smalltalk Testing: With Patterns》。Beck 认为基于用户界面的测试过于脆弱、实用性不足，并提出用语言本身来编写测试和检查结果。他的框架区分了失败（failure）与错误（error）：失败是预期内的问题，即实际结果与预期结果不同；错误是测试未检查到的意外状况。他建议开发者自行编写单元测试，每个类一个，并将 25% 到 50% 的时间用于开发测试。<sup>[2](https://dionatanmoura.com/wp-content/uploads/2024/06/simple-smalltalk-testing-with-patterns-kent-beck.pdf)</sup>

Erich Gamma 将 SUnit 移植到 Java，创建了 JUnit，xUnit 文献将其视为该架构的参考实现，也是最广泛使用和扩展的单元测试框架。<sup>[1](https://www.oreilly.com/library/view/unit-test-frameworks/0596006896/ch03.html)</sup> 几乎所有单元测试框架都遵循相同的 xUnit 风格——测试用例、夹具、套件和运行器。<sup>[5](https://docs.python.org/3/library/unittest.html?highlight=testcase)</sup>

## 按语言分类的框架

**Java。** JUnit 在 Java 测试中占据主导地位。一项针对 430 万个公开 GitHub Java 项目的实证研究发现，JUnit4 与 JUnit5 组合极为普遍，两者在 9.59% 的项目中同时出现；而第二受欢迎的 Java 测试框架 [TestNG](https://www.edgepedia.org/zh/testng) 约占 3% 的份额，且几乎从不脱离 JUnit 单独使用。<sup>[12](https://madeja.github.io/publications/21-mdpi-applsci-java.pdf)</sup> JUnit 5 将项目重构为三个子项目：JUnit Platform 作为在 JVM 上启动测试框架的基础，提供 `TestEngine` API；Jupiter 作为新的编程和扩展模型；Vintage 作为运行 JUnit 3 和 JUnit 4 测试的引擎。<sup>[3](https://docs.junit.org/5.0.0/user-guide/)</sup> JUnit 6.0.0 于 2025 年 9 月 30 日发布，将基线定为 Java 17 和 Kotlin 2.1，统一了 Platform、Jupiter 和 Vintage 的版本号，并新增了对 Kotlin `suspend` 函数作为测试方法的支持。<sup>[13](https://docs.junit.org/current/release-notes.html)</sup>

**Python。** 标准库的 `unittest` 模块最初受 JUnit 启发，支持测试自动化、共享初始化与关闭代码、将测试聚合为集合，以及测试与报告框架的独立性。<sup>[5](https://docs.python.org/3/library/unittest.html?highlight=testcase)</sup> pytest 是领先的第三方替代方案，使用普通 `assert` 语句并对失败表达式进行详细内省，自动发现测试模块和函数，并提供模块化夹具来管理小型或参数化的长生命周期测试资源；它可以直接运行 unittest 测试套件，并拥有包含超过 1300 个外部插件的插件架构。<sup>[4](https://docs.pytest.org/en/stable/index.html)</sup> pytest 夹具为测试提供已定义且可靠的上下文，例如配置了已知参数的数据库，并可在函数、类、模块、包或整个测试会话范围内复用。<sup>[14](https://pytest.org/en/latest/explanation/fixtures.html)</sup>

**JavaScript 与 TypeScript。** Jest 是一个测试运行器，其测试写法如 `expect(sum(1, 2)).toBe(3)`；它支持快照测试，首次运行创建快照文件，后续运行将输出与之比对。<sup>[6](https://jestjs.io/docs/getting-started)</sup><sup> • </sup><sup>[7](https://jestjs.io/docs/30.0/snapshot-testing)</sup> Mocha 是一个功能丰富的 [JavaScript](https://www.edgepedia.org/zh/javascript) 测试框架，可在 Node.js 和浏览器中运行，默认测试用例超时时间为 2000 毫秒，默认接口为 `bdd`。<sup>[15](https://legacy.mochajs.org/)</sup> [Vitest](https://www.edgepedia.org/zh/vitest) 构建于 Vite 之上，监听模块图并仅重新运行与修改过的源文件或测试文件相关的测试，其工作方式类似于 Vite 中的热模块替换。<sup>[16](https://main.vitest.dev/guide/features)</sup> Testing Library 系列包本身不是测试运行器；它提供以用户为中心的 DOM 查询，可与任何提供 DOM API 的环境配合使用，例如 Jest、Mocha 加 JSDOM，或真实浏览器。<sup>[17](https://testing-library.com/docs/)</sup>

**.NET。** Microsoft 为 .NET 记录了四种流行的测试框架：MSTest、NUnit、TUnit 和 xUnit.net。NUnit 最初从 JUnit 移植而来，此后经过重写，支持多种 .NET 平台；xUnit.net 是由 NUnit v2 原作者编写的免费开源工具；TUnit 完全构建于较新的 Microsoft.Testing.Platform 之上，不支持 VSTest。NUnit 和 xUnit.net 均为 .NET Foundation 的项目。<sup>[9](https://learn.microsoft.com/en-us/dotnet/core/testing/)</sup> MSTest 是 Microsoft 面向所有 .NET 语言的框架，获得全面支持、开源且跨平台，具备数据驱动测试、程序集/类/测试级别的生命周期管理以及并行执行能力。<sup>[18](https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-mstest-intro)</sup>

**C 与 C++。** GoogleTest 是一个基于 xUnit 的 C++ 框架，其断言成对出现：`ASSERT_*` 版本产生致命失败，中止当前函数；`EXPECT_*` 版本产生非致命失败，允许一个测试报告多个问题。测试用 `TEST()` 定义，共享夹具类时用 `TEST_F()` 定义，GoogleTest 为每个测试创建新的夹具对象。<sup>[8](https://github.com/google/googletest/blob/main/docs/primer.md)</sup> 它被 [Chromium](https://www.edgepedia.org/zh/chromium-web-browser) 项目、LLVM 编译器、[Protocol Buffers](https://www.edgepedia.org/zh/protocol-buffers) 和 OpenCV 等采用。<sup>[19](https://github.com/google/googletest)</sup> Catch2 主要是 C++ 的单元测试框架，同时也提供基本的微基准测试功能和简单的 BDD 宏。<sup>[20](https://catch2-temp.readthedocs.io/en/latest/)</sup> 对于 C，Ceedling 构建系统捆绑了 Unity——一个完全内置、不可禁用的 xUnit 风格测试框架——以及用于模拟的 CMock 和用于异常处理的 CException。<sup>[21](https://github.com/throwtheswitch/ceedling/blob/master/docs/mkdocs/testing-guide/frameworks.md)</sup>

**内置测试的语言。** Go 在其工具链中自带测试支持：以 `_test.go` 结尾的文件告知 `go test` 命令其中包含测试函数，函数名以 `Test` 开头，并接受一个 `*testing.T` 参数用于报告。`testing` 包还支持模糊测试（fuzzing），即用随机生成的输入调用函数，以发现单元测试未预料到的缺陷。<sup>[10](https://go.dev/doc/tutorial/add-a-test)</sup><sup> • </sup><sup>[22](https://go.dev/pkg/testing/?m=old)</sup> Rust 的 `cargo test` 编译并执行单元测试、集成测试和文档测试，将代码与 libtest（rustc 内置的单元测试与微基准测试框架）链接；标注 `#[test]` 的函数自动在多线程中运行，由 rustdoc 提取的文档测试在独立进程中并行运行。<sup>[11](https://doc.rust-lang.org/cargo/commands/cargo-test.html)</sup>

**PHP。** PHPUnit 是 PHP 的测试框架；PHPUnit 13 是当前稳定版本，于 2026 年 2 月 6 日发布，PHPUnit 14 已宣布为下一个主要版本。<sup>[23](https://phpunit.de/)</sup>

## 共同特征

大多数框架共享 xUnit 的术语体系：测试用例、测试夹具、测试套件和测试运行器。测试夹具（test fixture）代表执行一个或多个测试所需的准备工作及相关的清理工作，例如创建临时数据库或启动服务器进程；测试用例是单个测试单元；测试套件将用例聚合起来执行；运行器负责编排执行并报告结果。<sup>[5](https://docs.python.org/3/library/unittest.html?highlight=testcase)</sup> 各框架表达这些概念的方式不同：pytest 通过将命名夹具声明为测试函数参数来激活它们，而 GoogleTest 使用带有 `SetUp()` 和 `TearDown()` 方法的夹具类。<sup>[14](https://pytest.org/en/latest/explanation/fixtures.html)</sup><sup> • </sup><sup>[8](https://github.com/google/googletest/blob/main/docs/primer.md)</sup>

## 采用情况

测试框架的采用广泛但不均衡。在 Kochhar 等人对 GitHub 仓库的研究中，所分析的仓库中，61% 至少包含一个测试用例，开发者更多的项目拥有更多测试用例。<sup>[12](https://madeja.github.io/publications/21-mdpi-applsci-java.pdf)</sup> 同一研究团队针对 Java 的研究在 430 万个项目中识别出 26 种不同的单元测试框架在使用，基于 JUnit 的组合远远领先于 TestNG 等替代方案。<sup>[12](https://madeja.github.io/publications/21-mdpi-applsci-java.pdf)</sup> 对选择框架的团队而言，一个实际结论是：选择哪个框架不如拥有测试本身重要；当套件规模变大时，框架的主要差异在于语法、夹具处理以及对排序和并行执行的控制能力。<sup>[9](https://learn.microsoft.com/en-us/dotnet/core/testing/)</sup>

## References

1. [The xUnit Family of Unit Test Frameworks, Unit Test Frameworks (O'Reilly)](https://www.oreilly.com/library/view/unit-test-frameworks/0596006896/ch03.html)
2. [Kent Beck, Simple Smalltalk Testing: With Patterns](https://dionatanmoura.com/wp-content/uploads/2024/06/simple-smalltalk-testing-with-patterns-kent-beck.pdf)
3. [JUnit 5 User Guide](https://docs.junit.org/5.0.0/user-guide/)
4. [pytest: helps you write better programs, pytest documentation](https://docs.pytest.org/en/stable/index.html)
5. [unittest — Unit testing framework, Python documentation](https://docs.python.org/3/library/unittest.html?highlight=testcase)
6. [Getting Started, Jest](https://jestjs.io/docs/getting-started)
7. [Snapshot Testing, Jest](https://jestjs.io/docs/30.0/snapshot-testing)
8. [GoogleTest Primer, google/googletest](https://github.com/google/googletest/blob/main/docs/primer.md)
9. [Testing in .NET, Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/core/testing/)
10. [Add a test, The Go Programming Language](https://go.dev/doc/tutorial/add-a-test)
11. [cargo-test(1), The Cargo Book](https://doc.rust-lang.org/cargo/commands/cargo-test.html)
12. [Empirical Study of Test Case and Test Framework Presence in Public Projects on GitHub, Applied Sciences 2021](https://madeja.github.io/publications/21-mdpi-applsci-java.pdf)
13. [Release Notes, JUnit User Guide](https://docs.junit.org/current/release-notes.html)
14. [About fixtures, pytest documentation](https://pytest.org/en/latest/explanation/fixtures.html)
15. [Mocha documentation](https://legacy.mochajs.org/)
16. [Features, Vitest Guide](https://main.vitest.dev/guide/features)
17. [Introduction, Testing Library](https://testing-library.com/docs/)
18. [MSTest overview, Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-mstest-intro)
19. [google/googletest, GitHub](https://github.com/google/googletest)
20. [Catch2 documentation](https://catch2-temp.readthedocs.io/en/latest/)
21. [Using Unity, CMock & CException, Ceedling documentation](https://github.com/throwtheswitch/ceedling/blob/master/docs/mkdocs/testing-guide/frameworks.md)
22. [Package testing, The Go Programming Language](https://go.dev/pkg/testing/?m=old)
23. [PHPUnit: The testing framework for PHP](https://phpunit.de/)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › 软件工程与开发过程 › 软件测试与质量

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/list-of-unit-testing-frameworks

English original: https://www.edgepedia.org/list-of-unit-testing-frameworks
