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

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

问题描述

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

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.

是否有任何项目可以使纯 C 代码的单元测试与使用 JUnit 对 Java 代码进行单元测试一样简单?任何特别适用于嵌入式开发(交叉编译到 arm-linux 平台)的见解将不胜感激.

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

推荐答案

C 中的一个单元测试框架是 Check;可以在 此处 找到 C 语言中的单元测试框架列表并进行复制以下.根据您的运行时有多少标准库函数,您可能会也可能无法使用其中之一.

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

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.

与 Check 大致相同,包括分叉以在单独的地址空间中运行单元测试(实际上,Check 的原作者借用了 GNU Autounit 的想法).GNU Autounit 广泛使用 GLib,这意味着链接等需要特殊选项,但这对您来说可能不是大问题,特别是如果您已经在使用 GTK 或 GLib.请参阅 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.

同样使用 GLib,但不会 fork 来保护单元测试的地址空间.

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

标准 C,有 Win32 GUI 实现计划.当前不分叉或以其他方式保护单元测试的地址空间.在早期开发中.请参阅 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.

一个简单的框架,只有一个 .c 和一个 .h 文件,您可以将其放入源代码树中.请参阅 CuTest 主页.

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

C++ 的首要单元测试框架;您还可以使用它来测试 C 代码.它稳定,积极开发,并具有 GUI 界面.不将 CppUnit 用于 C 的主要原因首先是它非常大,其次您必须用 C++ 编写测试,这意味着您需要一个 C++ 编译器.如果这些听起来不是问题,那么绝对值得考虑,以及其他 C++ 单元测试框架.请参阅 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(Embedded Unit)是另一个嵌入式系统的单元测试框架.这个似乎被 AceUnit 取代了.嵌入式单元主页.

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 实现,显然仍处于早期开发阶段.请参阅CUnit for Ando 先生主页.

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

此列表上次更新于 2008 年 3 月.

更多框架:

CMocka

CMocka 是一个支持模拟对象的 C 测试框架.它易于使用和设置.

More frameworks:

CMocka

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

请参阅CMocka 主页.

Criterion 是一个跨平台的 C 单元测试框架,支持自动测试注册、参数化测试、理论,并且可以输出为多种格式,包括 TAP 和 JUnit XML.每个测试都在自己的进程中运行,因此可以根据需要报告或测试信号和崩溃.

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 是一个通用的单元测试工具,对 C 有很大的支持.它可以帮助创建 Makefile,生成用最少的迭代表"编码的大量测试用例,沿着状态机走,生成 C 存根等等.一般方法非常独特:判决基于好标准输出/坏标准输出".但是,比较功能是灵活的.因此,任何类型的脚本都可以用于检查.它可以应用于任何可以产生标准输出的语言.

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.

请参阅HWUT 主页.

适用于 C 和 C++ 的现代、可移植、跨语言单元测试和模拟框架.它提供了一个可选的 BDD 符号、一个模拟库、在单个进程中运行它的能力(使调试更容易).可以使用自动发现测试功能的测试运行器.但您可以通过编程方式创建自己的.

A modern, portable, cross-language unit testing and mocking framework for C and C++. It offers an optional BDD notation, a mocking library, the ability to run it in a single process (to make debugging easier). A test runner which discover automatically the test functions is available. But you can create your own programmatically.

CGreen 手册中解释了所有这些功能(以及更多).

All those features (and more) are explained in the CGreen manual.

维基百科在单元测试框架列表:C

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

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