Teaching TDD from the ground up

One of the first steps in any embedded software project is to implement an assert macro.  This is true for a few reasons:

  • you have certain assumptions about how the hardware should behave and you want to know when those assumptions are broken,
  • you need a way to signal outside of the custom hardware when something has gone wrong,
  • there is no standard function that can deal with these problems.

This might seem like a wasteful thing for every project to undertake, but actually it is a very constructive place to start.  Firstly, it frames the development around verification.  Secondly, when you find a bug you don’t start printf debugging, instead focusing on adding assertions.  And finally, from the very outset of the project you demystify the black box of your embedded system.

In the same way, I think that the first step in learning test driven development should be to write a testing framework.  And it needn’t be difficult – a simple test framework can be written in a few lines of code.  This is the test framework for testing the fake function framework.

/* Test Framework :-) */
void setup();
#define TEST_F(SUITE, NAME) void NAME()
#define RUN_TEST(SUITE, TESTNAME) printf(" Running %s.%s: \n", #SUITE, #TESTNAME); setup(); TESTNAME(); printf(" SUCCESS\n");
#define ASSERT_EQ(A, B) assert((A) == (B))
#define ASSERT_TRUE(A) assert((A))


It could be even more concise, but I wanted to make it compatible with the googletest framework so I can re-use the test cases in both C and C++ without modification.  Anyway, you get the point.

I am a big fan of testing frameworks, and for sure there is no need to re-write junit every time we start a new java program.  But for learning TDD, I think a lot of the mystery would disappear when participants make a simple test framework and use that to make their first few tests.



Filed under fff, software, testing

3 responses to “Teaching TDD from the ground up

  1. Mike,

    I have been tasked with finding a unit test framework for our company. I really like googletest and have tried fff and think it will work great for us. The problem I have run into is that in our environment we use visual studio. I can get my tests working using fff if I compile everything using g++ running mingw on my machine, but in visual studio it won’t compile. My code starts like this:
    extern “C”
    #include “Geometry.h”

    #include “gtest/gtest.h”
    #include “fff/fff.h”


    FAKE_VALUE_FUNC(Point2, rVector2PointAdd, RVector2, Point2);

    And when I compile I get the following:
    4 IntelliSense: function “rVector2PointAdd” is not a type name c:\ASI\UnitTest\Tests\GeometryTests.cpp 11 1 GeometryTests
    Error 3 error C4430: missing type specifier – int assumed. Note: C++ does not support default-int c:\asi\unittest\tests\geometrytests.cpp 11 1 GeometryTests
    Error 2 error C2061: syntax error : identifier ‘rVector2PointAdd’ c:\asi\unittest\tests\geometrytests.cpp 11 1 GeometryTests

    Have you encountered this, or have any ideas of what I can change to get my tests compiling in visual studio?

    • I figured out my problem. For some reason I left out the 2 at the end of FAKE_VALUE_FUNC which compiled no problem with g++ as soon as I realized this and made the fix this worked like a champ! Thanks for your work and I enjoy reading your blog!

      • meekrosoft

        Ah, it looks like like the parameter counting macros don’t work in Visual Studio – interesting! Glad you found a workaround.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s