views:

4545

answers:

7

I'd like to be able to unit test my Arduino code. Ideally I would be able to run any tests without having to upload the code to the Arduino. Are there any tools or libraries out there which can help me with this?

Update: There is an Arduino emulator in development which could be useful but it doesn't yet seem to be ready for use.

Update: AVR Studio from Atmel contains a chip simulator which could be useful but I can't see how I would use it in conjunction with the Arduino IDE.

+3  A: 

I am not aware of any platform which can test Arduino code.

However, there is the Fritzing platform, which you can use to model the hardware and later on export PCB diagrams and stuff.

Worth checking.

Yuval A
+8  A: 

I have considerable success unit testing my Pic code by abstracting out the hardware access and mocking it in my tests.

For example I abstract PORTA with

#define SetPortA(v) {PORTA = v;}

then SetPortA can easily be mocked, without adding overhead code in the Pic version.

Once the hardware abstraction has been tested a while I soon find that generally code goes from the test rig to the Pic and works first time.

Update:

I use a #include seam for the unit code, #including the unit code in a c++ file for the test rig, and a c file for the target code.

As an example I want to multiplex four 7 segment displays, one port driving the segments and a second selecting the display. The display code interfaces with the displays via SetSegmentData(char) and SetDisplay(char). I can mock these in my c++ test rig and check that I get the data I expect. For the target I use #define so that I get a direct assignment without the overhead of a function call

#define SetSegmentData(x) {PORTA = x;}
David Sykes
I can see in principle how I can use the preprocessor 'seam' for unit testing. However I'm not sure how I can do this without having an emulator on which to run the tests or an avr-gcc compatible compiler which outputs (in my case) Windows binaries...
Matthew Murdoch
Thanks for the update. Do you execute the unit tests on the PIC or on your PC?
Matthew Murdoch
The unit tests are run on a Mac using Xcode. To run them on the Pic probably would need an emulator of some kind. Abstracting it so it runs on the Mac makes switching processors a great deal easieer
David Sykes
The Arduino environment uses the avr-gcc compiler which has some idiosyncrasies which mean that compiling with gcc (or other C++ compiler) and running on a PC may not mean that the code will also compile on avr-gcc.
Matthew Murdoch
What kind of difference are you talking about? Are they things that can't be handled with some preprocessor directives?
Joe
+6  A: 

In the absence of any pre-existing unit test frameworks for Arduino, I have created ArduinoUnit. Here's a simple Arduino sketch demonstrating its use:

#include <ArduinoUnit.h>

// Create test suite
TestSuite suite;

void setup() {
}

// Create a test called 'addition' in the test suite
test(addition) {
    assertEquals(3, 1 + 2);
}

void loop() {
    // Run test suite, printing results to the serial port
    suite.run();
}
Matthew Murdoch
+1  A: 

Here's two resources for unit testing embedded code (although neither can be used directly with Arduino sketches).

Matthew Murdoch
+4  A: 

It seems that emulino would do the job perfectly.

Gonzo
+1 - Nice link, thanks.
Matthew Murdoch
+3  A: 

simavr is an AVR simulator using avr-gcc.

It already supports a few ATTiny and ATMega µC, and - according to the author - it's easy to add some more.

In the examples lies simduino, an arduino emulator. It supports running the arduino bootloader and can be programmed with avrdude through socat (a modified netcat).

Gonzo
+1 - Another great link - thank you.
Matthew Murdoch
+1  A: 

We are using Arduino boards for data acquisition in a large scientific experiment. Subsequently, we have to support several Arduino boards with different implementations. I wrote python utilities to dynamically load Arduino hex images during unit testing. The code found on the link below supports Windows and OSX via configuration file. To find out where your hex images are placed by the Arduino IDE, hit the shift key before you hit the build (play) button. Hit the shift while hitting upload to find out where your avrdude (command line upload utility) is located on your system / version of Arduino. Alternatively, you can look at the included config files and use your install location (currently on Arduino 0020).

http://github.com/toddstavish/Python-Arduino-Unit-Testing

toddstavish
+1 Great stuff! Do you have any information on how you did your unit testing once the images were uploaded?
Matthew Murdoch
We used nosetests to run our unit tests on the python side. The setup for each tests loads the correct hex image for that test. We start small and then work into more comprehensive testing. Make sure serial communication is working, make sure serial integration to the UI is working, check serial to DB integration, etc. The analog_read_speed pde and py show the basics of this (see github link above). Eventually, we will open source the entire project, so please stay tuned. :)
toddstavish