search

Faked Function Framework

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.

STRONGMINDS 

STRONGMINDS er et software-udviklingshus i vækst. Vi har solid erfaring med cloududvikling og integration af forretningssystemer og andet udstyr. Vi laver solidt software-håndværk, der giver værdi til din forretning, og du er altid velkommen til at kontakte os, så vi kan drøfte dine muligheder. 

Kontakt Icon Kontakt

STRONGMINDS 

INCUBA - Katrinebjerg

Åbogade 15 

DK - 8200 Aarhus N

 

T: (+45) 7026 6416

E: info@strongminds.dk

ERFA-gruppe for software-udviklingschefer

STRONGMINDS After Work er en ERFA-gruppe for softwareudviklingschefer i Østjylland med en LinkedIn-gruppe og relevante gå-hjem-møder, der kan skabe samarbejde, dialog og relationer. Sammen vil vi løfte og udvikle niveauet og miljøet for softwareudvikling som profession. Er det noget for dig, så kontakt os gerne.