单元测试 C 代码

发布于 2024-07-05 08:13:35 字数 1833 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

夜雨飘雪 2024-07-12 08:13:36

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

AceUnit

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

GNU 自动单元

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

cUnit

也使用 GLib,但不分叉以保护单元测试的地址空间。

CUnit

标准 C,计划用于 Win32 GUI 实现。 当前不分叉或以其他方式保护单元测试的地址空间。 处于早期开发阶段。 请参阅 CUnit 主页

Cu测试

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

CppUnit

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

embUnit

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

最小单位

一组最小的宏,就是这样! 重点是展示对代码进行单元测试是多么容易。 请参阅 MinUnit 主页

安藤先生的CUnit

一个相当新的 CUnit 实现,显然仍处于早期开发阶段。 请参阅CUnit for Mr. Ando 主页

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

更多框架:

CMocka

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

请参阅CMocka 主页

Criterion

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

有关更多信息,请参阅Criterion 主页

HWUT

HWUT 是一个通用的单元测试工具,对 C 语言有很好的支持。它可以帮助创建 Makefile、生成用最小“迭代表”编码的大量测试用例、沿着状态机运行、生成 C 存根等等。 一般方法非常独特:判决基于“好标准输出/坏标准输出”。 不过,比较功能很灵活。 因此,任何类型的脚本都可以用于检查。 它可以应用于任何可以产生标准输出的语言。

请参阅HWUT 主页

CGreen

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

所有这些功能(以及更多功能)均在CGreen 手册中进行了解释。

维基百科在 单元测试框架列表:C 下提供了 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

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.

More frameworks:

CMocka

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

See the CMocka 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 the HWUT homepage.

CGreen

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.

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

Wikipedia gives a detailed list of C unit testing frameworks under List of unit testing frameworks: C

救赎№ 2024-07-12 08:13:36

我个人喜欢 Google 测试框架

测试 C 代码的真正困难是打破对外部模块的依赖,以便您可以将代码隔离在单元中。 当您尝试围绕遗留代码进行测试时,这可能尤其成问题。 在这种情况下,我经常发现自己使用链接器在测试中使用存根函数。

这就是人们在谈论“接缝”时所指的内容。 在 C 中,您唯一的选择实际上是使用预处理器或链接器来模拟您的依赖项。

我的一个 C 项目中的典型测试套件可能如下所示:

#include "myimplementationfile.c"
#include <gtest/gtest.h>

// Mock out external dependency on mylogger.o
void Logger_log(...){}

TEST(FactorialTest, Zero) {
    EXPECT_EQ(1, Factorial(0));
}

请注意,您实际上包含 C 文件而不是头文件。 这提供了访问所有静态数据成员的优点。 在这里,我模拟了我的记录器(可能位于 logger.o 中并给出了一个空实现。这意味着测试文件独立于代码库的其余部分进行编译和链接,并独立执行。

至于交叉编译代码,为此,您需要在目标上有良好的设施。我已经在 PowerPC 架构上使用 googletest 交叉编译到 Linux 来完成此操作,因为您有一个完整的 shell 和操作系统来收集结果(对于不太丰富的环境)。我将其归类为没有完整操作系统的任何东西,您应该只在主机上构建并运行。无论如何,您都应该这样做,这样您就可以作为构建的一部分自动运行测试,

因为事实上,测试 C++ 代码通常要容易得多。 OO 代码通常比过程代码耦合性要低得多(当然,这在很大程度上取决于编码风格)。此外,在 C++ 中,您可以使用依赖注入和方法重写等技巧来将接缝插入到以其他方式封装的代码中

关于测试遗留代码的优秀书籍。 在其中一章中,他介绍了我强烈推荐的处理非面向对象代码的技术。

编辑:我编写了一个关于单元测试程序代码的博客文章GitHub 上提供源代码一个>。

编辑:有一本新书即将出版来自实用程序员,专门解决单元测试 C 代码,我强烈推荐

Personally I like the Google Test framework.

The real difficulty in testing C code is breaking the dependencies on external modules so you can isolate code in units. This can be especially problematic when you are trying to get tests around legacy code. In this case I often find myself using the linker to use stubs functions in tests.

This is what people are referring to when they talk about "seams". In C your only option really is to use the pre-processor or the linker to mock out your dependencies.

A typical test suite in one of my C projects might look like this:

#include "myimplementationfile.c"
#include <gtest/gtest.h>

// Mock out external dependency on mylogger.o
void Logger_log(...){}

TEST(FactorialTest, Zero) {
    EXPECT_EQ(1, Factorial(0));
}

Note that you are actually including the C file and not the header file. This gives the advantage of access to all the static data members. Here I mock out my logger (which might be in logger.o and give an empty implementation. This means that the test file compiles and links independently from the rest of the code base and executes in isolation.

As for cross-compiling the code, for this to work you need good facilities on the target. I have done this with googletest cross compiled to Linux on a PowerPC architecture. This makes sense because there you have a full shell and os to gather your results. For less rich environments (which I classify as anything without a full OS) you should just build and run on the host. You should do this anyway so you can run the tests automatically as part of the build.

I find testing C++ code is generally much easier due to the fact that OO code is in general much less coupled than procedural (of course this depends a lot on coding style). Also in C++ you can use tricks like dependency injection and method overriding to get seams into code that is otherwise encapsulated.

Michael Feathers has an excellent book about testing legacy code. In one chapter he covers techniques for dealing with non-OO code which I highly recommend.

Edit: I've written a blog post about unit testing procedural code, with source available on GitHub.

Edit: There is a new book coming out from the Pragmatic Programmers that specifically addresses unit testing C code which I highly recommend.

热血少△年 2024-07-12 08:13:36

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

Minunit is an incredibly simple unit testing framework.
I'm using it to unit test c microcontroller code for avr.

℉服软 2024-07-12 08:13:36

我说的几乎与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)编写的。

I say almost the same as ratkok but if you have a embedded twist to the unit tests then...

Unity - Highly recommended framework for unit testing C code.

#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();
}

The examples in the book that is mentioned in this thread TDD for embedded C are written using Unity (and CppUTest).

狼性发作 2024-07-12 08:13:36

我目前正在使用 CuTest 单元测试框架:

http://cutest.sourceforge.net/

它非常适合嵌入式系统,因为它非常轻量且简单。 我可以毫无问题地让它在目标平台和桌面上运行。 除了编写单元测试之外,所需要的只是:

  • 在任何地方都包含一个头文件
    您将 CuTest 例程称为
  • 一个附加的“C”文件
    编译/链接到图像中,
  • 将一些简单的代码添加到 main 中
    设置并调用单元测试 - I
    只需将其放在一个特殊的 main() 中即可
    被编译的函数如果
    UNITTEST 是在
    建造。

系统需要支持堆和一些 stdio 功能(并非所有嵌入式系统都具备)。 但代码非常简单,如果您的平台没有这些要求,您可能可以使用这些要求的替代方案。

通过明智地使用 extern "C"{} 块,它还可以很好地支持测试 C++。

I'm currently using the CuTest unit test framework:

http://cutest.sourceforge.net/

It's ideal for embedded systems as it's very lightweight and simple. I had no problems getting it to work on the target platform as well as on the desktop. In addition to writing the unit tests, all that's required is:

  • a header file included wherever
    you're calling the CuTest routines
  • a single additional 'C' file to be
    compiled/linked into the image
  • some simple code added to to main to
    set up and call the unit tests - I
    just have this in a special main()
    function that gets compiled if
    UNITTEST is defined during the
    build.

The system needs to support a heap and some stdio functionality (which not all embedded systems have). But the code is simple enough that you could probably work in alternatives to those requirements if your platform doesn't have them.

With some judicious use of extern "C"{} blocks it also supports testing C++ just fine.

晨与橙与城 2024-07-12 08:13:36

您可能还想看看 libtap,一个 C 测试框架,它输出 Test Anything Protocol (TAP ),从而与针对该技术推出的各种工具很好地集成。 它主要用于动态语言世界,但它易于使用并且变得非常流行。

一个例子:

#include <tap.h>

int main () {
    plan(5);

    ok(3 == 3);
    is("fnord", "eek", "two different strings not that way?");
    ok(3 <= 8732, "%d <= %d", 3, 8732);
    like("fnord", "f(yes|no)r*[a-f]$");
    cmp_ok(3, ">=", 10);

    done_testing();
}

You also might want to take a look at libtap, a C testing framework which outputs the Test Anything Protocol (TAP) and thus integrates well with a variety of tools coming out for this technology. It's mostly used in the dynamic language world, but it's easy to use and becoming very popular.

An example:

#include <tap.h>

int main () {
    plan(5);

    ok(3 == 3);
    is("fnord", "eek", "two different strings not that way?");
    ok(3 <= 8732, "%d <= %d", 3, 8732);
    like("fnord", "f(yes|no)r*[a-f]$");
    cmp_ok(3, ">=", 10);

    done_testing();
}
冷心人i 2024-07-12 08:13:36

有一个优雅的 C 单元测试框架,支持模拟对象,称为 cmocka。 它只需要标准 C 库,可在一系列计算平台(包括嵌入式)上运行并使用不同的编译器。

它还支持不同的消息输出格式,例如 Subunit、Test Anything Protocol 和 jUnit XML 报告。

cmocka 的创建也可以在嵌入式平台上工作,并且还支持 Windows。

一个简单的测试如下所示:

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

/* A test case that does nothing and succeeds. */
static void null_test_success(void **state) {
    (void) state; /* unused */
}

int main(void) {
    const struct CMUnitTest tests[] = {
        cmocka_unit_test(null_test_success),
    };
    return cmocka_run_group_tests(tests, NULL, NULL);
}

API 已完整记录,并且源代码中包含几个示例。

要开始使用 cmocka,您应该阅读 LWN.net 上的文章:在 C 中使用模拟对象进行单元测试

cmocka 1.0 已发布2015 年 2 月。

There is an elegant unit testing framework for C with support for mock objects called cmocka. It only requires the standard C library, works on a range of computing platforms (including embedded) and with different compilers.

It also has support for different message output formats like Subunit, Test Anything Protocol and jUnit XML reports.

cmocka has been created to also work on embedded platforms and also has Windows support.

A simple test looks like this:

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

/* A test case that does nothing and succeeds. */
static void null_test_success(void **state) {
    (void) state; /* unused */
}

int main(void) {
    const struct CMUnitTest tests[] = {
        cmocka_unit_test(null_test_success),
    };
    return cmocka_run_group_tests(tests, NULL, NULL);
}

The API is fully documented and several examples are part of the source code.

To get started with cmocka you should read the article on LWN.net: Unit testing with mock objects in C

cmocka 1.0 has been released February 2015.

夜光 2024-07-12 08:13:36

在我开始寻找模拟函数的方法之前,我并没有对遗留的 C 应用程序进行深入的测试。 我非常需要模拟来将我想要测试的 C 文件与其他文件隔离开来。 我尝试了 cmock,我想我会采用它。

Cmock 扫描头文件并根据它找到的原型生成模拟函数。 模拟将允许您完美隔离地测试 C 文件。 您所要做的就是将测试文件与模拟链接,而不是与真实的对象文件链接。

cmock 的另一个优点是,它将验证传递给模拟函数的参数,并且允许您指定模拟应提供的返回值。 这对于测试函数中不同的执行流程非常有用。

测试由典型的 testA()、testB() 函数组成,您可以在其中构建期望、调用函数来测试和检查断言。

最后一步是为您的测试统一生成一个运行程序。 Cmock 与统一测试框架绑定。 Unity 与任何其他单元测试框架一样易于学习。

非常值得一试,而且很容易掌握:

http://sourceforge.net/apps/trac/cmock /wiki

更新 1

我正在研究的另一个框架是 Cmockery。

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

它是一个纯C框架支持单元测试和模拟。 它不依赖 ruby​​(与 Cmock 相反),并且对外部库的依赖很少。

它需要更多的手动工作来设置模拟,因为它不生成代码。 这并不代表现有项目需要做很多工作,因为原型不会改变太多:一旦你有了模拟,你暂时不需要改变它们(这是我的情况)。 额外的输入提供了对模拟的完全控制。 如果有什么你不喜欢的,你只需改变你的模拟。

不需要特殊的测试运行程序。 您只需要创建一个测试数组并将其传递给 run_tests 函数。 这里也需要更多的手动工作,但我绝对喜欢独立自主框架的想法。

另外它还包含一些我不知道的漂亮的 C 技巧。

总的来说,Cmockery 需要对模拟有更多的了解才能开始。 示例应该可以帮助您克服这个问题。 看起来它可以用更简单的机制来完成这项工作。

I didn't get far testing a legacy C application before I started looking for a way to mock functions. I needed mocks badly to isolate the C file I want to test from others. I gave cmock a try and I think I will adopt it.

Cmock scans header files and generates mock functions based on prototypes it finds. Mocks will allow you to test a C file in perfect isolation. All you will have to do is to link your test file with mocks instead of your real object files.

Another advantage of cmock is that it will validate parameters passed to mocked functions, and it will let you specify what return value the mocks should provide. This is very useful to test different flows of execution in your functions.

Tests consist of the typical testA(), testB() functions in which you build expectations, call functions to test and check asserts.

The last step is to generate a runner for your tests with unity. Cmock is tied to the unity test framework. Unity is as easy to learn as any other unit test framework.

Well worth a try and quite easy to grasp:

http://sourceforge.net/apps/trac/cmock/wiki

Update 1

Another framework I am investigating is Cmockery.

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

It is a pure C framework supporting unit testing and mocking. It has no dependency on ruby (contrary to Cmock) and it has very little dependency on external libs.

It requires a bit more manual work to setup mocks because it does no code generation. That does not represent a lot of work for an existing project since prototypes won't change much: once you have your mocks, you won't need to change them for a while (this is my case). Extra typing provides complete control of mocks. If there is something you don't like, you simply change your mock.

No need of a special test runner. You only need need to create an array of tests and pass it to a run_tests function. A bit more manual work here too but I definitely like the idea of a self-contained autonomous framework.

Plus it contains some nifty C tricks I didn't know.

Overall Cmockery needs a bit more understanding of mocks to get started. Examples should help you overcome this. It looks like it can do the job with simpler mechanics.

瀟灑尐姊 2024-07-12 08:13:36

我们编写了CHEAT(托管在 GitHub),易于使用和移植。

它没有依赖项,无需安装或配置。
只需要一个头文件和一个测试用例。

#include <cheat.h>

CHEAT_TEST(mathematics_still_work,
    cheat_assert(2 + 2 == 4);
    cheat_assert_not(2 + 2 == 5);
)

测试编译成可执行文件,负责运行测试并报告其结果。

$ gcc -I . tests.c
$ ./a.out
..
---
2 successful of 2 run
SUCCESS

它也有漂亮的颜色。

We wrote CHEAT (hosted on GitHub) for easy usability and portability.

It has no dependencies and requires no installation or configuration.
Only a header file and a test case is needed.

#include <cheat.h>

CHEAT_TEST(mathematics_still_work,
    cheat_assert(2 + 2 == 4);
    cheat_assert_not(2 + 2 == 5);
)

Tests compile into an executable that takes care of running the tests and reporting their outcomes.

$ gcc -I . tests.c
$ ./a.out
..
---
2 successful of 2 run
SUCCESS

It has pretty colors too.

天邊彩虹 2024-07-12 08:13:36

As a C newbie, I found the slides called Test driven development in C very helpful. Basically, it uses the standard assert() together with && to deliver a message, without any external dependencies. If someone is used to a full stack testing framework, this probably won't do :)

夜深人未静 2024-07-12 08:13:36

CUnit

Embedded Unit 是嵌入式 C 系统的单元测试框架。 它的设计复制自 JUnit 和 CUnit 等,然后针对嵌入式 C 系统进行了一些修改。 嵌入式单元不需要 std C 库。 所有对象都分配到 const 区域。

Tessy 自动执行嵌入式软件的单元测试。

There is CUnit

And Embedded Unit is unit testing framework for Embedded C System. Its design was copied from JUnit and CUnit and more, and then adapted somewhat for Embedded C System. Embedded Unit does not require std C libs. All objects are allocated to const area.

And Tessy automates the unit testing of embedded software.

像极了他 2024-07-12 08:13:36

我不使用框架,我只是使用自动工具“检查”目标支持。 实现“main”并使用断言。

我的测试目录 Makefile.am 如下所示:

check_PROGRAMS = test_oe_amqp

test_oe_amqp_SOURCES = test_oe_amqp.c
test_oe_amqp_LDADD = -L$(top_builddir)/components/common -loecommon
test_oe_amqp_CFLAGS = -I$(top_srcdir)/components/common -static

TESTS = test_oe_amqp

I don't use a framework, I just use autotools "check" target support. Implement a "main" and use assert(s).

My test dir Makefile.am(s) look like:

check_PROGRAMS = test_oe_amqp

test_oe_amqp_SOURCES = test_oe_amqp.c
test_oe_amqp_LDADD = -L$(top_builddir)/components/common -loecommon
test_oe_amqp_CFLAGS = -I$(top_srcdir)/components/common -static

TESTS = test_oe_amqp
梦途 2024-07-12 08:13:36

Michael Feather 的书《有效处理遗留代码》介绍了许多 C 开发期间特定于单元测试的技术。

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

Michael Feather's book "Working Effectively with Legacy Code" presents a lot of techniques specific to unit testing during C development.

There are techniques related to dependency injection that are specific to C which I haven't seen anywhere else.

心如狂蝶 2024-07-12 08:13:36

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

书中提到的示例 嵌入式 C 的 TDD 是使用 CppUTest 编写的。

CppUTest - Highly recommended framework for unit testing C code.

The examples in the book that is mentioned in this thread TDD for embedded C are written using CppUTest.

月光色 2024-07-12 08:13:36

我将 CxxTest 用于嵌入式 c/c++ 环境(主要是 C++)。

我更喜欢 CxxTest,因为它有一个 perl/python 脚本来构建测试运行程序。 经过一个小斜率的设置(更小,因为您不必编写测试运行程序),它非常容易使用(包括示例和有用的文档)。 大多数工作是设置代码访问的“硬件”,以便我可以有效地进行单元/模块测试。 之后就可以轻松添加新的单元测试用例。

如前所述,它是一个 C/C++ 单元测试框架。 所以你需要一个 C++ 编译器。

CxxTest 用户指南
CxxTest 维基

I use CxxTest for an embedded c/c++ environment (primarily C++).

I prefer CxxTest because it has a perl/python script to build the test runner. After a small slope to get it setup (smaller still since you don't have to write the test runner), it's pretty easy to use (includes samples and useful documentation). The most work was setting up the 'hardware' the code accesses so I could unit/module test effectively. After that it's easy to add new unit test cases.

As mentioned previously it is a C/C++ unit test framework. So you will need a C++ compiler.

CxxTest User Guide
CxxTest Wiki

醉生梦死 2024-07-12 08:13:36

除了我明显的偏见

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

之外一种对 C 代码进行单元测试的简单好方法。 模仿 xUnit

other than my obvious bias

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

is a nice simple way to unit test C code. mimics xUnit

墨落成白 2024-07-12 08:13:36

阅读 Minunit 后,我​​认为更好的方法是在断言宏中进行测试,我使用的方法很像防御程序技术。 所以我使用了 Minunit 与标准断言混合的相同想法。 你可以在 k0ga 的博客

After reading Minunit I thought a better way was base the test in assert macro which I use a lot like defensive program technique. So I used the same idea of Minunit mixed with standard assert. You can see my framework (a good name could be NoMinunit) in k0ga's blog

游魂 2024-07-12 08:13:36

谷歌拥有优秀的测试框架。 https://github.com/google/googletest/blob/ master/googletest/docs/primer.md

是的,据我所知它可以与纯 C 一起使用,即不需要 C++ 功能(可能需要 C++ 编译器,不确定)。

Google has excellent testing framework. https://github.com/google/googletest/blob/master/googletest/docs/primer.md

And yes, as far as I see it will work with plain C, i.e. doesn't require C++ features (may require C++ compiler, not sure).

兮子 2024-07-12 08:13:36

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

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

If you are familiar with JUnit then I recommend CppUnit.
http://cppunit.sourceforge.net/cppunit-wiki

That is assuming you have c++ compiler to do the unit tests. if not then I have to agree with Adam Rosenfield that check is what you want.

调妓 2024-07-12 08:13:36

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

我公司有一个供客户使用的 C 库。 我们使用CxxTest(一个C++单元测试库)来测试代码。 CppUnit 也可以工作。 如果您陷入 C 困境,我推荐 RCUNIT(但 CUnit 也很好)。

First, look here: http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#C

My company has a C library our customers use. We use CxxTest (a C++ unit test library) to test the code. CppUnit will also work. If you're stuck in C, I'd recommend RCUNIT (but CUnit is good too).

过去的过去 2024-07-12 08:13:36

Cmockery 是一个最近启动的项目,它包含一个非常简单易用的 C 库,用于编写单元测试。

Cmockery is a recently launched project that consists on a very simple to use C library for writing unit tests.

朱染 2024-07-12 08:13:36

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

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

One technique to use is to develop the unit test code with a C++ xUnit framework (and C++ compiler), while maintaining the source for the target system as C modules.

Make sure you regularly compile your C source under your cross-compiler, automatically with your unit tests if possible.

深海不蓝 2024-07-12 08:13:36

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

该库已获得 BSD 许可,并包含许多其他有用的模块 - 网络、调试、常用数据结构、配置等 - 如果您在项目中需要它们......

LibU (http://koanlogic.com/libu) has an unit test module that allows explicit test suite/case dependencies, test isolation, parallel execution and a customizable report formatter (default formats are xml and txt).

The library is BSD licensed and contains many other useful modules - networking, debugging, commonly used data structures, configuration, etc. - should you need them in your projects ...

十级心震 2024-07-12 08:13:36

我很惊讶没有人提到Cutter (http://cutter.sourceforge.net/)
您可以测试 C 和 C++,它与自动工具无缝集成,并且有一个非常好的教程可用。

I'm surprised that no one mentioned Cutter (http://cutter.sourceforge.net/)
You can test C and C++, it seamlessly integrates with autotools and has a really nice tutorial available.

初心 2024-07-12 08:13:36

在对目标进行测试之前,我使用 RCUNIT 对 PC 上的嵌入式代码进行了一些单元测试。 良好的硬件接口抽象很重要,否则字节序和内存映射寄存器会杀了你。

I used RCUNIT to do some unit testing for embedded code on PC before testing on the target. Good hardware interface abstraction is important else endianness and memory mapped registers are going to kill you.

累赘 2024-07-12 08:13:36

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

共享 C/C++ 库的基本单元测试的自动生成器。 它能够为参数生成合理的(在大多数情况下,但不幸的是不是全部情况)输入数据,并通过分析标头中的声明为 API 中的每个函数编写简单的(“健全”或“浅薄”质量)测试用例文件。

生成的测试的质量允许检查简单用例中是否存在严重错误。 该工具能够构建和执行生成的测试,并检测崩溃(段错误)、中止、各种发出的信号、非零程序返回代码和程序挂起。

示例:

API Sanity Checker — test framework for C/C++ libraries:

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.

Examples:

烂柯人 2024-07-12 08:13:36

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

In case you are targeting Win32 platforms or NT kernel mode, you should have a look at cfix.

小…红帽 2024-07-12 08:13:36

如果您仍在寻找测试框架,CUnitWin32 是一个适用于 Win32/ NT平台。

这解决了我在使用其他测试框架时遇到的一个基本问题。 即全局/静态变量处于确定性状态,因为每个测试都是作为单独的进程执行的。

If you're still on the hunt for test frameworks, CUnitWin32 is one for the Win32/NT platform.

This solves one fundamental problem that I faced with other testing frameworks. Namely global/static variables are in a deterministic state because each test is executed as a separate process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文