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

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


当前回答

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

其他回答

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

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

除了我明显的偏见

http://code.google.com/p/seatest/

是一个很好的简单的方法来单元测试C代码。模仿xUnit

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的测试套件

如果你熟悉JUnit,那么我推荐CppUnit。 http://cppunit.sourceforge.net/cppunit-wiki

这是假设你有c++编译器来做单元测试。如果没有,那么我必须同意亚当·罗森菲尔德的观点,支票就是你想要的。

在阅读Minunit之后,我认为更好的方法是在assert宏中进行测试,我使用了很多防御程序技术。所以我使用Minunit混合标准断言的相同思想。您可以在k0ga的博客中看到我的框架(一个好名字可以是NoMinunit)