Custom Unity Library

There are frameworks such as Arduino with Mbed core and Espressif IoT Development Framework that contain a prebuilt library of Unity testing framework. Using PlatformIO’s version of Unity will lead to compilation issues (“multiple definitions”). See issue #3980.

The example below allows you to use PlatformIO’s Unity Test Runner and your custom Unity library. It can be a part of Frameworks or as a project dependency declared in lib_deps.

Project Example

We have a project based on Arduino Mbed-core that ships with a custom prebuilt “libunity.a”. Let’s leverage from the existing Unity Test Runner and disable the default throwtheswitch/Unity package.

Structure

/project
├── platformio.ini
└── test
    ├── test_blink
    │   └── test_main.cpp
    └── test_custom_runner.py

2 directories, 3 files

File contents

platformio.ini

[env:nano33ble]
platform = nordicnrf52
board = nano33ble
framework = arduino
test_framework = custom

test/test_custom_runner.py

Custom Test Runner based on UnityTestRunner.

from platformio.public import UnityTestRunner

class CustomTestRunner(UnityTestRunner):

    # Ignore "throwtheswitch/Unity" package
    EXTRA_LIB_DEPS = None

    # Do not add default Unity to the build process
    def configure_build_env(self, env):
        pass

test/test_blink/test_main.cpp

#include <Arduino.h>
#include <unity.h>

void setUp(void) {
    // set stuff up here
}

void tearDown(void) {
    // clean stuff up here
}

void simple_test(void)
{
    TEST_ASSERT_EQUAL(33, 33);
}

void setup()
{
    delay(2000);

    UNITY_BEGIN();
    RUN_TEST(simple_test);
    UNITY_END();
}

void loop()
{
    delay(1000);
}