Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Google Test OK for testing C code?

Tags:

c

googletest

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.

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

Thanks.

like image 811
Jason Avatar asked Mar 17 '11 05:03

Jason


People also ask

What can be used for unit testing in C?

The most scalable way to write unit tests in C is using a unit testing framework, such as: CppUTest. Unity. Google Test.

What is Gtest used for?

Google Test (also known as gtest) is a unit testing library for the C++ programming language, based on the xUnit architecture. The library is released under the BSD 3-clause license.

Is Google an open source Test?

Google Test and xUnit are both open source tools. Google Test with 15.9K GitHub stars and 6.26K forks on GitHub appears to be more popular than xUnit with 2.55K GitHub stars and 607 GitHub forks.

How do I add a Google Test to a C++ project?

Add a Google Test project in Visual Studio 2019In Solution Explorer, right-click on the solution node and choose Add > New Project. Set Language to C++ and type test in the search box. From the results list, choose Google Test Project. Give the test project a name and choose OK.


2 Answers

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

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/

like image 76
mikelong Avatar answered Sep 17 '22 13:09

mikelong


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.

like image 34
Alex B Avatar answered Sep 21 '22 13:09

Alex B