Cannot build tests

Hello everybody!

I want to add unit tests to my project. I followed the different guides available on the official documentation, but nothing worked.

I thought that maybe, the configuration of my project was not correct, so I tried to setup the most simple project anyone can think of: a hello world. Turns out, it doesn’t work either.

This is the structure of my “test” project, created with pio init --ide vim:

.
├── include
│   └── README
├── lib
│   └── README
├── platformio.ini
├── src
│   └── main.cpp
└── test
    ├── README
    └── main.cpp

Here is the plateformio.ini:

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

My src/main.cpp is very simple:

#include <Arduino.h>

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("Hi there");
  delay(1000);
}

Finally, here is my test/main.cpp:

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

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

If I run pio run and monitor the serial of my arduino, I can read “Hi there” every second. On the other hand, if I run pio test I have a bunch of warnings such as:

/home/furnost/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/8.2.1/../../../../arm-none-eabi/bin/ld: /home/furnost/.platformio/packages/framework-arduino-mbed/variants/ARDUINO_NANO33BLE/libs/libmbed.a(unity.o):(.rodata.UnityStrErrDouble+0x0): multiple definition of `UnityStrErrDouble'; .pio/build/nano33ble/libUnityTestLib.a(unity.c.o):(.rodata.UnityStrErrDouble+0x0): first defined here
/home/furnost/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/8.2.1/../../../../arm-none-eabi/bin/ld: /home/furnost/.platformio/packages/framework-arduino-mbed/variants/ARDUINO_NANO33BLE/libs/libmbed.a(unity.o):(.rodata.UnityStrErrFloat+0x0): multiple definition of `UnityStrErrFloat'; .pio/build/nano33ble/libUnityTestLib.a(unity.c.o):(.rodata.UnityStrErrFloat+0x0): first defined here
/home/furnost/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/8.2.1/../../../../arm-none-eabi/bin/ld: warning: size of symbol `Unity' changed from 132 in .pio/build/nano33ble/libUnityTestLib.a(unity.c.o) to 160 in /home/furnost/.platformio/packages/framework-arduino-mbed/variants/ARDUINO_NANO33BLE/libs/libmbed.a(unity.o)

And an error message:

collect2: error: ld returned 1 exit status
*** [.pio/build/nano33ble/firmware.elf] Error 1

In my real project, I am using different “libraries”: Arduino.h, ArduinoBLE.h, rtos.h and mbed.h. I don’t know if it helps but…

I am not quite sure about what to do, nor do I know what causes the problem. Any help will be greatly appreciated!

Furnost

Meh. The precompiled mbed-os base library that used in the Arduino core already has Unity compiled into it. This creates a conflict.

grafik

There definitely needs to be a core issues opened for this.

Can you try the following:

  • go into the folder /home/furnost/.platformio/packages/framework-arduino-mbed/variants/ARDUINO_NANO33BLE/libs/
  • make a backup copy of libmbed.a as libmbed_original.a
  • open a shell in the folder and execute the command
~/.platformio/packages/toolchain-gccarmnoneeabi/bin/arm-none-eabi-ar dv libmbed.a unity.o unity_handler.o

to remove the unity object files (per here).

Then try reruning “Test”. Does it compile and run properly?

This will break normal firmware compilation though (move back libmed_original.a to libmbed.a), I just want to know whether that works.

EDIT: Above is only true if you also use the Unity testing functions in the main firmware code. Otherwise it is not actually a problem.

Issue Unit testing impossible for frameworks including Unity themselves · Issue #3980 · platformio/platformio-core · GitHub is open by the way.

@maxgerhardt Thank you for this swift answer!

As you expected, I can now run the tests correctly in both projects (test and real). I can also compile normally.

Thanks for opening an issue. In the meantime, I hope that the library will not be updated too much :upside_down_face: .

Furnost

See Custom Unity Library — PlatformIO latest documentation

Could you re-test it with pio upgrade --dev?