今年夏天,我开发了一个用纯c语言编写的嵌入式系统。这是我所在公司接管的一个现有项目。我已经非常习惯使用JUnit在Java中编写单元测试,但不知道为现有代码(需要重构)和添加到系统中的新代码编写单元测试的最佳方法是什么。

有什么项目可以让单元测试纯C代码像使用JUnit测试Java代码一样简单吗?任何特别适用于嵌入式开发(交叉编译到arm-linux平台)的见解都将非常感谢。


当前回答

Minunit是一个非常简单的单元测试框架。 我用它来单元测试c微控制器代码avr。

其他回答

使用的一种技术是使用c++ xUnit框架(和c++编译器)开发单元测试代码,同时将目标系统的源代码维护为C模块。

确保在交叉编译器下定期编译C源代码,如果可能的话自动使用单元测试。

如果您的目标是Win32平台或NT内核模式,您应该看看cfix。

API完整性检查器- C/ c++库的测试框架:

An automatic generator of basic unit tests for a shared C/C++ library. It is able to generate reasonable (in most, but unfortunately not all, cases) input data for parameters and compose simple ("sanity" or "shallow"-quality) test cases for every function in the API through the analysis of declarations in header files. The quality of generated tests allows to check absence of critical errors in simple use cases. The tool is able to build and execute generated tests and detect crashes (segfaults), aborts, all kinds of emitted signals, non-zero program return code and program hanging.

例子:

fontconfig 2.8.0测试套件 FreeType 2.4.8的测试套件

Michael Feather的书《有效地使用遗留代码》介绍了很多专门用于C开发期间单元测试的技术。

有一些与依赖注入相关的技术是特定于C语言的,我在其他任何地方都没有见过。

LibU (http://koanlogic.com/libu)有一个单元测试模块,允许显式测试套件/用例依赖、测试隔离、并行执行和可定制的报告格式化器(默认格式是xml和txt)。

这个库是BSD许可的,包含许多其他有用的模块——网络、调试、常用的数据结构、配置等等——如果你的项目需要它们的话……