Unit Testing Frameworks for C: Comparison

You can use any C or C++ unit testing framework. Its easy enough to call C functions from C++.

My opinion is that you want to have as little output as possible from your tests. ie if everything is OK, it should print '100% passed'. Otherwise it should only print out details of test failures.

see xprogramming.com (original link broken; here it is on the way back machine), scroll down to the Unit Testing table and look for the C Language or C++ frameworks. The most 'standard' it seems is cppUnit.


I've used gtest and found it to be pretty easy to use (It is C++ though). Really though, it doesn't matter too much which one you pick. Just pick one and learn it.


Unity, a test framework for C has a rich set of assertions including bitwise and memory block comparisons. If you're not dealing with a large legacy codebase I'd recommend using Ceedling, the parent project of Unity. Ceedling builds your Unity tests, production code and integrates a nice mocking framework called CMock. CMock can auto-generate a mock/substitute for a module from it's public interface header.

There's a video showing you how to get started with Ceedling and Unity. (DISCLAIMER: I created the video).

An alternative, is CppUTest. It compiles to 1 static library (2 if you want to use it's mocking framework, CppUMock). Tests are written in C++ and it integrates with your existing makefile project. It also offers memory leak detection alternatives for malloc/free, new/delete. By comparison, it's assertions are not as comprehensive as Unity, but it's probably better suited to testing C/C++ legacy projects.

Tags:

C

Tdd