Unit testing Arduino (C++) code [duplicate] - c++

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. 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.
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.

Don't Run Unit Tests on the Arduino Device or Emulator
The case against microcontroller Device/Emulator/Sim-based tests
There's a lot of discussion about what unit test means and I'm not
really trying to make an argument about that here. This post is not
telling you to avoid all practical testing on your ultimate target
hardware. I am trying to make a point about optimizing your
development feedback cycle by eliminating your target hardware from
your most mundane and frequent tests. The units under test are assumed
to be much smaller than the whole project.
The purpose of unit testing is to test the quality of your own code. Unit tests should generally never test the functionality of factors outside of your control.
Think about it this way: Even if you were to test functionality of the Arduino library, the microcontroller hardware, or an emulator, it is absolutely impossible for such test results to tell you anything about the quality of your own work. Hence, it is far more valuable and efficient to write unit tests that do not run on the target device (or emulator).
Frequent testing on your target hardware has a painfully slow cycle:
Tweak your code
Compile and upload to Arduino device
Observe behavior and guess whether your code is doing what you expect
Repeat
Step 3 is particularly nasty if you expect to get diagnostic messages via serial port but your project itself needs to use your Arduino's only hardware serial port. If you were thinking that the SoftwareSerial library might help, you should know that doing so is likely to disrupt any functionality that requires accurate timing like generating other signals at the same time. This problem has happened to me.
Again, if you were to test your sketch using an emulator and your time-critical routines ran perfectly until you uploaded to the actual Arduino, then the only lesson you're going to learn is that the emulator is flawed--and knowing this still reveals nothing about the quality of your own work.
If it's silly to test on the device or emulator, what should I do?
You're probably using a computer to work on your Arduino project. That computer is orders of magnitudes faster than the microcontroller. Write the tests to build and run on your computer.
Remember, the behavior of the Arduino library and microcontroller should be assumed to be either correct or at least consistently incorrect.
When your tests produce output contrary to your expectations, then you likely have a flaw in your code that was tested. If your test output matches your expectations, but the program does not behave correctly when you upload it to the Arduino, then you know that your tests were based on incorrect assumptions and you likely have a flawed test. In either case, you will have been given real insights on what your next code changes should be. The quality of your feedback is improved from "something is broken" to "this specific code is broken".
How to Build and Run Tests on Your PC
The first thing you need to do is identify your testing goals. Think about what parts of your own code you want to test and then make sure to construct your program in such a way that you can isolate discrete parts for testing.
If the parts that you want to test call any Arduino functions, you will need to provide mock-up replacements in your test program. This is much less work than it seems. Your mock-ups don't have to actually do anything but providing predictable input and output for your tests.
Any of your own code that you intend to test needs to exist in source files other than the .pde sketch. Don't worry, your sketch will still compile even with some source code outside of the sketch. When you really get down to it, little more than your program's normal entry point should be defined in the sketch file.
All that remains is to write the actual tests and then compile it using your favorite C++ compiler! This is probably best illustrated with a real world example.
An actual working example
One of my pet projects found here has some simple tests that run on the PC. For this answer submission, I'll just go over how I mocked-up some of Arduino library functions and the tests I wrote to test those mock-ups. This is not contrary to what I said before about not testing other people's code because I was the one who wrote the mock-ups. I wanted to be very certain that my mock-ups were correct.
Source of mock_arduino.cpp, which contains code that duplicates some support functionality provided by the Arduino library:
#include <sys/timeb.h>
#include "mock_arduino.h"
timeb t_start;
unsigned long millis() {
timeb t_now;
ftime(&t_now);
return (t_now.time - t_start.time) * 1000 + (t_now.millitm - t_start.millitm);
}
void delay( unsigned long ms ) {
unsigned long start = millis();
while(millis() - start < ms){}
}
void initialize_mock_arduino() {
ftime(&t_start);
}
I use the following mock-up to produce readable output when my code writes binary data to the hardware serial device.
fake_serial.h
#include <iostream>
class FakeSerial {
public:
void begin(unsigned long);
void end();
size_t write(const unsigned char*, size_t);
};
extern FakeSerial Serial;
fake_serial.cpp
#include <cstring>
#include <iostream>
#include <iomanip>
#include "fake_serial.h"
void FakeSerial::begin(unsigned long speed) {
return;
}
void FakeSerial::end() {
return;
}
size_t FakeSerial::write( const unsigned char buf[], size_t size ) {
using namespace std;
ios_base::fmtflags oldFlags = cout.flags();
streamsize oldPrec = cout.precision();
char oldFill = cout.fill();
cout << "Serial::write: ";
cout << internal << setfill('0');
for( unsigned int i = 0; i < size; i++ ){
cout << setw(2) << hex << (unsigned int)buf[i] << " ";
}
cout << endl;
cout.flags(oldFlags);
cout.precision(oldPrec);
cout.fill(oldFill);
return size;
}
FakeSerial Serial;
and finally, the actual test program:
#include "mock_arduino.h"
using namespace std;
void millis_test() {
unsigned long start = millis();
cout << "millis() test start: " << start << endl;
while( millis() - start < 10000 ) {
cout << millis() << endl;
sleep(1);
}
unsigned long end = millis();
cout << "End of test - duration: " << end - start << "ms" << endl;
}
void delay_test() {
unsigned long start = millis();
cout << "delay() test start: " << start << endl;
while( millis() - start < 10000 ) {
cout << millis() << endl;
delay(250);
}
unsigned long end = millis();
cout << "End of test - duration: " << end - start << "ms" << endl;
}
void run_tests() {
millis_test();
delay_test();
}
int main(int argc, char **argv){
initialize_mock_arduino();
run_tests();
}
This post is long enough, so please refer to my project on GitHub to see some more test cases in action. I keep my works-in-progress in branches other than master, so check those branches for extra tests, too.
I chose to write my own lightweight test routines, but more robust unit-test frameworks like CppUnit are also available.

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() {
Serial.begin(9600);
}
// 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();
}

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;}

It seems that emulino would do the job perfectly.
Emulino is an emulator for the Arduino platform by Greg Hewgill. (Source)
GitHub repository

simavr is an AVR simulator using avr-gcc.
It already supports a few ATTiny and ATMega microcontrollers, 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).

You can unit test in Python with my project, PySimAVR. Arscons is used for building and simavr for simulation.
Example:
from pysimavr.sim import ArduinoSim
def test_atmega88():
mcu = 'atmega88'
snippet = 'Serial.print("hello");'
output = ArduinoSim(snippet=snippet, mcu=mcu, timespan=0.01).get_serial()
assert output == 'hello'
Start test:
$ nosetests pysimavr/examples/test_example.py
pysimavr.examples.test_example.test_atmega88 ... ok

I built arduino_ci for this purpose. Although it's limited to testing Arduino libraries (and not standalone sketches), it enables unit tests to be run either locally or on a CI system (like Travis CI or Appveyor).
Consider a very simple library in your Arduino Library directory, called DoSomething, with do-something.cpp:
#include <Arduino.h>
#include "do-something.h"
int doSomething(void) {
return 4;
};
You'd unit test it as follows (with a test file called test/is_four.cpp or some such):
#include <ArduinoUnitTests.h>
#include "../do-something.h"
unittest(library_does_something)
{
assertEqual(4, doSomething());
}
unittest_main() // this is a macro for main(). just go with it.
That's all. If that assertEqual syntax and test structure looks familiar, it's because I adopted some of Matthew Murdoch's ArduinoUnit library
that he referred to in his answer.
See Reference.md for more information about unit testing I/O pins, the clock, Serial ports, etc.
These unit tests are compiled and run using a script contained in a ruby gem. For examples of how to set that up, see the README.md or just copy from one of these examples:
A practical example, testing a Queue implementation
Another set of tests on another Queue project
A complex example, simulating a library that controls an interactive device over a SoftwareSerial connection as part of the Adafruit FONA library
The DoSomething example library shown above, used to test arduino_ci itself

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.

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 Mac OS X via a 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 key 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 configuration files and use your install location (currently on Arduino 0020).
http://github.com/toddstavish/Python-Arduino-Unit-Testing

This program allows automated running of several Arduino unit tests. The testing process is started on the PC but the tests run on the actual Arduino hardware. One set of unit tests is typically used to test one Arduino library.
(this
Arduino Forum: http://arduino.cc/forum/index.php?topic=140027.0
GitHub project page: http://jeroendoggen.github.com/Arduino-TestSuite
Page in the Python Package Index: http://pypi.python.org/pypi/arduino_testsuite
The unit tests are written with the "Arduino Unit Testing Library": http://code.google.com/p/arduinounit
The following steps are performed for each set of unit tests:
Read the config file to find out which tests to run
The script compiles and uploads an Arduino sketch that contains the unit testing code.
The unit tests are run on the Arduino board.
The results of the test are printed over the serial port and analyzed by the Python script.
The script starts the next test, repeating the above steps for all test that are requested in the configuration file.
The script prints a summary showing an overview of all the failed/passed tests in the complete testsuite.

Keep hardware-specific code separate or abstracted away from the rest so you can test and debug that bigger "rest" on any platform for which you have good tools and with which you're familiar most.
Basically, try to build as much of the final code from as many known-to-work building blocks as possible. The remaining hardware-specific work will then be much easier and faster. You may finish it by using existing emulators and/or emulating devices on your own. And then, of course, you'll need to test the real thing somehow. Depending on circumstances, that may or may not be very well automatable (i.e. who or what will press buttons and provide other inputs? who or what will observe and interpret various indicators and outputs?).

James W. Grenning writes great books and this one is about unit testing embedded C code Test Driven Development for Embedded C.

I am using Searduino when writing Arduino code. Searduino is an Arduino simulator and a development environment (Makefiles, C code ...) that makes it easy to hack in C/C++ using your favorite editor. You can import Arduino sketches and run them in the simulator.
Screenshot of Searduino 0.8: http://searduino.files.wordpress.com/2014/01/jearduino-0-8.png
Searduino 0.9 will be released and a video will be recorded as soon as the lasts tests are done .... in a day or two.
Testing on the simulator is not to be considered as real tests, but it certainly have helped me a lot in finding stupid/logical mistakes (forgetting to do pinMode(xx, OUTPUT), etc.).
BTW: I am one of the people developing Searduino.

There is a project called ncore, which provides native core for Arduino. And allows you to write tests for Arduino code.
From the project description
The native core allows you to compile and run Arduino sketches on the
PC, generally with no modification. It provides native versions of
standard Arduino functions, and a command-line interepreter to give
inputs to your sketch that would normally come from the hardware
itself.
Also on the "what do I need to use it" section
If you want to build the tests, you'll need cxxtest from
http://cxxtest.tigris.org. NCORE has been tested with cxxtest 3.10.1.

If you want to unit-test code outside MCU (on desktop), check out libcheck:
https://libcheck.github.io/check/
I used it to test my own embedded code few times. It's pretty robust framework.

You can use emulare — you can drag and drop a microcontroller on a diagram and run your code in Eclipse. The documentation on the website tells you how to set it up.

Use Proteus VSM with an Arduino library to debug your code or to test it.
It is a best practice before getting your code onboard, but be sure with timings because the simulation does not run realtime as they run on the board.

Try Autodesk circuit simulator. It allows to test Arduino code and circuits with many other hardware components.

In basic Arduino is written with C and C++, even libraries of arduino are written in C and C++. So,in simple terms just handle the code as C and C++ and try doing the unit testing. Here, by the word "handle" I mean you to change all the basic syntax like serial.println to sysout, pinmode to varaibles, void loop to while() loop which breaks either in keystock or after some iteration.
I know this is little a long process and not so straight forward.On my personal experience, once you get to do with it, this turns to be more reliable.
-Nandha_Frost

In case you are interested in running an INO sketch and checkout the serial output, I have a working implementation of that in my Arduino NMEA checksum project.
The following script takes the file and uses Arduino CLI to compile it to a HEX file which is then loaded to SimAVR which evaluates it and prints the serial output. Since all Arduino programs run forever without really having an option of killing themselves (exit(0) doesn't work), I let the sketch run for a few seconds and then diff the captured output with expected output.
Download and extract Arduino CLI (in this case version 0.5.0 - latest at the time of writing):
curl -L https://github.com/arduino/arduino-cli/releases/download/0.5.0/arduino-cli_0.5.0_Linux_64bit.tar.gz -o arduino-cli.tar.gz
tar -xvzf arduino-cli.tar.gz
Now you can update the index and install the appropriate core:
./arduino-cli core update-index
./arduino-cli core install arduino:avr
Assuming your sketch is named nmea-checksum.ino, to get ELF and HEX, run:
./arduino-cli compile -b arduino:avr:uno nmea-checksum.ino
Next up, SimAVR to run the HEX (or ELF) - I build from source because the latest release didn't work for me:
sudo apt-get update
sudo apt-get install -y build-essential libelf-dev avr-libc gcc-avr freeglut3-dev libncurses5-dev pkg-config
git clone https://github.com/buserror/simavr.git
cd simavr
make
Successful compilation will give you simavr/run_avr which you can use to run the sketch. Like I said, timeout it otherwise it will never terminate:
cd simavr
timeout 10 ./run_avr -m atmega168 -f 16000000 ../../nmea-checksum.ino.arduino.avr.uno.elf &> nmea-checksum.ino.clog || true
The generated file will have ANSI color code control characters wrapping the serial output, to get rid of those:
cat nmea-checksum.ino.clog | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" > nmea-checksum.ino.log
cat nmea-checksum.ino.log
Now all you need to do is compared this file to a known good file:
diff nmea-checksum.ino.log ../../nmea-checksum.ino.test
If there are no differences, diff will exit with code 0, otherwise the script will fail.

Related

GPIO Pins RaspberryPi Using C++

I'm writing a program to control the GPIO pins on my raspberryPi with C++ and having difficulty I'm able to export with the following code:
char pathString[256];
sprintf(pathString, "%s/export", "/sys/class/gpio");
ofstream exporterFile(pathString);
exporterFile << pinNumber;
exporterFile.close()
This works an successfully exports the pin but this does not set the direction:
sprintf(pathString, "%sgpio%d/direction", "/sys/class/gpio", pinNumber);
ofstream directionFile(pathString);
directionFile << pinDirection;
directionFile.close();
For some reason I cannot write to the file, perhaps I do not have the right privileges. My question is, is that the problem and if so how do I solve it so I can write to the file.
Thanks in advance
You need to be root or run your program with sudo in order to use the GPIO pins.
However, I'd recommend using the wiringpi library http://wiringpi.com/ to access GPIO from c/c++. It is easy to use and raises the abstraction level a bit. It also lets you do things like PWM. A program using wiringpi also needs to be run with sudo.

Is it possible to mark tests as taking long time in googletest runs

Most of my tests finish quickly, the time taken is unnoticeable. But a few of them take a few seconds. I would like to print a hint to the user:
TEST(something, thing) {
std::cout << "This might take a few seconds\n";
ASSERT_EQ(expected_result, long_computation());
}
This doesn't blend in well with what is printed. Is there a feature for this in googletest? I couldn't find anything related. Any way to make goggliest understand it, print a hint to the user, and even report error in case the test runs too long? Or any plugin that does this? Thx
TEST(something, thing, max_time: 3 seconds) {
ASSERT_EQ(expected_result, long_computation());
}
I'm not sure anything like this exists but I believe you can tailor some of the features you want at least partially.
You can measure the time of execution of your test yourself and add statements like ASSERT_GT(time_limit, measured_time).
Consider using https://github.com/google/googletest/blob/master/googletest/docs/advanced.md#logging-additional-information for writing the timings or "long"/"fast" attributes.
Consider using https://github.com/google/googletest/blob/master/googletest/docs/advanced.md#running-a-subset-of-the-tests for running only fast, or only long tests, f.e. run only fast tests (provided you named all long tests like *Long): ./foo_test --gtest_filter=-*Long

Arduino at 11.0592MHz. Modify Timer0, delayMicroseconds()

I am using the ATMega328P at 11.0592MHz with the Arduino environment. I recognized that the delayMicroseconds() function is about 27% too fast. The reason is, that the code in wiring.c assumes that the clock peed is now 8MHz.
Now I try to fix it. I found different posts but I am not sure what is the most Arduino compatible way. What can you recommend?
Multiply the variable "us" in wiring.c with 1.27 if the F_CPU is 11.0592MHz? Easy but only affects delayMicroseconds and not millis(), micros(), delay(), etc.
Change the prescale factor maybe from 64 to ???
Other ideas or guides?
Thank you in advance.
Felix
The Arduino way would be to make a custom PLATFORM for your board with that of the new F_CPU speed. The core libraries should carry this F_CPU through.
This is easy enough, by making a boards.txt file with your differences. Where the location of the file (same between the two) is different between the two current IDE's. In the case of Arduino IDE 1.0.5:
C:\Users\mflaga\Documents\Arduino\hardware\myArduino11MgHz\boards.txt
and in the case of 1.5.5:
C:\Users\mflaga\Documents\Arduino\hardware\myArduino11MgHz\avr\boards.txt
Where in your case the sketch directory would be different.
# See: http://code.google.com/p/arduino/wiki/Platforms
##############################################################
myArduino11MgHz.name=Arduino 11MgHz
myArduino11MgHz.vid.0=0x2341
myArduino11MgHz.pid.0=0x0043
myArduino11MgHz.vid.1=0x2341
myArduino11MgHz.pid.1=0x0001
myArduino11MgHz.upload.tool=avrdude
myArduino11MgHz.upload.protocol=arduino
myArduino11MgHz.upload.maximum_size=32256
myArduino11MgHz.upload.maximum_data_size=2048
myArduino11MgHz.upload.speed=115200
myArduino11MgHz.bootloader.tool=avrdude
myArduino11MgHz.bootloader.low_fuses=0xFF
myArduino11MgHz.bootloader.high_fuses=0xDE
myArduino11MgHz.bootloader.extended_fuses=0x05
myArduino11MgHz.bootloader.unlock_bits=0x3F
myArduino11MgHz.bootloader.lock_bits=0x0F
myArduino11MgHz.bootloader.file=optiboot/optiboot_atmega328.hex
myArduino11MgHz.build.mcu=atmega328p
myArduino11MgHz.build.f_cpu=11059200L
myArduino11MgHz.build.board=AVR_myArduino11MgHz
myArduino11MgHz.build.core=arduino:arduino
myArduino11MgHz.build.variant=arduino:standard
##############################################################
Here is a 3rd party GUI Editor of the board.txt.
Disclaimer. The above does compile and should work fine. Where I have not actually tested and loaded into a unit.
That said, I would expect the bootloader's F_CPU not to match. There are three possible solutions to this.
First; the optiboot loader should have a corresponding target with the below deviation:
\arduino-1.5.5\hardware\arduino\avr\bootloaders\optiboot\Makefile
myArduino11MgHz: AVR_FREQ = 11059200L
Second; Change the boards.txt upload speed to match the change of =115200*(11059200/16000000).
myArduino11MgHz.upload.speed=79626
This being a non typical baud rate, either avrdude or the serial port may support it.
Third; Don't use the bootload, by uploading via the ISP programmer.
I have likewise planned to make a board with a different F_CPU, but only actually made custom boards, with the same speed. I would be curious to know if the above actually works, in your case.

Running c++ in browser

I have written some basic c++ programs in one of my classes for school. I was wondering if it was possible to somehow virtually run the program in a broswer. I would like to post the program to my website. Once its posted, a person could access the program, run the program, and, interact with the program. I'm not trying to write C++ for my website, it would be more for an interactive portfolio.
Is this possible?
Use codepad, a website which lets you compile and share code online.
#include <iostream>
int main(int argc, char** argv) {
std::cout << "Hello, Stack Overflow!" << std::endl;
return 0;
}
There is also Google Native Client SDK that allows C++ code to run in browser. Microsoft Active X is also a viable option. I am only saying it is possible not recommended.
You can only run the program on your server, not on the client's machine.
At least not without downloading and manually executing it. Anything else would be an open door for malware...
I see two options, but both very overkill:
Write (or find) a C++ interpreter in JavaScript
Use a VM running an operating system (e.g. jslinux and demonstrate your programs there.
The sensible option is to just give people a way to view and download the source code, I guess.
Google chrome supports this: http://www.readwriteweb.com/cloud/2011/08/google-officially-announces-cc.php
But it's by no means "mainstream" or standards-based.
Another solution (codepad like) would be to use https://ideone.com/ which seems much nicer to use than codepad, more user-friendly, but does the same:
Allow you to write C++ (60 languages possibles) directly from the browser and compile it and render result in the browser (I tried using printf and it worked fine). Possibility of forking source code.
https://ideone.com/baYzfe
The following two programs are quite useful :
1) Ideone
2) Codepad
You can compile, run, and share code online in any browser.
You can use Emscripten to compile C++ to Javascript. Emscripten can compile LLVM bitcode to Javascript. Some demos of Emscripten can be found here, including a raytracer and a text-to-speech engine that was compiled from C++ to Javascript.
To run x86 binaries in a web browser, you could also use an emulator such as v86. This is one possible way to compile and run C++ programs in a browser.
One of the best sites for running C++ and other multiple languages online is Repl.it
This example: repl.it/#abranhe/stackoverflow
#include <iostream>
int main() {
std::cout << "Hello Stackoverflow\n";
return 0;
}
One of the biggest pros it has is that you can work with multiple files, working with header (header.h) files etc. None of the below websites provide this option:
Codepad.org
JSLinux
IDEone
I really recommend it! You will love it!
Also wanted to add Google Colab here as an option:
Cell 1:
%%writefile hello.cpp
#include <iostream>
int main(int argc, char** argv) {
std::cout << "Hello, Stack Overflow!" << std::endl;
return 0;
}
Cell 2:
%%script bash
g++ hello.cpp -o test
ls -laX
./test

How can I unit test Arduino code?

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. 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.
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.
Don't Run Unit Tests on the Arduino Device or Emulator
The case against microcontroller Device/Emulator/Sim-based tests
There's a lot of discussion about what unit test means and I'm not
really trying to make an argument about that here. This post is not
telling you to avoid all practical testing on your ultimate target
hardware. I am trying to make a point about optimizing your
development feedback cycle by eliminating your target hardware from
your most mundane and frequent tests. The units under test are assumed
to be much smaller than the whole project.
The purpose of unit testing is to test the quality of your own code. Unit tests should generally never test the functionality of factors outside of your control.
Think about it this way: Even if you were to test functionality of the Arduino library, the microcontroller hardware, or an emulator, it is absolutely impossible for such test results to tell you anything about the quality of your own work. Hence, it is far more valuable and efficient to write unit tests that do not run on the target device (or emulator).
Frequent testing on your target hardware has a painfully slow cycle:
Tweak your code
Compile and upload to Arduino device
Observe behavior and guess whether your code is doing what you expect
Repeat
Step 3 is particularly nasty if you expect to get diagnostic messages via serial port but your project itself needs to use your Arduino's only hardware serial port. If you were thinking that the SoftwareSerial library might help, you should know that doing so is likely to disrupt any functionality that requires accurate timing like generating other signals at the same time. This problem has happened to me.
Again, if you were to test your sketch using an emulator and your time-critical routines ran perfectly until you uploaded to the actual Arduino, then the only lesson you're going to learn is that the emulator is flawed--and knowing this still reveals nothing about the quality of your own work.
If it's silly to test on the device or emulator, what should I do?
You're probably using a computer to work on your Arduino project. That computer is orders of magnitudes faster than the microcontroller. Write the tests to build and run on your computer.
Remember, the behavior of the Arduino library and microcontroller should be assumed to be either correct or at least consistently incorrect.
When your tests produce output contrary to your expectations, then you likely have a flaw in your code that was tested. If your test output matches your expectations, but the program does not behave correctly when you upload it to the Arduino, then you know that your tests were based on incorrect assumptions and you likely have a flawed test. In either case, you will have been given real insights on what your next code changes should be. The quality of your feedback is improved from "something is broken" to "this specific code is broken".
How to Build and Run Tests on Your PC
The first thing you need to do is identify your testing goals. Think about what parts of your own code you want to test and then make sure to construct your program in such a way that you can isolate discrete parts for testing.
If the parts that you want to test call any Arduino functions, you will need to provide mock-up replacements in your test program. This is much less work than it seems. Your mock-ups don't have to actually do anything but providing predictable input and output for your tests.
Any of your own code that you intend to test needs to exist in source files other than the .pde sketch. Don't worry, your sketch will still compile even with some source code outside of the sketch. When you really get down to it, little more than your program's normal entry point should be defined in the sketch file.
All that remains is to write the actual tests and then compile it using your favorite C++ compiler! This is probably best illustrated with a real world example.
An actual working example
One of my pet projects found here has some simple tests that run on the PC. For this answer submission, I'll just go over how I mocked-up some of Arduino library functions and the tests I wrote to test those mock-ups. This is not contrary to what I said before about not testing other people's code because I was the one who wrote the mock-ups. I wanted to be very certain that my mock-ups were correct.
Source of mock_arduino.cpp, which contains code that duplicates some support functionality provided by the Arduino library:
#include <sys/timeb.h>
#include "mock_arduino.h"
timeb t_start;
unsigned long millis() {
timeb t_now;
ftime(&t_now);
return (t_now.time - t_start.time) * 1000 + (t_now.millitm - t_start.millitm);
}
void delay( unsigned long ms ) {
unsigned long start = millis();
while(millis() - start < ms){}
}
void initialize_mock_arduino() {
ftime(&t_start);
}
I use the following mock-up to produce readable output when my code writes binary data to the hardware serial device.
fake_serial.h
#include <iostream>
class FakeSerial {
public:
void begin(unsigned long);
void end();
size_t write(const unsigned char*, size_t);
};
extern FakeSerial Serial;
fake_serial.cpp
#include <cstring>
#include <iostream>
#include <iomanip>
#include "fake_serial.h"
void FakeSerial::begin(unsigned long speed) {
return;
}
void FakeSerial::end() {
return;
}
size_t FakeSerial::write( const unsigned char buf[], size_t size ) {
using namespace std;
ios_base::fmtflags oldFlags = cout.flags();
streamsize oldPrec = cout.precision();
char oldFill = cout.fill();
cout << "Serial::write: ";
cout << internal << setfill('0');
for( unsigned int i = 0; i < size; i++ ){
cout << setw(2) << hex << (unsigned int)buf[i] << " ";
}
cout << endl;
cout.flags(oldFlags);
cout.precision(oldPrec);
cout.fill(oldFill);
return size;
}
FakeSerial Serial;
and finally, the actual test program:
#include "mock_arduino.h"
using namespace std;
void millis_test() {
unsigned long start = millis();
cout << "millis() test start: " << start << endl;
while( millis() - start < 10000 ) {
cout << millis() << endl;
sleep(1);
}
unsigned long end = millis();
cout << "End of test - duration: " << end - start << "ms" << endl;
}
void delay_test() {
unsigned long start = millis();
cout << "delay() test start: " << start << endl;
while( millis() - start < 10000 ) {
cout << millis() << endl;
delay(250);
}
unsigned long end = millis();
cout << "End of test - duration: " << end - start << "ms" << endl;
}
void run_tests() {
millis_test();
delay_test();
}
int main(int argc, char **argv){
initialize_mock_arduino();
run_tests();
}
This post is long enough, so please refer to my project on GitHub to see some more test cases in action. I keep my works-in-progress in branches other than master, so check those branches for extra tests, too.
I chose to write my own lightweight test routines, but more robust unit-test frameworks like CppUnit are also available.
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() {
Serial.begin(9600);
}
// 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();
}
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;}
It seems that emulino would do the job perfectly.
Emulino is an emulator for the Arduino platform by Greg Hewgill. (Source)
GitHub repository
simavr is an AVR simulator using avr-gcc.
It already supports a few ATTiny and ATMega microcontrollers, 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).
You can unit test in Python with my project, PySimAVR. Arscons is used for building and simavr for simulation.
Example:
from pysimavr.sim import ArduinoSim
def test_atmega88():
mcu = 'atmega88'
snippet = 'Serial.print("hello");'
output = ArduinoSim(snippet=snippet, mcu=mcu, timespan=0.01).get_serial()
assert output == 'hello'
Start test:
$ nosetests pysimavr/examples/test_example.py
pysimavr.examples.test_example.test_atmega88 ... ok
I built arduino_ci for this purpose. Although it's limited to testing Arduino libraries (and not standalone sketches), it enables unit tests to be run either locally or on a CI system (like Travis CI or Appveyor).
Consider a very simple library in your Arduino Library directory, called DoSomething, with do-something.cpp:
#include <Arduino.h>
#include "do-something.h"
int doSomething(void) {
return 4;
};
You'd unit test it as follows (with a test file called test/is_four.cpp or some such):
#include <ArduinoUnitTests.h>
#include "../do-something.h"
unittest(library_does_something)
{
assertEqual(4, doSomething());
}
unittest_main() // this is a macro for main(). just go with it.
That's all. If that assertEqual syntax and test structure looks familiar, it's because I adopted some of Matthew Murdoch's ArduinoUnit library
that he referred to in his answer.
See Reference.md for more information about unit testing I/O pins, the clock, Serial ports, etc.
These unit tests are compiled and run using a script contained in a ruby gem. For examples of how to set that up, see the README.md or just copy from one of these examples:
A practical example, testing a Queue implementation
Another set of tests on another Queue project
A complex example, simulating a library that controls an interactive device over a SoftwareSerial connection as part of the Adafruit FONA library
The DoSomething example library shown above, used to test arduino_ci itself
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.
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 Mac OS X via a 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 key 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 configuration files and use your install location (currently on Arduino 0020).
http://github.com/toddstavish/Python-Arduino-Unit-Testing
This program allows automated running of several Arduino unit tests. The testing process is started on the PC but the tests run on the actual Arduino hardware. One set of unit tests is typically used to test one Arduino library.
(this
Arduino Forum: http://arduino.cc/forum/index.php?topic=140027.0
GitHub project page: http://jeroendoggen.github.com/Arduino-TestSuite
Page in the Python Package Index: http://pypi.python.org/pypi/arduino_testsuite
The unit tests are written with the "Arduino Unit Testing Library": http://code.google.com/p/arduinounit
The following steps are performed for each set of unit tests:
Read the config file to find out which tests to run
The script compiles and uploads an Arduino sketch that contains the unit testing code.
The unit tests are run on the Arduino board.
The results of the test are printed over the serial port and analyzed by the Python script.
The script starts the next test, repeating the above steps for all test that are requested in the configuration file.
The script prints a summary showing an overview of all the failed/passed tests in the complete testsuite.
Keep hardware-specific code separate or abstracted away from the rest so you can test and debug that bigger "rest" on any platform for which you have good tools and with which you're familiar most.
Basically, try to build as much of the final code from as many known-to-work building blocks as possible. The remaining hardware-specific work will then be much easier and faster. You may finish it by using existing emulators and/or emulating devices on your own. And then, of course, you'll need to test the real thing somehow. Depending on circumstances, that may or may not be very well automatable (i.e. who or what will press buttons and provide other inputs? who or what will observe and interpret various indicators and outputs?).
James W. Grenning writes great books and this one is about unit testing embedded C code Test Driven Development for Embedded C.
I am using Searduino when writing Arduino code. Searduino is an Arduino simulator and a development environment (Makefiles, C code ...) that makes it easy to hack in C/C++ using your favorite editor. You can import Arduino sketches and run them in the simulator.
Screenshot of Searduino 0.8: http://searduino.files.wordpress.com/2014/01/jearduino-0-8.png
Searduino 0.9 will be released and a video will be recorded as soon as the lasts tests are done .... in a day or two.
Testing on the simulator is not to be considered as real tests, but it certainly have helped me a lot in finding stupid/logical mistakes (forgetting to do pinMode(xx, OUTPUT), etc.).
BTW: I am one of the people developing Searduino.
There is a project called ncore, which provides native core for Arduino. And allows you to write tests for Arduino code.
From the project description
The native core allows you to compile and run Arduino sketches on the
PC, generally with no modification. It provides native versions of
standard Arduino functions, and a command-line interepreter to give
inputs to your sketch that would normally come from the hardware
itself.
Also on the "what do I need to use it" section
If you want to build the tests, you'll need cxxtest from
http://cxxtest.tigris.org. NCORE has been tested with cxxtest 3.10.1.
If you want to unit-test code outside MCU (on desktop), check out libcheck:
https://libcheck.github.io/check/
I used it to test my own embedded code few times. It's pretty robust framework.
You can use emulare — you can drag and drop a microcontroller on a diagram and run your code in Eclipse. The documentation on the website tells you how to set it up.
Use Proteus VSM with an Arduino library to debug your code or to test it.
It is a best practice before getting your code onboard, but be sure with timings because the simulation does not run realtime as they run on the board.
Try Autodesk circuit simulator. It allows to test Arduino code and circuits with many other hardware components.
In basic Arduino is written with C and C++, even libraries of arduino are written in C and C++. So,in simple terms just handle the code as C and C++ and try doing the unit testing. Here, by the word "handle" I mean you to change all the basic syntax like serial.println to sysout, pinmode to varaibles, void loop to while() loop which breaks either in keystock or after some iteration.
I know this is little a long process and not so straight forward.On my personal experience, once you get to do with it, this turns to be more reliable.
-Nandha_Frost
In case you are interested in running an INO sketch and checkout the serial output, I have a working implementation of that in my Arduino NMEA checksum project.
The following script takes the file and uses Arduino CLI to compile it to a HEX file which is then loaded to SimAVR which evaluates it and prints the serial output. Since all Arduino programs run forever without really having an option of killing themselves (exit(0) doesn't work), I let the sketch run for a few seconds and then diff the captured output with expected output.
Download and extract Arduino CLI (in this case version 0.5.0 - latest at the time of writing):
curl -L https://github.com/arduino/arduino-cli/releases/download/0.5.0/arduino-cli_0.5.0_Linux_64bit.tar.gz -o arduino-cli.tar.gz
tar -xvzf arduino-cli.tar.gz
Now you can update the index and install the appropriate core:
./arduino-cli core update-index
./arduino-cli core install arduino:avr
Assuming your sketch is named nmea-checksum.ino, to get ELF and HEX, run:
./arduino-cli compile -b arduino:avr:uno nmea-checksum.ino
Next up, SimAVR to run the HEX (or ELF) - I build from source because the latest release didn't work for me:
sudo apt-get update
sudo apt-get install -y build-essential libelf-dev avr-libc gcc-avr freeglut3-dev libncurses5-dev pkg-config
git clone https://github.com/buserror/simavr.git
cd simavr
make
Successful compilation will give you simavr/run_avr which you can use to run the sketch. Like I said, timeout it otherwise it will never terminate:
cd simavr
timeout 10 ./run_avr -m atmega168 -f 16000000 ../../nmea-checksum.ino.arduino.avr.uno.elf &> nmea-checksum.ino.clog || true
The generated file will have ANSI color code control characters wrapping the serial output, to get rid of those:
cat nmea-checksum.ino.clog | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" > nmea-checksum.ino.log
cat nmea-checksum.ino.log
Now all you need to do is compared this file to a known good file:
diff nmea-checksum.ino.log ../../nmea-checksum.ino.test
If there are no differences, diff will exit with code 0, otherwise the script will fail.