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))
https://github.com/meekrosoft/fff/blob/master/test/c_test_framework.h
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.
Image may be NSFW.
Clik here to view.

Clik here to view.
