98

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.

Jason
  • 2,233
  • 3
  • 24
  • 27

5 Answers5

92

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/

mikelong
  • 3,694
  • 2
  • 35
  • 40
17

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.

Alex B
  • 82,554
  • 44
  • 203
  • 280
  • Add to that list of incompatible things: any use of in your C headers. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60932 – Jonah Sep 22 '20 at 12:00
  • 1
    is now compatible with Google Test as of 2021-08-11 as mentioned by Jakub here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60932 – ptk Nov 06 '21 at 00:15
9

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.

Kim Gräsman
  • 7,438
  • 1
  • 28
  • 41
8

Jason, be aware of that!!! :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:

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

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

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

from GTest website gmock_for_dummies.md so you will use only macros like expect_equal, expect_bigger_than and so on...

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.

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

DerKasper
  • 167
  • 2
  • 11
Zdeno Pavlik
  • 728
  • 6
  • 15
  • 11
    EXPECT_CALL is not defined in GoogleTest, but in GoogleMock (a mocking framework--not a unit testing framework). GoogleTest defines macros like EXPECT_EQ and EXPECT_TRUE, which are perfectly suited to unit testing procedural (non-OO) code. I'm currently using it for a project, and it works fine. – weberc2 Mar 14 '15 at 18:08
2

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?

Friedrich
  • 5,916
  • 25
  • 45