Unit Testing Embedded C: Testing Firmware Off the Target

Firmware is usually tested by flashing a board and watching what happens. That is necessary, but it is slow, hard to automate and poor at reaching error paths. Much of a typical product, from protocol parsing to control logic, can be tested without hardware. Compiled for a PC and run against test doubles, it can be checked on every commit. This article covers how to do that and where its limits are.

At a glance

Unity
A small open-source unit test framework for C, widely used in embedded projects.
Seconds
Host-built tests run in seconds, so they can run on every build.
gcov
The GCC coverage tool, which shows which lines and branches the tests reached.
stdint.h
Fixed-width integer types keep behaviour the same on the PC and the target.

The problem

On-target testing ties every check to a board, a programmer and a person. Error paths, such as a corrupted message or a sensor that never answers, are hard to provoke on demand, so they are tested once or not at all.

The obstacle to host testing is usually the code itself. When logic calls registers or vendor functions directly, it will not compile for a PC, and testing looks harder than it is.

Test the logic on the PC and the hardware on the board. Trying to do both in one place makes both harder.

Making code testable

Put hardware access behind a small interface, then provide a test double in the host build: a fake that behaves like the device, or a mock that checks the calls made to it. Link-time substitution, where the test build links a different implementation of the same functions, needs no changes to production code. Unity with CMock, CppUTest and GoogleTest are common choices.

Watch for differences between the PC and the target. The size of int differs on 8-bit and 16-bit parts, structure padding and byte order can differ, and the PC will not reproduce timing. Fixed-width types and explicit byte handling remove most of this risk.

What good looks like

Pick a module with real logic and few hardware calls, such as a message parser, and get it building and tested on the PC first.

Introduce interfaces and test doubles for the hardware it touches, and add tests for the error paths that are hard to reach on a board.

Run the tests in continuous integration on every change, and track coverage to find logic that has none.

Testing on the target and on the host
On targetOn host
Time per runMinutes, including flashingSeconds
Real timing and peripheralsYesNo
Runs in CI without hardwareNeeds a test rigYes
Provoking error pathsDifficultStraightforward
Finding driver and hardware faultsYesOnly through fakes
The two approaches find different kinds of fault and work best together.
Kinds of test double
DoubleWhat it doesTypical use
StubReturns fixed valuesSupplying a sensor reading
FakeWorking, simplified implementationIn-memory flash or a simulated clock
MockChecks the calls it receives against expectationsVerifying the bytes a driver sends on a bus
SpyRecords calls for later inspectionCounting how often a function was called
Common terms. Frameworks and teams sometimes use them loosely.

What it changes

Fast Feedback

Regressions are reported minutes after the change that caused them.

Error Paths Covered

Timeouts, bad data and full buffers are tested deliberately, not by chance.

Safer Refactoring

A test suite makes it possible to restructure code with confidence.

Less Rig Time

Hardware testing focuses on what only hardware can show.

Where to go next

Keep hardware-in-the-loop tests for drivers, timing and integration. Host tests complement them rather than replacing them.

Add a test for each bug fixed in the field, so the same fault cannot return unnoticed.

Problems this prevents

  • Error paths that were never exercised
  • Regressions found weeks after the change
  • Logic that only compiles for the target
  • Type size surprises between PC and target

Discuss your project

Email support@aavico.com or use Get in Touch when the website finishes loading.