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.
unit-testing embedded arduino avr avr-gcc
add a comment |
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.
unit-testing embedded arduino avr avr-gcc
There is another thread on this question from 2011 at arduino.cc/forum/index.php?action=printpage;topic=54356.0
– Jakob
Jan 8 '12 at 19:58
Thanks @Jakob. An Arduino simulator referenced in that thread (with other potentially useful links at the bottom of the page): arduino.com.au/Simulator-for-Arduino.html
– Matthew Murdoch
Jan 9 '12 at 12:30
4
Unfortunately its only for Windows, I'd like to see a way to simply compile and run Arduino code from command line without any closed source or hardware dependencies.
– Jakob
Jan 12 '12 at 11:55
2
A little update, 5 years later: Simavr is still very much active and has improved a lot since the question has been asked, so I thought it deserves being bumped closer to the top. And it might be the right tool for regression testing, scenario based testing and why not also unit testing. That way the code you test is the same as the one on the target hardware.
– zmo
May 29 '15 at 7:31
For important projects, consider a hardware tester; another MCU than can time and test button/switch reactions, boot time, temp, v/ma usage, weird option permutations, etc. Yes, it's more hardware to build, but it can add a safety layer onto revision making. a lot of pro devices use jtag et al.
– dandavis
Nov 15 '16 at 19:40
add a comment |
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.
unit-testing embedded arduino avr avr-gcc
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.
unit-testing embedded arduino avr avr-gcc
unit-testing embedded arduino avr avr-gcc
edited Sep 23 '12 at 22:47
Peter Mortensen
13.8k1987113
13.8k1987113
asked Apr 23 '09 at 8:43
Matthew MurdochMatthew Murdoch
20.5k2481121
20.5k2481121
There is another thread on this question from 2011 at arduino.cc/forum/index.php?action=printpage;topic=54356.0
– Jakob
Jan 8 '12 at 19:58
Thanks @Jakob. An Arduino simulator referenced in that thread (with other potentially useful links at the bottom of the page): arduino.com.au/Simulator-for-Arduino.html
– Matthew Murdoch
Jan 9 '12 at 12:30
4
Unfortunately its only for Windows, I'd like to see a way to simply compile and run Arduino code from command line without any closed source or hardware dependencies.
– Jakob
Jan 12 '12 at 11:55
2
A little update, 5 years later: Simavr is still very much active and has improved a lot since the question has been asked, so I thought it deserves being bumped closer to the top. And it might be the right tool for regression testing, scenario based testing and why not also unit testing. That way the code you test is the same as the one on the target hardware.
– zmo
May 29 '15 at 7:31
For important projects, consider a hardware tester; another MCU than can time and test button/switch reactions, boot time, temp, v/ma usage, weird option permutations, etc. Yes, it's more hardware to build, but it can add a safety layer onto revision making. a lot of pro devices use jtag et al.
– dandavis
Nov 15 '16 at 19:40
add a comment |
There is another thread on this question from 2011 at arduino.cc/forum/index.php?action=printpage;topic=54356.0
– Jakob
Jan 8 '12 at 19:58
Thanks @Jakob. An Arduino simulator referenced in that thread (with other potentially useful links at the bottom of the page): arduino.com.au/Simulator-for-Arduino.html
– Matthew Murdoch
Jan 9 '12 at 12:30
4
Unfortunately its only for Windows, I'd like to see a way to simply compile and run Arduino code from command line without any closed source or hardware dependencies.
– Jakob
Jan 12 '12 at 11:55
2
A little update, 5 years later: Simavr is still very much active and has improved a lot since the question has been asked, so I thought it deserves being bumped closer to the top. And it might be the right tool for regression testing, scenario based testing and why not also unit testing. That way the code you test is the same as the one on the target hardware.
– zmo
May 29 '15 at 7:31
For important projects, consider a hardware tester; another MCU than can time and test button/switch reactions, boot time, temp, v/ma usage, weird option permutations, etc. Yes, it's more hardware to build, but it can add a safety layer onto revision making. a lot of pro devices use jtag et al.
– dandavis
Nov 15 '16 at 19:40
There is another thread on this question from 2011 at arduino.cc/forum/index.php?action=printpage;topic=54356.0
– Jakob
Jan 8 '12 at 19:58
There is another thread on this question from 2011 at arduino.cc/forum/index.php?action=printpage;topic=54356.0
– Jakob
Jan 8 '12 at 19:58
Thanks @Jakob. An Arduino simulator referenced in that thread (with other potentially useful links at the bottom of the page): arduino.com.au/Simulator-for-Arduino.html
– Matthew Murdoch
Jan 9 '12 at 12:30
Thanks @Jakob. An Arduino simulator referenced in that thread (with other potentially useful links at the bottom of the page): arduino.com.au/Simulator-for-Arduino.html
– Matthew Murdoch
Jan 9 '12 at 12:30
4
4
Unfortunately its only for Windows, I'd like to see a way to simply compile and run Arduino code from command line without any closed source or hardware dependencies.
– Jakob
Jan 12 '12 at 11:55
Unfortunately its only for Windows, I'd like to see a way to simply compile and run Arduino code from command line without any closed source or hardware dependencies.
– Jakob
Jan 12 '12 at 11:55
2
2
A little update, 5 years later: Simavr is still very much active and has improved a lot since the question has been asked, so I thought it deserves being bumped closer to the top. And it might be the right tool for regression testing, scenario based testing and why not also unit testing. That way the code you test is the same as the one on the target hardware.
– zmo
May 29 '15 at 7:31
A little update, 5 years later: Simavr is still very much active and has improved a lot since the question has been asked, so I thought it deserves being bumped closer to the top. And it might be the right tool for regression testing, scenario based testing and why not also unit testing. That way the code you test is the same as the one on the target hardware.
– zmo
May 29 '15 at 7:31
For important projects, consider a hardware tester; another MCU than can time and test button/switch reactions, boot time, temp, v/ma usage, weird option permutations, etc. Yes, it's more hardware to build, but it can add a safety layer onto revision making. a lot of pro devices use jtag et al.
– dandavis
Nov 15 '16 at 19:40
For important projects, consider a hardware tester; another MCU than can time and test button/switch reactions, boot time, temp, v/ma usage, weird option permutations, etc. Yes, it's more hardware to build, but it can add a safety layer onto revision making. a lot of pro devices use jtag et al.
– dandavis
Nov 15 '16 at 19:40
add a comment |
19 Answers
19
active
oldest
votes
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.
1
This is a great answer! Thank you!
– Jonathan Arkell
Jul 4 '14 at 13:33
4
@WarrenMacEvoy Again, I think you've taken my advice and made it into something that it is not. You should certainly test your code in its actual environment AT SOME POINT. My argument is that you should not do that every day and you certainly shouldn't call it a unit test.
– Iron Savior
Jul 27 '15 at 16:10
1
@toasted_flakes I'm not sure where you got that quote, but it's not something I've said. Unit tests running on the device have a lot of problems--very slow feedback loop, you may not have any serial ports or any other means of IO to spare on your target device, and they have very limited capacity which can impact the scope of your test suite.
– Iron Savior
May 25 '16 at 4:51
1
@ChristianHujer You should certainly test on real hardware--nobody is saying you should never test on target hardware. My post is about tightening your your daily development feedback cycle tight by unit testing on your development machine. Your testing overhead is minimized this way because you will only test on your target hardware when it is necessary.
– Iron Savior
Jan 15 '18 at 17:35
1
@Benjohn Arduino sketch source files used to have "pde" extension even though they're C++. arduino.cc/en/Guide/Environment#toc1
– Iron Savior
Jan 18 '18 at 23:50
|
show 13 more comments
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();
}
14
The tests seem to run only on the arduino, so you cannot execute them automatically on your development machine. The basic idea of unit tests is to run them automatically, so the current design seems to be more a debugging tool but no real unit testing framework.
– Jakob
Jan 8 '12 at 19:52
1
You're right. To be able to run these on a PC would, in addition, require either an Arduino or AVR emulator. There is no real hardware abstraction layer in the Arduino libraries (at the moment) and the AVR emulators when I looked were all still in development. If things have moved on now then in principle this could be done.
– Matthew Murdoch
Jan 9 '12 at 12:25
1
@IronSavior The same reason you would run unit tests for a desktop application on a desktop computer. They validate that the code you've written functions correctly in its intended environment.
– Matthew Murdoch
May 31 '13 at 11:10
10
@MatthewMurdoch I'm afraid that you're incorrect. By definition, unit tests are never run in the target environment. In fact, the very idea behind unit testing is to completely eliminate the target environment from testing. They're always run in a lab-like environment that mocks all of the activity external to the unit being tested so as to ensure that the success or failure of the test reflects ONLY on the unit under test. That's one of the biggest reasons people use the concept of Inversion of Control in complex projects.
– Iron Savior
May 31 '13 at 14:38
1
@IronSavior I think this is what I meant. So if you run the unit tests in the development environment rather than the target, what about architecture-specific code? For instance what if a test relies on the size of integer types, which is unspecified in C++ but might be set for the target environment and may vary on the development environment (because you never know who may fork your project)?
– marcv81
Oct 1 '14 at 16:37
|
show 12 more comments
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;}
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
Apr 23 '09 at 21:24
Thanks for the update. Do you execute the unit tests on the PIC or on your PC?
– Matthew Murdoch
Apr 24 '09 at 10:18
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
Apr 24 '09 at 11:26
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
Sep 1 '09 at 10:08
What kind of difference are you talking about? Are they things that can't be handled with some preprocessor directives?
– Joseph Lisee
Mar 15 '10 at 20:35
|
show 2 more comments
It seems that emulino would do the job perfectly.
Emulino is an emulator for the Arduino platform by Greg Hewgill. (Source)
GitHub repository
add a comment |
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).
add a comment |
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
add a comment |
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.
add a comment |
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
+1 Great stuff! Do you have any information on how you did your unit testing once the images were uploaded?
– Matthew Murdoch
Oct 11 '10 at 11:44
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
Oct 11 '10 at 21:28
add a comment |
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.
add a comment |
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?).
add a comment |
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.
add a comment |
James W. Grenning writes great books and this one is about unit testing embedded C code Test Driven Development for Embedded C.
add a comment |
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.
This is an interesting project. Unfortunately, it looks like it's now dead, as it's had no progress for 6 years.
– Cerin
Jul 10 '18 at 20:29
add a comment |
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
This looks interesting, but I'm not sure that it's correctly testing Arduino code. From the output you posted, it's compiling to x86_64 architecture, which obviously isn't used for the Arduino. That could introduce bugs caused by conflicts between type implementations.
– Cerin
Jul 10 '18 at 21:35
That sort of bug is certainly possible. Do you have an example I could use for a test case?
– Ian
Jul 12 '18 at 3:59
add a comment |
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.
The only downside is that this doesn't support g++, which makes it useless for testing most Arduino libraries that use C++ features.
– Cerin
Jul 10 '18 at 21:46
add a comment |
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.
add a comment |
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.
add a comment |
Try Autodesk circuit simulator. It allows to test Arduino code and circuits with many other hardware components.
add a comment |
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
add a comment |
protected by Community♦ Oct 18 '17 at 12:28
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
19 Answers
19
active
oldest
votes
19 Answers
19
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
1
This is a great answer! Thank you!
– Jonathan Arkell
Jul 4 '14 at 13:33
4
@WarrenMacEvoy Again, I think you've taken my advice and made it into something that it is not. You should certainly test your code in its actual environment AT SOME POINT. My argument is that you should not do that every day and you certainly shouldn't call it a unit test.
– Iron Savior
Jul 27 '15 at 16:10
1
@toasted_flakes I'm not sure where you got that quote, but it's not something I've said. Unit tests running on the device have a lot of problems--very slow feedback loop, you may not have any serial ports or any other means of IO to spare on your target device, and they have very limited capacity which can impact the scope of your test suite.
– Iron Savior
May 25 '16 at 4:51
1
@ChristianHujer You should certainly test on real hardware--nobody is saying you should never test on target hardware. My post is about tightening your your daily development feedback cycle tight by unit testing on your development machine. Your testing overhead is minimized this way because you will only test on your target hardware when it is necessary.
– Iron Savior
Jan 15 '18 at 17:35
1
@Benjohn Arduino sketch source files used to have "pde" extension even though they're C++. arduino.cc/en/Guide/Environment#toc1
– Iron Savior
Jan 18 '18 at 23:50
|
show 13 more comments
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.
1
This is a great answer! Thank you!
– Jonathan Arkell
Jul 4 '14 at 13:33
4
@WarrenMacEvoy Again, I think you've taken my advice and made it into something that it is not. You should certainly test your code in its actual environment AT SOME POINT. My argument is that you should not do that every day and you certainly shouldn't call it a unit test.
– Iron Savior
Jul 27 '15 at 16:10
1
@toasted_flakes I'm not sure where you got that quote, but it's not something I've said. Unit tests running on the device have a lot of problems--very slow feedback loop, you may not have any serial ports or any other means of IO to spare on your target device, and they have very limited capacity which can impact the scope of your test suite.
– Iron Savior
May 25 '16 at 4:51
1
@ChristianHujer You should certainly test on real hardware--nobody is saying you should never test on target hardware. My post is about tightening your your daily development feedback cycle tight by unit testing on your development machine. Your testing overhead is minimized this way because you will only test on your target hardware when it is necessary.
– Iron Savior
Jan 15 '18 at 17:35
1
@Benjohn Arduino sketch source files used to have "pde" extension even though they're C++. arduino.cc/en/Guide/Environment#toc1
– Iron Savior
Jan 18 '18 at 23:50
|
show 13 more comments
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.
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.
edited Jan 2 at 19:15
answered Jul 11 '12 at 16:46
Iron SaviorIron Savior
2,99721629
2,99721629
1
This is a great answer! Thank you!
– Jonathan Arkell
Jul 4 '14 at 13:33
4
@WarrenMacEvoy Again, I think you've taken my advice and made it into something that it is not. You should certainly test your code in its actual environment AT SOME POINT. My argument is that you should not do that every day and you certainly shouldn't call it a unit test.
– Iron Savior
Jul 27 '15 at 16:10
1
@toasted_flakes I'm not sure where you got that quote, but it's not something I've said. Unit tests running on the device have a lot of problems--very slow feedback loop, you may not have any serial ports or any other means of IO to spare on your target device, and they have very limited capacity which can impact the scope of your test suite.
– Iron Savior
May 25 '16 at 4:51
1
@ChristianHujer You should certainly test on real hardware--nobody is saying you should never test on target hardware. My post is about tightening your your daily development feedback cycle tight by unit testing on your development machine. Your testing overhead is minimized this way because you will only test on your target hardware when it is necessary.
– Iron Savior
Jan 15 '18 at 17:35
1
@Benjohn Arduino sketch source files used to have "pde" extension even though they're C++. arduino.cc/en/Guide/Environment#toc1
– Iron Savior
Jan 18 '18 at 23:50
|
show 13 more comments
1
This is a great answer! Thank you!
– Jonathan Arkell
Jul 4 '14 at 13:33
4
@WarrenMacEvoy Again, I think you've taken my advice and made it into something that it is not. You should certainly test your code in its actual environment AT SOME POINT. My argument is that you should not do that every day and you certainly shouldn't call it a unit test.
– Iron Savior
Jul 27 '15 at 16:10
1
@toasted_flakes I'm not sure where you got that quote, but it's not something I've said. Unit tests running on the device have a lot of problems--very slow feedback loop, you may not have any serial ports or any other means of IO to spare on your target device, and they have very limited capacity which can impact the scope of your test suite.
– Iron Savior
May 25 '16 at 4:51
1
@ChristianHujer You should certainly test on real hardware--nobody is saying you should never test on target hardware. My post is about tightening your your daily development feedback cycle tight by unit testing on your development machine. Your testing overhead is minimized this way because you will only test on your target hardware when it is necessary.
– Iron Savior
Jan 15 '18 at 17:35
1
@Benjohn Arduino sketch source files used to have "pde" extension even though they're C++. arduino.cc/en/Guide/Environment#toc1
– Iron Savior
Jan 18 '18 at 23:50
1
1
This is a great answer! Thank you!
– Jonathan Arkell
Jul 4 '14 at 13:33
This is a great answer! Thank you!
– Jonathan Arkell
Jul 4 '14 at 13:33
4
4
@WarrenMacEvoy Again, I think you've taken my advice and made it into something that it is not. You should certainly test your code in its actual environment AT SOME POINT. My argument is that you should not do that every day and you certainly shouldn't call it a unit test.
– Iron Savior
Jul 27 '15 at 16:10
@WarrenMacEvoy Again, I think you've taken my advice and made it into something that it is not. You should certainly test your code in its actual environment AT SOME POINT. My argument is that you should not do that every day and you certainly shouldn't call it a unit test.
– Iron Savior
Jul 27 '15 at 16:10
1
1
@toasted_flakes I'm not sure where you got that quote, but it's not something I've said. Unit tests running on the device have a lot of problems--very slow feedback loop, you may not have any serial ports or any other means of IO to spare on your target device, and they have very limited capacity which can impact the scope of your test suite.
– Iron Savior
May 25 '16 at 4:51
@toasted_flakes I'm not sure where you got that quote, but it's not something I've said. Unit tests running on the device have a lot of problems--very slow feedback loop, you may not have any serial ports or any other means of IO to spare on your target device, and they have very limited capacity which can impact the scope of your test suite.
– Iron Savior
May 25 '16 at 4:51
1
1
@ChristianHujer You should certainly test on real hardware--nobody is saying you should never test on target hardware. My post is about tightening your your daily development feedback cycle tight by unit testing on your development machine. Your testing overhead is minimized this way because you will only test on your target hardware when it is necessary.
– Iron Savior
Jan 15 '18 at 17:35
@ChristianHujer You should certainly test on real hardware--nobody is saying you should never test on target hardware. My post is about tightening your your daily development feedback cycle tight by unit testing on your development machine. Your testing overhead is minimized this way because you will only test on your target hardware when it is necessary.
– Iron Savior
Jan 15 '18 at 17:35
1
1
@Benjohn Arduino sketch source files used to have "pde" extension even though they're C++. arduino.cc/en/Guide/Environment#toc1
– Iron Savior
Jan 18 '18 at 23:50
@Benjohn Arduino sketch source files used to have "pde" extension even though they're C++. arduino.cc/en/Guide/Environment#toc1
– Iron Savior
Jan 18 '18 at 23:50
|
show 13 more comments
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();
}
14
The tests seem to run only on the arduino, so you cannot execute them automatically on your development machine. The basic idea of unit tests is to run them automatically, so the current design seems to be more a debugging tool but no real unit testing framework.
– Jakob
Jan 8 '12 at 19:52
1
You're right. To be able to run these on a PC would, in addition, require either an Arduino or AVR emulator. There is no real hardware abstraction layer in the Arduino libraries (at the moment) and the AVR emulators when I looked were all still in development. If things have moved on now then in principle this could be done.
– Matthew Murdoch
Jan 9 '12 at 12:25
1
@IronSavior The same reason you would run unit tests for a desktop application on a desktop computer. They validate that the code you've written functions correctly in its intended environment.
– Matthew Murdoch
May 31 '13 at 11:10
10
@MatthewMurdoch I'm afraid that you're incorrect. By definition, unit tests are never run in the target environment. In fact, the very idea behind unit testing is to completely eliminate the target environment from testing. They're always run in a lab-like environment that mocks all of the activity external to the unit being tested so as to ensure that the success or failure of the test reflects ONLY on the unit under test. That's one of the biggest reasons people use the concept of Inversion of Control in complex projects.
– Iron Savior
May 31 '13 at 14:38
1
@IronSavior I think this is what I meant. So if you run the unit tests in the development environment rather than the target, what about architecture-specific code? For instance what if a test relies on the size of integer types, which is unspecified in C++ but might be set for the target environment and may vary on the development environment (because you never know who may fork your project)?
– marcv81
Oct 1 '14 at 16:37
|
show 12 more comments
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();
}
14
The tests seem to run only on the arduino, so you cannot execute them automatically on your development machine. The basic idea of unit tests is to run them automatically, so the current design seems to be more a debugging tool but no real unit testing framework.
– Jakob
Jan 8 '12 at 19:52
1
You're right. To be able to run these on a PC would, in addition, require either an Arduino or AVR emulator. There is no real hardware abstraction layer in the Arduino libraries (at the moment) and the AVR emulators when I looked were all still in development. If things have moved on now then in principle this could be done.
– Matthew Murdoch
Jan 9 '12 at 12:25
1
@IronSavior The same reason you would run unit tests for a desktop application on a desktop computer. They validate that the code you've written functions correctly in its intended environment.
– Matthew Murdoch
May 31 '13 at 11:10
10
@MatthewMurdoch I'm afraid that you're incorrect. By definition, unit tests are never run in the target environment. In fact, the very idea behind unit testing is to completely eliminate the target environment from testing. They're always run in a lab-like environment that mocks all of the activity external to the unit being tested so as to ensure that the success or failure of the test reflects ONLY on the unit under test. That's one of the biggest reasons people use the concept of Inversion of Control in complex projects.
– Iron Savior
May 31 '13 at 14:38
1
@IronSavior I think this is what I meant. So if you run the unit tests in the development environment rather than the target, what about architecture-specific code? For instance what if a test relies on the size of integer types, which is unspecified in C++ but might be set for the target environment and may vary on the development environment (because you never know who may fork your project)?
– marcv81
Oct 1 '14 at 16:37
|
show 12 more comments
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();
}
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();
}
edited Feb 25 '13 at 10:03
answered Apr 26 '09 at 20:12
Matthew MurdochMatthew Murdoch
20.5k2481121
20.5k2481121
14
The tests seem to run only on the arduino, so you cannot execute them automatically on your development machine. The basic idea of unit tests is to run them automatically, so the current design seems to be more a debugging tool but no real unit testing framework.
– Jakob
Jan 8 '12 at 19:52
1
You're right. To be able to run these on a PC would, in addition, require either an Arduino or AVR emulator. There is no real hardware abstraction layer in the Arduino libraries (at the moment) and the AVR emulators when I looked were all still in development. If things have moved on now then in principle this could be done.
– Matthew Murdoch
Jan 9 '12 at 12:25
1
@IronSavior The same reason you would run unit tests for a desktop application on a desktop computer. They validate that the code you've written functions correctly in its intended environment.
– Matthew Murdoch
May 31 '13 at 11:10
10
@MatthewMurdoch I'm afraid that you're incorrect. By definition, unit tests are never run in the target environment. In fact, the very idea behind unit testing is to completely eliminate the target environment from testing. They're always run in a lab-like environment that mocks all of the activity external to the unit being tested so as to ensure that the success or failure of the test reflects ONLY on the unit under test. That's one of the biggest reasons people use the concept of Inversion of Control in complex projects.
– Iron Savior
May 31 '13 at 14:38
1
@IronSavior I think this is what I meant. So if you run the unit tests in the development environment rather than the target, what about architecture-specific code? For instance what if a test relies on the size of integer types, which is unspecified in C++ but might be set for the target environment and may vary on the development environment (because you never know who may fork your project)?
– marcv81
Oct 1 '14 at 16:37
|
show 12 more comments
14
The tests seem to run only on the arduino, so you cannot execute them automatically on your development machine. The basic idea of unit tests is to run them automatically, so the current design seems to be more a debugging tool but no real unit testing framework.
– Jakob
Jan 8 '12 at 19:52
1
You're right. To be able to run these on a PC would, in addition, require either an Arduino or AVR emulator. There is no real hardware abstraction layer in the Arduino libraries (at the moment) and the AVR emulators when I looked were all still in development. If things have moved on now then in principle this could be done.
– Matthew Murdoch
Jan 9 '12 at 12:25
1
@IronSavior The same reason you would run unit tests for a desktop application on a desktop computer. They validate that the code you've written functions correctly in its intended environment.
– Matthew Murdoch
May 31 '13 at 11:10
10
@MatthewMurdoch I'm afraid that you're incorrect. By definition, unit tests are never run in the target environment. In fact, the very idea behind unit testing is to completely eliminate the target environment from testing. They're always run in a lab-like environment that mocks all of the activity external to the unit being tested so as to ensure that the success or failure of the test reflects ONLY on the unit under test. That's one of the biggest reasons people use the concept of Inversion of Control in complex projects.
– Iron Savior
May 31 '13 at 14:38
1
@IronSavior I think this is what I meant. So if you run the unit tests in the development environment rather than the target, what about architecture-specific code? For instance what if a test relies on the size of integer types, which is unspecified in C++ but might be set for the target environment and may vary on the development environment (because you never know who may fork your project)?
– marcv81
Oct 1 '14 at 16:37
14
14
The tests seem to run only on the arduino, so you cannot execute them automatically on your development machine. The basic idea of unit tests is to run them automatically, so the current design seems to be more a debugging tool but no real unit testing framework.
– Jakob
Jan 8 '12 at 19:52
The tests seem to run only on the arduino, so you cannot execute them automatically on your development machine. The basic idea of unit tests is to run them automatically, so the current design seems to be more a debugging tool but no real unit testing framework.
– Jakob
Jan 8 '12 at 19:52
1
1
You're right. To be able to run these on a PC would, in addition, require either an Arduino or AVR emulator. There is no real hardware abstraction layer in the Arduino libraries (at the moment) and the AVR emulators when I looked were all still in development. If things have moved on now then in principle this could be done.
– Matthew Murdoch
Jan 9 '12 at 12:25
You're right. To be able to run these on a PC would, in addition, require either an Arduino or AVR emulator. There is no real hardware abstraction layer in the Arduino libraries (at the moment) and the AVR emulators when I looked were all still in development. If things have moved on now then in principle this could be done.
– Matthew Murdoch
Jan 9 '12 at 12:25
1
1
@IronSavior The same reason you would run unit tests for a desktop application on a desktop computer. They validate that the code you've written functions correctly in its intended environment.
– Matthew Murdoch
May 31 '13 at 11:10
@IronSavior The same reason you would run unit tests for a desktop application on a desktop computer. They validate that the code you've written functions correctly in its intended environment.
– Matthew Murdoch
May 31 '13 at 11:10
10
10
@MatthewMurdoch I'm afraid that you're incorrect. By definition, unit tests are never run in the target environment. In fact, the very idea behind unit testing is to completely eliminate the target environment from testing. They're always run in a lab-like environment that mocks all of the activity external to the unit being tested so as to ensure that the success or failure of the test reflects ONLY on the unit under test. That's one of the biggest reasons people use the concept of Inversion of Control in complex projects.
– Iron Savior
May 31 '13 at 14:38
@MatthewMurdoch I'm afraid that you're incorrect. By definition, unit tests are never run in the target environment. In fact, the very idea behind unit testing is to completely eliminate the target environment from testing. They're always run in a lab-like environment that mocks all of the activity external to the unit being tested so as to ensure that the success or failure of the test reflects ONLY on the unit under test. That's one of the biggest reasons people use the concept of Inversion of Control in complex projects.
– Iron Savior
May 31 '13 at 14:38
1
1
@IronSavior I think this is what I meant. So if you run the unit tests in the development environment rather than the target, what about architecture-specific code? For instance what if a test relies on the size of integer types, which is unspecified in C++ but might be set for the target environment and may vary on the development environment (because you never know who may fork your project)?
– marcv81
Oct 1 '14 at 16:37
@IronSavior I think this is what I meant. So if you run the unit tests in the development environment rather than the target, what about architecture-specific code? For instance what if a test relies on the size of integer types, which is unspecified in C++ but might be set for the target environment and may vary on the development environment (because you never know who may fork your project)?
– marcv81
Oct 1 '14 at 16:37
|
show 12 more comments
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;}
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
Apr 23 '09 at 21:24
Thanks for the update. Do you execute the unit tests on the PIC or on your PC?
– Matthew Murdoch
Apr 24 '09 at 10:18
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
Apr 24 '09 at 11:26
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
Sep 1 '09 at 10:08
What kind of difference are you talking about? Are they things that can't be handled with some preprocessor directives?
– Joseph Lisee
Mar 15 '10 at 20:35
|
show 2 more comments
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;}
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
Apr 23 '09 at 21:24
Thanks for the update. Do you execute the unit tests on the PIC or on your PC?
– Matthew Murdoch
Apr 24 '09 at 10:18
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
Apr 24 '09 at 11:26
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
Sep 1 '09 at 10:08
What kind of difference are you talking about? Are they things that can't be handled with some preprocessor directives?
– Joseph Lisee
Mar 15 '10 at 20:35
|
show 2 more comments
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;}
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;}
edited Dec 8 '10 at 2:27
Peter Mortensen
13.8k1987113
13.8k1987113
answered Apr 23 '09 at 13:13
David SykesDavid Sykes
20.4k166175
20.4k166175
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
Apr 23 '09 at 21:24
Thanks for the update. Do you execute the unit tests on the PIC or on your PC?
– Matthew Murdoch
Apr 24 '09 at 10:18
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
Apr 24 '09 at 11:26
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
Sep 1 '09 at 10:08
What kind of difference are you talking about? Are they things that can't be handled with some preprocessor directives?
– Joseph Lisee
Mar 15 '10 at 20:35
|
show 2 more comments
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
Apr 23 '09 at 21:24
Thanks for the update. Do you execute the unit tests on the PIC or on your PC?
– Matthew Murdoch
Apr 24 '09 at 10:18
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
Apr 24 '09 at 11:26
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
Sep 1 '09 at 10:08
What kind of difference are you talking about? Are they things that can't be handled with some preprocessor directives?
– Joseph Lisee
Mar 15 '10 at 20:35
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
Apr 23 '09 at 21:24
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
Apr 23 '09 at 21:24
Thanks for the update. Do you execute the unit tests on the PIC or on your PC?
– Matthew Murdoch
Apr 24 '09 at 10:18
Thanks for the update. Do you execute the unit tests on the PIC or on your PC?
– Matthew Murdoch
Apr 24 '09 at 10:18
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
Apr 24 '09 at 11:26
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
Apr 24 '09 at 11:26
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
Sep 1 '09 at 10:08
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
Sep 1 '09 at 10:08
What kind of difference are you talking about? Are they things that can't be handled with some preprocessor directives?
– Joseph Lisee
Mar 15 '10 at 20:35
What kind of difference are you talking about? Are they things that can't be handled with some preprocessor directives?
– Joseph Lisee
Mar 15 '10 at 20:35
|
show 2 more comments
It seems that emulino would do the job perfectly.
Emulino is an emulator for the Arduino platform by Greg Hewgill. (Source)
GitHub repository
add a comment |
It seems that emulino would do the job perfectly.
Emulino is an emulator for the Arduino platform by Greg Hewgill. (Source)
GitHub repository
add a comment |
It seems that emulino would do the job perfectly.
Emulino is an emulator for the Arduino platform by Greg Hewgill. (Source)
GitHub repository
It seems that emulino would do the job perfectly.
Emulino is an emulator for the Arduino platform by Greg Hewgill. (Source)
GitHub repository
edited Aug 7 '17 at 17:18
Peter Mortensen
13.8k1987113
13.8k1987113
answered Nov 20 '09 at 21:08
GonzoGonzo
36933
36933
add a comment |
add a comment |
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).
add a comment |
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).
add a comment |
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).
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).
edited Dec 15 '10 at 18:55
Peter Mortensen
13.8k1987113
13.8k1987113
answered Feb 1 '10 at 17:25
GonzoGonzo
36933
36933
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Oct 25 '11 at 14:19
Peter Mortensen
13.8k1987113
13.8k1987113
answered Oct 24 '11 at 10:44
pontyponty
48665
48665
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Apr 23 '09 at 9:14
Yuval AdamYuval Adam
111k76264362
111k76264362
add a comment |
add a comment |
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
+1 Great stuff! Do you have any information on how you did your unit testing once the images were uploaded?
– Matthew Murdoch
Oct 11 '10 at 11:44
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
Oct 11 '10 at 21:28
add a comment |
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
+1 Great stuff! Do you have any information on how you did your unit testing once the images were uploaded?
– Matthew Murdoch
Oct 11 '10 at 11:44
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
Oct 11 '10 at 21:28
add a comment |
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
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
edited Dec 8 '10 at 2:23
Peter Mortensen
13.8k1987113
13.8k1987113
answered Oct 4 '10 at 0:52
toddstavishtoddstavish
151237
151237
+1 Great stuff! Do you have any information on how you did your unit testing once the images were uploaded?
– Matthew Murdoch
Oct 11 '10 at 11:44
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
Oct 11 '10 at 21:28
add a comment |
+1 Great stuff! Do you have any information on how you did your unit testing once the images were uploaded?
– Matthew Murdoch
Oct 11 '10 at 11:44
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
Oct 11 '10 at 21:28
+1 Great stuff! Do you have any information on how you did your unit testing once the images were uploaded?
– Matthew Murdoch
Oct 11 '10 at 11:44
+1 Great stuff! Do you have any information on how you did your unit testing once the images were uploaded?
– Matthew Murdoch
Oct 11 '10 at 11:44
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
Oct 11 '10 at 21:28
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
Oct 11 '10 at 21:28
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 11 '13 at 12:59
jeroendoggenjeroendoggen
8113
8113
add a comment |
add a comment |
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?).
add a comment |
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?).
add a comment |
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?).
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?).
answered Oct 24 '11 at 12:14
Alexey FrunzeAlexey Frunze
52.2k953129
52.2k953129
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Aug 7 '17 at 17:20
Peter Mortensen
13.8k1987113
13.8k1987113
answered Jan 10 '14 at 23:40
user3183814user3183814
5111
5111
add a comment |
add a comment |
James W. Grenning writes great books and this one is about unit testing embedded C code Test Driven Development for Embedded C.
add a comment |
James W. Grenning writes great books and this one is about unit testing embedded C code Test Driven Development for Embedded C.
add a comment |
James W. Grenning writes great books and this one is about unit testing embedded C code Test Driven Development for Embedded C.
James W. Grenning writes great books and this one is about unit testing embedded C code Test Driven Development for Embedded C.
edited Oct 25 '11 at 14:17
Peter Mortensen
13.8k1987113
13.8k1987113
answered Nov 30 '10 at 14:46
Rafael VegaRafael Vega
3,30332144
3,30332144
add a comment |
add a comment |
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.
This is an interesting project. Unfortunately, it looks like it's now dead, as it's had no progress for 6 years.
– Cerin
Jul 10 '18 at 20:29
add a comment |
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.
This is an interesting project. Unfortunately, it looks like it's now dead, as it's had no progress for 6 years.
– Cerin
Jul 10 '18 at 20:29
add a comment |
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.
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.
edited Aug 11 '12 at 11:37
Matthew Murdoch
20.5k2481121
20.5k2481121
answered Aug 6 '12 at 15:21
SudarSudar
8,2972163109
8,2972163109
This is an interesting project. Unfortunately, it looks like it's now dead, as it's had no progress for 6 years.
– Cerin
Jul 10 '18 at 20:29
add a comment |
This is an interesting project. Unfortunately, it looks like it's now dead, as it's had no progress for 6 years.
– Cerin
Jul 10 '18 at 20:29
This is an interesting project. Unfortunately, it looks like it's now dead, as it's had no progress for 6 years.
– Cerin
Jul 10 '18 at 20:29
This is an interesting project. Unfortunately, it looks like it's now dead, as it's had no progress for 6 years.
– Cerin
Jul 10 '18 at 20:29
add a comment |
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
This looks interesting, but I'm not sure that it's correctly testing Arduino code. From the output you posted, it's compiling to x86_64 architecture, which obviously isn't used for the Arduino. That could introduce bugs caused by conflicts between type implementations.
– Cerin
Jul 10 '18 at 21:35
That sort of bug is certainly possible. Do you have an example I could use for a test case?
– Ian
Jul 12 '18 at 3:59
add a comment |
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
This looks interesting, but I'm not sure that it's correctly testing Arduino code. From the output you posted, it's compiling to x86_64 architecture, which obviously isn't used for the Arduino. That could introduce bugs caused by conflicts between type implementations.
– Cerin
Jul 10 '18 at 21:35
That sort of bug is certainly possible. Do you have an example I could use for a test case?
– Ian
Jul 12 '18 at 3:59
add a comment |
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 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
edited Jan 8 at 19:59
answered Mar 7 '18 at 21:59
IanIan
6,15421936
6,15421936
This looks interesting, but I'm not sure that it's correctly testing Arduino code. From the output you posted, it's compiling to x86_64 architecture, which obviously isn't used for the Arduino. That could introduce bugs caused by conflicts between type implementations.
– Cerin
Jul 10 '18 at 21:35
That sort of bug is certainly possible. Do you have an example I could use for a test case?
– Ian
Jul 12 '18 at 3:59
add a comment |
This looks interesting, but I'm not sure that it's correctly testing Arduino code. From the output you posted, it's compiling to x86_64 architecture, which obviously isn't used for the Arduino. That could introduce bugs caused by conflicts between type implementations.
– Cerin
Jul 10 '18 at 21:35
That sort of bug is certainly possible. Do you have an example I could use for a test case?
– Ian
Jul 12 '18 at 3:59
This looks interesting, but I'm not sure that it's correctly testing Arduino code. From the output you posted, it's compiling to x86_64 architecture, which obviously isn't used for the Arduino. That could introduce bugs caused by conflicts between type implementations.
– Cerin
Jul 10 '18 at 21:35
This looks interesting, but I'm not sure that it's correctly testing Arduino code. From the output you posted, it's compiling to x86_64 architecture, which obviously isn't used for the Arduino. That could introduce bugs caused by conflicts between type implementations.
– Cerin
Jul 10 '18 at 21:35
That sort of bug is certainly possible. Do you have an example I could use for a test case?
– Ian
Jul 12 '18 at 3:59
That sort of bug is certainly possible. Do you have an example I could use for a test case?
– Ian
Jul 12 '18 at 3:59
add a comment |
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.
The only downside is that this doesn't support g++, which makes it useless for testing most Arduino libraries that use C++ features.
– Cerin
Jul 10 '18 at 21:46
add a comment |
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.
The only downside is that this doesn't support g++, which makes it useless for testing most Arduino libraries that use C++ features.
– Cerin
Jul 10 '18 at 21:46
add a comment |
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.
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.
answered Aug 19 '17 at 18:10
ezaquariiezaquarii
1,764812
1,764812
The only downside is that this doesn't support g++, which makes it useless for testing most Arduino libraries that use C++ features.
– Cerin
Jul 10 '18 at 21:46
add a comment |
The only downside is that this doesn't support g++, which makes it useless for testing most Arduino libraries that use C++ features.
– Cerin
Jul 10 '18 at 21:46
The only downside is that this doesn't support g++, which makes it useless for testing most Arduino libraries that use C++ features.
– Cerin
Jul 10 '18 at 21:46
The only downside is that this doesn't support g++, which makes it useless for testing most Arduino libraries that use C++ features.
– Cerin
Jul 10 '18 at 21:46
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Oct 20 '12 at 9:09
ЯegDwight
21.6k93849
21.6k93849
answered May 23 '12 at 17:38
ImreImre
111
111
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Aug 7 '17 at 17:16
Peter Mortensen
13.8k1987113
13.8k1987113
answered Apr 17 '17 at 13:49
sathishsathish
228
228
add a comment |
add a comment |
Try Autodesk circuit simulator. It allows to test Arduino code and circuits with many other hardware components.
add a comment |
Try Autodesk circuit simulator. It allows to test Arduino code and circuits with many other hardware components.
add a comment |
Try Autodesk circuit simulator. It allows to test Arduino code and circuits with many other hardware components.
Try Autodesk circuit simulator. It allows to test Arduino code and circuits with many other hardware components.
edited Aug 7 '17 at 17:18
Peter Mortensen
13.8k1987113
13.8k1987113
answered Jan 3 '17 at 7:53
Sidhant GoyalSidhant Goyal
114
114
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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 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
answered Nov 23 '18 at 5:20
Nandha FrostNandha Frost
166
166
add a comment |
add a comment |
protected by Community♦ Oct 18 '17 at 12:28
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
There is another thread on this question from 2011 at arduino.cc/forum/index.php?action=printpage;topic=54356.0
– Jakob
Jan 8 '12 at 19:58
Thanks @Jakob. An Arduino simulator referenced in that thread (with other potentially useful links at the bottom of the page): arduino.com.au/Simulator-for-Arduino.html
– Matthew Murdoch
Jan 9 '12 at 12:30
4
Unfortunately its only for Windows, I'd like to see a way to simply compile and run Arduino code from command line without any closed source or hardware dependencies.
– Jakob
Jan 12 '12 at 11:55
2
A little update, 5 years later: Simavr is still very much active and has improved a lot since the question has been asked, so I thought it deserves being bumped closer to the top. And it might be the right tool for regression testing, scenario based testing and why not also unit testing. That way the code you test is the same as the one on the target hardware.
– zmo
May 29 '15 at 7:31
For important projects, consider a hardware tester; another MCU than can time and test button/switch reactions, boot time, temp, v/ma usage, weird option permutations, etc. Yes, it's more hardware to build, but it can add a safety layer onto revision making. a lot of pro devices use jtag et al.
– dandavis
Nov 15 '16 at 19:40