I'm not an expert on test-driven development. I know the general ideas behind TDD, however I never used it in my projects.
I want to start using TDD, at least creating some unit testing on some more critical units. However I have many difficulties.
For a module that calculates a checksum, it is very simple to create a unit test. However an embedded firmware is more complex.
Consider a simple example. When the user press button 1, a buzzer must be activated for 1 second.
----- Start of buzzer.c ----- #include "timer.h" #include "bsp.h"
statis Timer tmr_buzzer;
void buzzer_on(uint16 time_ms) { TimerSet(&tmr_buzzer, time_ms); bsp_buzzer_on(); }
void buzzer_off(void) { bsp_buzzer_off(); }
void buzzer_task(void) { if (TimerExpired(&tmr_buzzer)) { buzzer_off(); } }
----- End of buzzer.c -----
Of course I have a bsp.c/bsp.h and a timer.c/timer.h.
Now I want to create a unit test for buzzer module, to run on PC.
----- Start of test_buzzer.c ----- #include #include "timer.h" /* driver for PC testing */ #include "bsp.h" /* bsp_buzzer_on/off declarations */
static bool buzzer_on;
int main(int argc, char *argv[]) { /* Test if buzzer_on() works */ buzzer_on(10000); if (!buzzer_on) ... test failed ... /* Wait for 10010 ms */ if (buzzer_on) ... test failed ...
... }
void bsp_buzzer_on(void) { buzzer_on = true; } void bsp_buzzer_off(void) { buzzer_on = false; }
----- End of test_buzzer.c -----
How do you simulate passing of the time (wait for 1010ms)? Of course, I don't want to wait 10 seconds for the test. I need to speed up the time.
Is this the correct way to create a unit testing for a buzzer library?