Unit Testing Libraries for C

../../../_images/unittest.png

The last time I did some unit testing on C, I used Novaprova. However, at least at the moment, this doesn’t run on non-Intel platforms (like the ARM based Pi).

I decided to make a quick survey of what tools there are. I focused mainly on the ones that are already available through Linux package managers.

Everyone loves Autotools

The leading approach seems to be to use autotools’ built-in test runner i.e. “make check”.

To choose two of my favourite projects at random, VLC and JSON-C, both follow this approach.

Using GCC’s built-in preprocessor and linker gets you some way to mocking and so on.

A lot of applications roll their own test runners using bash or Python scripts.

If you are using a big framework, you may have already installed testing utilities as part of it, for example GNOME’s Glib and Boost (which is C++) both come with their own testing tools.

Without further ado, onward to the tables. Currently, these are just in approximate alphabetical order.

C Only

Name Apt Package Projects Using It
Check check Expat, GStreamer
cmocka cmocka libssh, OpenVPN
CUnit libcunit1 OpenDataPlane
GNU Autounit libautounit-dev  

C and C++

Name Apt Package Projects Using It
API Sanity Checker api-sanity-checker  
CppUnit libcppunit-dev LibreOffice
CppUTest cpputest  
Criterion criterion-dev (ppa)  
CTest (CMake) cmake Netflix, ZeroMQ, libpng, Poppler
Cutter cutter-testing-framework libnfc, Groonga
DejaGnu dejagnu GCC, Other GNU tools
Google Test googletest Chromium, LLVM, OpenCV, Blender

C++ Only

There are loads of C++ testing utilities such as Catch and CxxTest (and too many others to mention) that do not claim explicit support for C.

If you want to use one of those, you can just test your C code using a C++ compiler.

Some purists will object, as in this Stackoverflow comment:

I know C and C++ has a lot of overlap, but it doesn’t strike me as a good idea to use a C++ testing library when you’re producing code that will be ultimately compiled in a C compiler

Tell me your stories

I haven’t had the opportunity yet to go beyond listing them.

What are you using? What have I missed?

I’m especially keen to get more information to fill in the third column in the table above (i.e. what any testing tools vaguely well known open source projects use).