Unit Testing

Overview

If the particular embedded code in question doesn’t rely on any hardware peripherals (like a message decoder or mathematical function), you don’t usually need the embedded system to perform the tests on. This is great, since you can run them on a normal computer, and save you all the time and hassle of setting correct test conditions on a microcontroller.

Linux is a great tool to use here, as it is great for compiling and testing C/C++ code, and usually supports the same compiler as which the embedded system uses (e.g. GCC), meaning you have to make little or no changes. See Linux Bash Commands For C for instructions on how to compile C code in Linux, and the Bash page for more helpful bash (Linux terminal) commands.

It is common practise to put unit tests in a separate folder to your source code. I (and others, including those who write libraries in python) use the name test .

UnitTest++

UnitTest++ is a small, lightweight, but capable unit test library for C++ code.

Example output from UnitTest++ when one of the tests fails.

example-output-from-unittestpp-when-test-fail Example output from UnitTest++ when one of the tests fails.

Running Just One Test Suite

The unit tests can be grouped into “Suites” which are designed to be for grouping together similar tests. You can also run just one test suite, useful for preventing yourself getting swamped with errors when debugging failed unit tests. The documentation on many sites on how to run just one suite is wrong as of June 2013 (maybe they recently updated the code). Here is a way I found of doing it:

Replace "Test Suite Name" with the name of the test suite you want to run.

External Links

A really good read is How To Write Good Unit Tests, on the DeveloperForce website.