18

I want to unit test my Arduino code. Ideally, I want to execute and test my code without uploading it to the board. What tools or libraries can help me with this?

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

2
  • 7
    Unit testing and running the code in an emulator are quite different things however. Commented Feb 13, 2014 at 9:46
  • 1
    Yes, there are two questions here, one is about unit testing arduino code, and the other about emulators for the arduino to execute the code. Maybe create a specific question for each, especially the unit testing one that has not been addressed before.
    – drodri
    Commented Apr 1, 2014 at 22:12

3 Answers 3

10

You can easily simulate your Arduino code using the famous Proteus ISIS.
You just will have to search for the specific Arduino simulation library for Proteus.

Here you a small tutorial of how to make this thing done:

First, identify the path the Arduino IDE generates the hex file to?
for example if you are using the official IDE you will find the hex file location in the "arduino IDE path/lib/preferences.txt"
Or if you are using Arduino ERW which I recommended the most on windows! So you can simply open the output folder by "Sketch > Copy HEX file as path".

Second, download the Arduino library from Here. Then copy ARDUINO.LIB and ARDUINO.IDX to folder "library" in the install directory Proteus.

Third, open ISIS and you should be able to find the component.

Please find this image of the worksheet after finishing the above steps, and simulation works just fine! Simulating Arduino Uno on Proteus ISIS

And as a head start for you, please find this small memo I prepared for the Arduino Uno Pins Layout:

0-14: I/O
A0-A5: Analog

We can use analog as digital:
=============================
Pinout:
00: Rx      | Can't be used as I/O if I wrote in the code Serial.begin
01: TX      |
02: Interrupt
03: Interrupt & PWM
04: 
05: PWM
06: PWM
07: 
08:
09: PWM
10: PWM & SPI
11: PWM & SPI
12: SPI
13: SPI
A0: 
A1:
A2:
A3:
A4: TWI
A5: TWI
10

I always try to isolate the hardware logic from the rest of the domain logic.

Classes (or whatever) from domain logic can be programmed in c++ (with the known arduino restrictions like exceptions or std lib) and can be tested with gtest. When these classes are tested, simply instance from arduino program excluding the main with tests.

If your project is pure hardware this solution will not help you very much, but if you have your own algorithms, protocols...etc, test them with Google test will save you hours of on board testing and memory problems with unexpected results and your software will gain in quality.

1
  • Yes, this is an important point in SW dev that is almost always skipped in arduino development. Testing small units of your code can save hours of time if your project has complex functions or algorithms.
    – drodri
    Commented Apr 1, 2014 at 22:13
4

Have you looked at the AVR simulator that's part of Atmel Studio? Considering Atmel designed the processor, the simulation may very well be running the HDL that was used to produce the MCU itself.

I suspect if you're doing something involved enough to be using unit-testing, you should probably stop thinking of your device as an "Arduino", but rather "An ATmega MCU on a PCB". There are much more information and tools available about ATmega CPUs in general then Arduinos.

Not the answer you're looking for? Browse other questions tagged or ask your own question.