May 2014 15 www.drdobbs.com
Unit Testing in C:
Tools and Conventions
Two lightweight testing frameworks make it easy to unit test C code.
n this article, I look at unit tests using two unit test harnesses
that work in C. Along the way, I will also discuss some of the
common terminology of automated unit testing.
Let me start by discussing the fundamental tool: the test
What Is A Unit Test Harness?
A unit test harness is a software package that allows a programmer
to express how production code should behave. A unit test har-
ness's job is to provide these capabilities:
• A common language to express test cases
• A common language to express expected results
• Access to the features of the production code programming language
• A place to collect all the unit test cases for the project, system,
• A mechanism to run the test cases, either in full or in partial
• A concise report of the test suite success or failure
• A detailed report of any test failures
I'll shortly look at two popular harnesses for testing embedded
C.They are both easy to use and are descendants of the xUnit family
of unit test harnesses.
First, I'll employ Unity, a C-only test harness (http://is.gd/tJc88X).
Later, I will use CppUTest, a unit test harness written in C++, but not
requiring C++ knowledge to use. You'll find that the bulk of the ma-
terial in this article can be applied using any test harness.
By James W. Grenning
Table of Contents
From the Vault