Faked Function Framework

by Morten Hoffmann Sørensen 17. August 2012 14:36

Intro

Perhaps you're doing embedded C programming and have been going through the motions of covering already written code by unit tests (as opposed to doing the development TDD style). Doing this, you've probably sometimes felt the pain of having to stub out an entire module, just to change the behavior of one single method, writing endless lines of repetitive empty method implementations. Or perhaps you find it hard to maintain your test code and share common stub traits?

In that case, perhaps you need to look at Mike Long's faked-function-framework (FFF) as suggested by James Grenning at this year's BITsCon software conference.

Example

Say, you want to stub a method with the signature:

void calculateBigThing(int seed);

Using FFF you can do it like this:

/* Preamble */
#include "fff.h"
DEFINE_FFF_GLOBALS;

FAKE_VOID_FUNC1(calculateBigThing, int);

The statement above simply means fake (or stub, ignoring the cry of puritans) calculateBigThing as a function returning void, taking one int parameter.

Behind the scenes (using macros) FFF implements not only the stub, but also a mock like framework that makes it possible for you to write statements setting up expectations like this:

calculateBigThing_fake.return_val = 33;
ASSERT_EQUAL(expected, calculateBigThing_fake.arg0_history[0]);

ASSERT_EQUAL being your favorite xUnit (C/C++) testing framework assertion macro (CppUnit, CUnit, CppUTest, ...).

You can also stub functions returning a value with up to 10 args and even set up sequences of return values.

References

Check out the work of Mike Long on https://github.com/meekrosoft/fff.

Also check out James Grenning articles on how to apply FFF dealing with low-level OS stuff.

And while you're at it, read Mike Long's excellent article on how to unit test operations on low level memory mapped variables using FFF.

Tags:

Embedded

Om Strongminds

Strongminds arbejder med kundespecifik softwareudvikling til industri, web og mobil og leverer tilhørende serviceydelser, rådgivning og projekter.

Læs mere

Overblik