By dave | November 18, 2018

Getting started Unit testing with Arduino platform

This article discusses how to unit test a simple project with Arduino, if you’re not used to writing unit tests, or need more background, then first read this guide on unit testing embedded projects.

Presently, all our testing using a custom test framework that is built into IoAbstraction, it can be considered as the least number of components that implement a test framework. It works pretty well overall, we wrote this because we needed the testing to work on all Arduino boards, and even mbed boards too.

Our unit test library is really considered somewhat as an integral part of our libraries, and we advertise it as such, it may work for what you need, if your expectations are at the right level. It is available from here:

However, there is also AUnit, which if you do not need it to work on all boards may well work for you:

  • AUnit is available through library manager, just install it direct from the IDE.
  • [] the library source code itself.
  • [] if you want to automate the running of the tests.

For a project that has a sketch and source files

In this scenario we have application, with a .INO file that starts the application, and calls upon the services of a component SomeComponent; which is in the src directory. In this case, we’d want to test at least the component, so we’d create a tests directory at the top level and create TestSomeComponent under this directory, just like a normal Arduino sketch (IE: file name matches directory name).


For a library that has examples and tests

Before I discovered how easy it was to have proper unit tests run on the Arduino platform, I used to mix examples with unit tests, I’d actually have very complex examples that were really unit or integration tests. Now I separate them out, as they are very different use cases. Tests should conclusively test a component of the system; whereas examples should demonstrate how to use the components of the library, in the simplest way possible.

Let’s now look at an example layout for a library; where there examples are both examples and tests. We create a tests folder as above, and create any tests we wish to run under this folder, in exactly the same way as an Arduino sketch.


Getting started in the code

Much of how to get started with AUnit is available in the documentation on the AUnit github page, and I certainly don’t want to duplicate it here. What I will do here is give a simple example, to show how easy it is to get started.

Step 1 - install the AUnit library through library manager.

Step 2 - create a tests directory in the top level of your project.

Step 3 - create a CompTest1 directory under that and an INO file with the same name (replace test name as desired)

Step 4 - write the test..

After the above, we have an empty test that does nothing, we need something to test. Let’s create our LibComponent.h from above that just adds up two numbers and returns the result. Then write a test



#include <Arduino.h>

class LibComponent {
    int addTwoNumbers(int a, int b) {
        return a + b;



#line 2 "CompTest1.ino"

#include <AUnit.h>
#include "LibComponent.h"

using namespace aunit;

// here's what we are actually testing
LibComponent compUnderTest;

// in setup, we prepare the serial port.
void setup() {

// we just call the test runner in loop
void loop() {
test(addingTwoNumbersTogether) {
    assertEqual(compUnderTest.addTwoNumbers(10, 10), 20);

Results of running the test

[Starting] Opening the serial port - /dev/cu.usbmodemFD1321
[Info] Opened the serial port - /dev/cu.usbmodemFD1321
TestRunner started on 1 test(s).
Test addingTwoNumbersTogether passed.

That is about the simplest unit test that can be written. It should give you a starting point for writing tests on Arduino. To run the test from an IDE just open the sketch and run it. It can be automated using AUniter as discussed above.

Please also see my page linked below on testing with IoAbstraction; which makes testing even easier.

Other pages within this category

comments powered by Disqus

This site uses cookies to analyse traffic, and to record consent. We also embed Twitter, Youtube and Disqus content on some pages, these companies have their own privacy policies.

Our privacy policy applies to all pages on our site

Should you need further guidance on how to proceed: External link for information about cookie management.

Send a message

Please use the forum for help with UI & libraries.

This message will be securely transmitted to our servers.