如何写纯C单元测试? [英] How to write unit tests in plain C?

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

问题描述

我已经开始深入到 GLib的的文档,发现它也提供了一个单元测试框架。

I've started to dig into the GLib documentation and discovered that it also offers a unit testing framework.

但你怎么能单元测试在程序语言?还是需要用C编程OO?

But how could you do unit tests in a procedural language? Or does it require to program OO in C?

推荐答案

单元测试仅需要切平面,或在边界测试哪些可以做。这是很简单的测试C函数不调用其他函数,或者调用也只测试等功能。这一情况的一些例子是执行计算或逻辑运算的功能,并且在本质上是功能性。功能在相同的输入总是产生相同的输出感。测试这些功能都可以有一个巨大的好处,即使它是什么,通常认为是单元测试的一小部分。

Unit testing only requires "cut-planes" or boundaries at which testing can be done. It is quite straightforward to test C functions which do not call other functions, or which call only other functions that are also tested. Some examples of this are functions which perform calculations or logic operations, and are functional in nature. Functional in the sense that the same input always results in the same output. Testing these functions can have a huge benefit, even though it is a small part of what is normally thought of as unit testing.

更复杂的测试,如使用嘲笑或存根的也是可能的,但是因为它是在更动态语言,或甚至只是面向对象的语言,例如C ++它是几乎没有容易。解决这个的一种方式是使用#定义。这方面的一个例子是这篇文章,单元测试OpenGL应用程序的,它展示了如何模拟出OpenGL调用。这使您可以测试OpenGL调用的是有效的序列进行。

More sophisticated testing, such as the use of mocks or stubs is also possible, but it is not nearly as easy as it is in more dynamic languages, or even just object oriented languages such as C++. One way to approach this is to use #defines. One example of this is this article, Unit testing OpenGL applications, which shows how to mock out OpenGL calls. This allows you to test that valid sequences of OpenGL calls are made.

另一种选择是采取弱符号的优势。例如,所有的MPI API函数是弱符号,因此,如果你在自己的应用程序中定义相同的符号,您的实现覆盖在库中执行不力。如果库中的符号是不弱,你会得到在链接时重复符号错误。然后,您可以实现什么是有效整个MPI C API,它可以让你确保调用正确并匹配了一个模拟有没有可能导致死锁任何额外要求。也可以使用加载库的弱符号的dlopen()则dlsym(),并通过电话如果必要的。 MPI实际上提供了PMPI符号,这是强大的,所以没有必要使用的dlopen()和朋友。

Another option is to take advantage of weak symbols. For example, all MPI API functions are weak symbols, so if you define the same symbol in your own application, your implementation overrides the weak implementation in the library. If the symbols in the library weren't weak, you would get duplicate symbol errors at link time. You can then implement what is effectively a mock of the entire MPI C API, which allows you to ensure that calls are matched up properly and that there aren't any extra calls that could cause deadlocks. It is also possible to load the library's weak symbols using dlopen() and dlsym(), and pass the call on if necessary. MPI actually provides the PMPI symbols, which are strong, so it is not necessary to use dlopen() and friends.

您可以实现多为C.单元测试的好处是稍硬,它可能无法得到保险,你可能期望从Ruby编写或Java的东西的同一水平,但它绝对值得做的事情。

You can realize many of the benefits of unit testing for C. It is slightly harder, and it may not be possible to get the same level of coverage you might expect from something written in Ruby or Java, but it's definitely worth doing.

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

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