如何测试C代码[重复]

时间:2022-09-06 18:50:28

This question already has an answer here:

这个问题已经有了答案:

How do you perfom Unit-Test like tests in C? Which framework or do you do other Tests as Unit-Tests on code level?

如何在C中完成单元测试之类的测试?在代码级别上,您是做单元测试的框架还是做其他测试?

6 个解决方案

#1


11  

Check is a good test framework for C.

Check是C的一个很好的测试框架。

#2


17  

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.

测试C代码的真正困难是打破对外部模块的依赖关系,这样您就可以将代码隔离在单元中。当您试图在遗留代码周围进行测试时,这可能会特别成问题。在这种情况下,我经常使用链接器在测试中使用存根函数。

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.

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

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

我的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));
}

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.

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

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.

至于交叉编译代码,要使其工作,您需要目标上的良好工具。我已经在PowerPC架构上编译到Linux的googletest cross上完成了这一任务。这是有意义的,因为您有一个完整的shell和os来收集结果。对于不太丰富的环境(我将其分类为没有完整的OS),您应该在主机上构建和运行。无论如何,您都应该这样做,以便您可以作为构建的一部分自动运行测试。

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.

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

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.

Michael Feathers有一本关于测试遗留代码的优秀书籍。在一章中,他介绍了处理非oo代码的技术,我极力推荐这些技术。

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

编辑:我写了一篇关于单元测试过程代码的博客文章,可以在GitHub上找到源代码。

#3


2  

XCode on Mac has something they called CPTest kind of built-in. I made a portable version that I posted here

Mac上的XCode有一种叫做CPTest的内置功能。我在这里发布了一个便携版本

http://www.loufranco.com/blog/files/AppleCppUnitPort.html

http://www.loufranco.com/blog/files/AppleCppUnitPort.html

I was targeting gcc on Windows, but I imagine it would work elsewhere, and it's a very simple framework if you want to see how to implement such a thing.

我的目标是Windows上的gcc,但我认为它在其他地方也可以工作,如果你想了解如何实现这样一个东西,它是一个非常简单的框架。

#4


0  

You can find information on a number of C unit test frameworks at www.xprogramming.com, in particular at the software link.

您可以在www.xprogramming.com找到关于许多C单元测试框架的信息,特别是在software link上。

#5


0  

You may want to have a look at cfix -- it is a unit testing framework specialized for Win32 platforms and NT kernel mode. Supports both C and C++.

您可能想了解一下cfix——它是一个专门用于Win32平台和NT内核模式的单元测试框架。支持C和c++。

#6


0  

If you're using Windows as your dev environment, CUnitWin32 might serve your purpose

如果你使用Windows作为你的开发环境,CUnitWin32可能会达到你的目的。

#1


11  

Check is a good test framework for C.

Check是C的一个很好的测试框架。

#2


17  

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.

测试C代码的真正困难是打破对外部模块的依赖关系,这样您就可以将代码隔离在单元中。当您试图在遗留代码周围进行测试时,这可能会特别成问题。在这种情况下,我经常使用链接器在测试中使用存根函数。

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.

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

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

我的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));
}

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.

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

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.

至于交叉编译代码,要使其工作,您需要目标上的良好工具。我已经在PowerPC架构上编译到Linux的googletest cross上完成了这一任务。这是有意义的,因为您有一个完整的shell和os来收集结果。对于不太丰富的环境(我将其分类为没有完整的OS),您应该在主机上构建和运行。无论如何,您都应该这样做,以便您可以作为构建的一部分自动运行测试。

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.

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

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.

Michael Feathers有一本关于测试遗留代码的优秀书籍。在一章中,他介绍了处理非oo代码的技术,我极力推荐这些技术。

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

编辑:我写了一篇关于单元测试过程代码的博客文章,可以在GitHub上找到源代码。

#3


2  

XCode on Mac has something they called CPTest kind of built-in. I made a portable version that I posted here

Mac上的XCode有一种叫做CPTest的内置功能。我在这里发布了一个便携版本

http://www.loufranco.com/blog/files/AppleCppUnitPort.html

http://www.loufranco.com/blog/files/AppleCppUnitPort.html

I was targeting gcc on Windows, but I imagine it would work elsewhere, and it's a very simple framework if you want to see how to implement such a thing.

我的目标是Windows上的gcc,但我认为它在其他地方也可以工作,如果你想了解如何实现这样一个东西,它是一个非常简单的框架。

#4


0  

You can find information on a number of C unit test frameworks at www.xprogramming.com, in particular at the software link.

您可以在www.xprogramming.com找到关于许多C单元测试框架的信息,特别是在software link上。

#5


0  

You may want to have a look at cfix -- it is a unit testing framework specialized for Win32 platforms and NT kernel mode. Supports both C and C++.

您可能想了解一下cfix——它是一个专门用于Win32平台和NT内核模式的单元测试框架。支持C和c++。

#6


0  

If you're using Windows as your dev environment, CUnitWin32 might serve your purpose

如果你使用Windows作为你的开发环境,CUnitWin32可能会达到你的目的。