单元测试C代码 [英] Unit Testing C Code

查看:800
本文介绍了单元测试C代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今年夏天在C写了一个嵌入式系统,这是一个现有的项目,我所在的公司已经接管了。我已经习惯于使用JUnit在Java中编写单元测试,但是对于为现有代码(需要重构)以及添加到系统中的新代码编写单元测试的最佳方式而言是一个不便。



有没有办法使单元测试简单的C代码像单元测试Java代码一样简单,例如, JUnit ?将非常感谢任何专门针对嵌入式开发(跨编译到arm-linux平台)的洞察力。

解决方案

C中的单元测试框架是检查; C中的单元测试框架列表可以在 here 中找到并被复制下面。根据你的运行库有多少标准的库函数,你可能不能使用其中的一个。


AceUnit



AceUnit(高级C和嵌入式单元)将自己作为一个舒适的C代码单元测试框架。它试图模仿JUnit 4.x,并包含类似反射的功能。 AceUnit可以在资源约束环境中使用,例如嵌入式软件开发,并且重要的是它在不能包含单个标准头文件并且无法从ANSI / ISO C库调用单个标准C函数的环境中运行良好。它还有一个Windows端口。尽管作者已经表示有兴趣添加这样的功能,但它并不使用叉子来捕获信号。请参阅 AceUnit主页



GNU Autounit



与Check相似,包括在单独的地址空间中运行单元测试(实际上,Check的原始作者借用了GNU Autounit的想法)。 GNU Autounit广泛使用GLib,这意味着链接和这样需要特殊选项,但这可能不是一个大问题,特别是如果您已经使用GTK或GLib。请参阅 GNU Autounit主页



cUnit



还使用GLib,但是叉不保护单元测试的地址空间。



CUnit



标准C,具有Win32 GUI实现的计划。目前叉叉或其他方式保护单元测试的地址空间。在早期发展。请参阅 CUnit主页



CuTest



一个简单的框架,只有一个.c和一个.h文件,你放入你的源代码树。请参阅 CuTest主页



CppUnit



C ++的首要单元测试框架;你也可以用它来测试C代码。它稳定,积极开发,并具有GUI界面。不使用CppUnit C的主要原因首先是它是相当大的,其次你必须用C ++编写你的测试,这意味着你需要一个C ++编译器。如果这些听起来不像是担心,那么与其他C ++单元测试框架一起,绝对值得考虑。请参阅 CppUnit主页



embUnit



embUnit(嵌入式单元)是嵌入式系统的另一个单元测试框架。这个似乎被AceUnit所取代。 嵌入式单位主页



MinUnit



一组最小的宏,就是这样!要点是显示单元测试代码的容易程度。请参阅 MinUnit主页



安藤先生的CUnit



一个相当新的CUnit实现,显然还处于早期开发阶段。请参阅安藤先生主页的CUnit



此列表最近更新于2008年3月。


其他:



CMocka



CMocka是支持模拟对象的C的测试框架。它很容易使用和设置。 CMocka 官方主页。



条件



标准是一个跨平台C单元测试框架,支持自动测试注册,参数化测试,理论,可以输出到多种格式,包括TAP和JUnit XML。每个测试都在自己的过程中运行,因此如果需要,可以报告或测试信号和崩溃。有关详细信息,请参阅标准主页



HWUT



HWUT是一个通用的单元测试工具,对C有很大的支持。它可以帮助创建Makefile,生成以最小迭代表编码的大量测试用例状态机,生成C-stub等等。一般方法是非常独特的:判决是基于良好的stdout / bad stdout。比较功能虽然灵活,因此,可以使用任何类型的脚本进行检查。它可以应用于可以产生标准输出的任何语言。请参阅 HWUT 主页。


I worked on an embedded system this summer written in straight C. It was an existing project that the company I work for had taken over. I have become quite accustomed to writing unit tests in Java using JUnit but was at a loss as to the best way to write unit tests for existing code (which needed refactoring) as well as new code added to the system.

Is there any way to make unit testing plain C code as easy as unit testing Java code with, for example, JUnit? Any insight that would apply specifically to embedded development (cross-compiling to arm-linux platform) would be greatly appreciated.

解决方案

One unit testing framework in C is Check; a list of unit testing frameworks in C can be found here and is reproduced below. Depending on how many standard library functions your runtime has, you may or not be able to use one of those.

AceUnit

AceUnit (Advanced C and Embedded Unit) bills itself as a comfortable C code unit test framework. It tries to mimick JUnit 4.x and includes reflection-like capabilities. AceUnit can be used in resource constraint environments, e.g. embedded software development, and importantly it runs fine in environments where you cannot include a single standard header file and cannot invoke a single standard C function from the ANSI / ISO C libraries. It also has a Windows port. It does not use forks to trap signals, although the authors have expressed interest in adding such a feature. See the AceUnit homepage.

GNU Autounit

Much along the same lines as Check, including forking to run unit tests in a separate address space (in fact, the original author of Check borrowed the idea from GNU Autounit). GNU Autounit uses GLib extensively, which means that linking and such need special options, but this may not be a big problem to you, especially if you are already using GTK or GLib. See the GNU Autounit homepage.

cUnit

Also uses GLib, but does not fork to protect the address space of unit tests.

CUnit

Standard C, with plans for a Win32 GUI implementation. Does not currently fork or otherwise protect the address space of unit tests. In early development. See the CUnit homepage.

CuTest

A simple framework with just one .c and one .h file that you drop into your source tree. See the CuTest homepage.

CppUnit

The premier unit testing framework for C++; you can also use it to test C code. It is stable, actively developed, and has a GUI interface. The primary reasons not to use CppUnit for C are first that it is quite big, and second you have to write your tests in C++, which means you need a C++ compiler. If these don’t sound like concerns, it is definitely worth considering, along with other C++ unit testing frameworks. See the CppUnit homepage.

embUnit

embUnit (Embedded Unit) is another unit test framework for embedded systems. This one appears to be superseded by AceUnit. Embedded Unit homepage.

MinUnit

A minimal set of macros and that’s it! The point is to show how easy it is to unit test your code. See the MinUnit homepage.

CUnit for Mr. Ando

A CUnit implementation that is fairly new, and apparently still in early development. See the CUnit for Mr. Ando homepage.

This list was last updated in March 2008.

Others:

CMocka

CMocka is a test framework for C with support for mock objects. It's easy to use and setup. CMocka official homepage.

Criterion

Criterion is a cross-platform C unit testing framework supporting automatic test registration, parameterized tests, theories, and that can output to multiple formats, including TAP and JUnit XML. Each test is run in its own process, so signals and crashes can be reported or tested if needed. See the Criterion homepage for more information.

HWUT

HWUT is a general Unit Test tool with great support for C. It can help to create Makefiles, generate massive test cases coded in minimal 'iteration tables', walk along state machines, generate C-stubs and more. The general approach is pretty unique: Verdicts are based on 'good stdout/bad stdout'. The comparison function, though, is flexible. Thus, any type of script may be used for checking. It may be applied to any language that can produce standard output. See HWUT homepage.

这篇关于单元测试C代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆