Blog moved to http://www.andrevdm.com/

Friday 25 July 2014

Unit testing embedded C projects with seatest

I recently wrote my first embedded C project and was quite surprised to find that unit tests were not as widely used as I would have expected. There seems to be a general opinion that unit tests are less useful for embedded development that for application development. I find this very strange because debugging embedded systems is hard compared to application development.

Fortunately not everyone agrees with this sentiment and in the end it was relatively easy to get unit tests working thanks to SeaTest (https://code.google.com/p/seatest/). SeaTest is simple and specifically designed for embedded-c projects.

To get it working for a Microchip MPLabX project I did this
  1. Created a _test directory in my main projects directory
  2. Created my unit tests in this directory in test.c
  3. In this directory created a bash script named "test"
  4. Copied seatest.c and seatest.h into the directory
  5. Created dummy PIC include files in this directory
    1. xc.h
    2. xc.c
    3. pic18f4550.h
  6. Created a plib directory under _test containing
    1. timers.h
The idea being that the my test directory (_test) contained files for mocking the Microchip libraries to make testing possible. This turned out to be much simpler than I had feared.


Test runner bash script

#/bin/sh
gcc -std=c99 -o test.o -D TESTING -I . -I .. xc.c test.c ../buttons.c ../lcd.c ../dateTime.c ../timerUi.c ../timer.c seatest.c && ./test.o

Nothing fancy... It does this
  • Defines a TESTING constant
  • Includes the local directory for the "mock" libraries
  • Includes the parent directory for my actual code
  • Runs ./test.o if the compile succeeds

Mock/Stub files

Most of the stub files (timers.h etc) are empty files just to keep the compiler happy. I then just copied whatever definitions I needed from MPLabX's libraries to get the rest working.


Tests

Tests are then simply a matter of calling a function and using the seatest assert functions.

There definitely are things that are hard to test e.g. interrupt routines but as long as your code is modular you should usually be able to test the methods that e.g. the interrupt routine calls.

Overall I found this to be a very simple approach and it certainly helped me get my project up and running a lot faster with a lot more confidence