On-Chip Unit Testing the Arduino with AdruinoUnit

On the climb to becoming a competent software engineer, unit testing becomes second nature. It’s like brushing one’s teeth — not generally a joyous task done with boundless enthusiasm, but it’s an important discipline that prevents a lot of later pain. Hardware does throw a wrench into the system since one can’t auto-build the physical world. I’ve worked mostly on projects that involve hardware and am used to using simulators, but this is hack space.

The Adruino platform is a simple sandbox designed for rapid prototyping, and is very nice for tinkering. Its ability to throw together an idea quickly is it strength. However, as the software becomes more involved, the platform’s weakness comes to the forefront.

I like to keep things as light as is practical, and the situation didn’t call for time spent in crafting a big, beautiful auto-build system. I wanted to do some simple unit testing. The best fit for the situation that I could find was to use ArduinoUnit and run the tests on-chip. ArduinoUnit is basically a set of C macros that allow you to exercise bits of code.

Its advantage is that it’s simple. Download the code, move the ArduinoUnit.h header to the Arduino local library space, and start writing tests. Its primary disadvantage is huge — it’s on-chip, which means not only one can’t auto-build, it’s a completely manual process. One has to push the code to the microcontroller and view the results on the serial output. (One can build custom outputs, but that didn’t fit this siutation.)

Additionally, the number of options are also limited. For value checking there are only the assertTrue() and assertEquals() macros. But it’s enough.

Because it’s a set of macros, you can get bitten. For example, you cannot write

[cpp]if(condition) assertEquals(target, value);[/cpp]

but must put the macro in braces, like

[cpp]if(condition) { assertEquals(target, value); }[/cpp]

Also, you cannot pass any method that modifies data into the macro, as it can get called twice. As a contrived example loosely based on my situation,

[cpp]int count;
int counting_function() {

count = 0;
assertEquals(1, counting_function());[/cpp]

will fail, complaining that 1 was expected but the value was 2. This is because counting_function() gets called twice, not once.

All together, ArduinoUnit’s restrictions are a pain, but it’s a drop-dead easy tool if it happens to fit the problem space. Get in, do it, get out. It fits into the hackish nature of the Adruino platform very well. I’ll keep it the toolbox.

Update History

16 Feb 2013
Updated link to ArduinoUnit.
This entry was posted in Micro & Hardware and tagged , , , . Bookmark the permalink.

2 Responses to On-Chip Unit Testing the Arduino with AdruinoUnit

  1. Great write-up!

    You might be interested to know that ArduinoUnit has moved to github (https://github.com/mmurdoch/arduinounit). If you are interested in helping improve it (removing some of its limitations, perhaps :)), any issues you create and/or pull requests will be greatly received.



  2. Matthew, I really appreciate the notification. I’ll update my post to reflect this.

Leave a Reply