5

Is there a unit testing framework for "plain C", which can easily be integrated with the Visual Studio Test Explorer?

Since I would like to introduce unit testing to our firmware/embedded team in a similar way we are doing for .NET apps, my primary concern is user friendliness, i.e. it has to be simple to write a test and get the green light inside the test explorer.

I am aware of the Microsoft Unit Testing Framework for C++, but since the team has no experience with C++, I am unsure if this will go smoothly. According to the author of Criterion in this HN thread:

Because C hasn't been a subset of C++ since C99, there are plenty of case where trying to do C in C++ is awkward or just not possible with standard C++:

  • First, you'd have to wrap your header inclusions in extern "C" to disable mangling, then you would have to make sure you static_cast all your pointers where normally void* conversions would have done its job.

  • Furthermore, all interfaces relying on designated initializers and compound literals are broken unless you decide to compile in nonstandard GNU C++.

  • And there are more incompatibilities, such as using static or const in array parameter declarations or using VLA in macros which are not recognized by C++.

Second concern is integration with our CI server (Bamboo), but I don't expect any problems there once it's running.

1

Google test grew out of the Cmockery C unit test project, which Google open sourced and abandoned, which now lives on as Cmocka.

I can highly recommend it from personal experience. However, I have only used it from Eclipse, not MSVC, so can’t guarantee anything.

AFAIK, MSVC will let you choose your toolchain, so you would only be using it for the GUI; thus there should be no problem if you use a C99 compiler.

I don’t know if it will fit into the Test Explorer, but this will probably not be a question of C/C++, but more likely of whether the test explorer can parse the output of a given unit test log.

cmocka supports several different message output formats like Test Anything Protocol, Subunit, xUnit XML or the original cmockery output format

So, you stand a good chance. In particular, I woudl expect tge tets explorer to support xUnit format.

I would suggest spending a few hours on a simple “hello, world” test.

  • 1
    The way Test Explorer works is that it passes several different types of arguments to the built executable (i.e. "give me a list of tests", "execute specific test", "execute all tests"), so I am presuming it shouldn't be a problem to write an adapter. But are there any advantages in using Cmocka vs GoogleTest/GoogleMock? Especially since GoogleTest already has an extension? – Groo Feb 24 '17 at 10:45
  • Well, you specified "plain C", so I went for a plain C unit test system. I am not sure that you could use Google Test (which I consider to be superior), since it will require C++ objects as mocks; you would have to try it out & see – Mawg Feb 24 '17 at 11:19
  • 1
    well, you're right, to use Google Test, we need to put all externs and C inludes inside extern "C" blocks to get C linkage, but apart from that we haven't had any major issues. Nevertheless your answered the question so I'll accept it. – Groo Feb 24 '17 at 13:03
  • A heartfelt thanks to you, Had I known that, or even taken time to try it out, I would not have rushed into cmocka. Did you just pretend, then, that your C functions & structs to be mocked were C++ Classes? – Mawg Feb 24 '17 at 15:12
  • 1
    Since MSVC doesn't support --wrap and weak binding like GCC (which I believe are needed by cmocka), we used HippoMocks for mocking and GoogleTest for testing. HippoMocks rewrites the function jump table in order to mock functions, it's a single header file you need to include, and it seems to be portable accross different platforms. Criterion seems to work the same way). So we simply use gtest for tests and then mock when needed using HippoMocks, and get full VS Test Explorer support, can debug individual tests and stuff like that. – Groo Feb 24 '17 at 16:38
1

We started doing some tests with Google Test (which includes Google Mock) to get things going and see which problems will arise in practice. This is still a C++ testing framework, so the concerns from my question still apply, but it was easy to integrate so we just had to give it a try:

  • It's very easy to write a test (not much plumbing compared to some other C++ frameworks)
  • Google test library needs to be built once and then included in the project, but the procedure is fairly simple
  • Extension for VS2015 allows VS integration (viewing tests in Test Explorer, debugging individual tests, etc.)
  • Creates jUnit XML reports allowing simple integration with Bamboo.

(Update 2017)

Just to update: in the end we continued using Google Test since Visual Studio extension works fine and it was faster to just use extern "C" linkage instead of going for some other C testing framework which would require us to write an extension for VS ourselves.

However, we decided to use hippomocks for mocking since it's a single header mocking framework which mocks C functions and works with the MSVC compiler. People using GCC can use CMocka, but it doesn't work with MSVC (but then again "GCC people" probably woulnd't use Visual Studio in the first place).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.