C语言 Google Test 可以测试 C 代码吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5335268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:05:13  来源:igfitidea点击:

Is Google Test OK for testing C code?

cgoogletest

提问by Jason

So I've come to like and enjoy using Google Test for a C++ project I'm involved in. I'm just bringing up a new project that will be straight C (a library) and so far can't see any reason why not to continuing using Google Test, even though its a C++ framework. Having a C++ compiler available will not be an issue.

所以我开始喜欢并喜欢在我参与的 C++ 项目中使用 Google 测试。不要继续使用 Google Test,即使它是一个 C++ 框架。拥有可用的 C++ 编译器将不是问题。

Are there are specific reasons why I shouldn't use Google Test for testing straight C code?

为什么我不应该使用 Google Test 来测试直接的 C 代码,是否有特定的原因?

Thanks.

谢谢。

回答by mikelong

It is pretty common to test C code using a C++ testing frameworks, even the leading book on the subjectfollows this approach. I have used googletest extensively in the past to unit test C code and can recommend it.

使用 C++ 测试框架测试 C 代码是很常见的,即使是关于该主题的主要书籍也遵循这种方法。我过去曾广泛使用 googletest 对 C 代码进行单元测试,可以推荐它。

I have written a blog post about it that might be useful: http://meekrosoft.wordpress.com/2009/11/09/unit-testing-c-code-with-the-googletest-framework/

我写了一篇关于它可能有用的博客文章:http: //meekrosoft.wordpress.com/2009/11/09/unit-testing-c-code-with-the-googletest-framework/

回答by Alex B

As all Google's C++ code, Google Test does not use exceptions, so exception safety flow won't be an issue. As long as your headers are C++-compatible (not using C++ keywords, export symbols with correct linkage), it should be fine.

与所有 Google 的 C++ 代码一样,Google Test 不使用异常,因此异常安全流不会成为问题。只要您的头文件与 C++ 兼容(不使用 C++ 关键字,导出具有正确链接的符号),应该没问题。

回答by Zdeno Pavlik

Jason, be aware of that!!! :D

杰森,注意这一点!!!:D

As Meekrosoft said, yes, it is possible. I also used his website when I tried to do that. It works, but there is one big problem:

正如 Meekrosoft 所说,是的,这是可能的。当我尝试这样做时,我也使用了他的网站。它有效,但有一个大问题:

GTest is objected oriented tool and C language isn't!

GTest 是面向对象的工具而 C 语言不是!

In example, in GTest you have a lot of functions (80% of whole API) that request object as parameter, for example:

例如,在 GTest 中有很多函数(整个 API 的 80%)请求对象作为参数,例如:

EXPECT_CALL(turtle, PenDown())              // turtle is object(class) and PenDown() is method of that object
      .Times(AtLeast(1));

from GTest website https://code.google.com/p/googlemock/wiki/ForDummiesso you will use only macros like expect_equal, expect_bigger_than and so on...

来自 GTest 网站https://code.google.com/p/googlemock/wiki/ForDummies所以你将只使用像expect_equal、expect_bigger_than等宏......

I would like to suggest you tool CMocka(or some other C unit testing tools). It is also from google (modified by group of non-google developers) and it is created directly for C language. I use this one when I want to test C-type source code.

我想建议您使用CMocka(或其他一些 C 单元测试工具)。它也是来自 google(由非 google 开发人员小组修改),它是直接为 C 语言创建的。当我想测试 C 类型的源代码时,我会使用这个。

I hope this helps.. :-) Have a nice day.. :-)

我希望这会有所帮助.. :-) 祝您有美好的一天.. :-)

回答by Kim Gr?sman

I just thought I'd add another point: since gtest is C++, you'll be parsing your C headers under test as C++. This means the tests don't guarantee that the headers are consumable from C. I recently ran into this with a C library I'm building.

我只是想补充一点:由于 gtest 是 C++,因此您将在测试中将 C 头文件解析为 C++。这意味着测试不能保证头文件可以从 C 中使用。我最近在构建一个 C 库时遇到了这个问题。

回答by Friedrich

I could not name one. I guess there will be some things which you don't have in "normal" C. E.g I think the TestCase are derived from a certain class. But within the test you can test whatever you like and so why not C?

我无法说出一个名字。我想会有一些你在“正常”CEg 中没有的东西我认为 TestCase 是从某个类派生的。但是在测试中你可以测试任何你喜欢的东西,那么为什么不测试 C?