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

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


当前回答

我说几乎和ratkok一样,但如果你有一个嵌入的扭曲单元测试,那么……

Unity——强烈推荐用于单元测试C代码的框架。

#include <unity.h>

void test_true_should_be_true(void)
{
    TEST_ASSERT_TRUE(true);
}

int main(void)
{
    UNITY_BEGIN();
    RUN_TEST(test_true_should_be_true);
    return UNITY_END();
}

书中提到的嵌入式C线程TDD中的例子是使用Unity(和CppUTest)编写的。

其他回答

我很惊讶没有人提到卡特(http://cutter.sourceforge.net/) 你可以测试C和c++,它与autotools无缝集成,并且有一个非常好的教程。

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

这是CUnit

嵌入式单元是嵌入式C系统的单元测试框架。它的设计抄袭了JUnit和CUnit等,然后为嵌入式C系统做了一些调整。嵌入式单元不需要标准C库。所有对象都分配到const区域。

Tessy自动化了嵌入式软件的单元测试。

首先,看看这里:http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C

我的公司有一个客户使用的C库。我们使用CxxTest(一个c++单元测试库)来测试代码。CppUnit也可以工作。如果你被C卡住了,我推荐RCUNIT(但CUnit也很好)。

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