question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unit testing impossible for frameworks including Unity themselves

See original GitHub issue

What kind of issue is this?

  • [X ] PlatformIO Core. If you’ve found a bug, please provide an information below.

You can erase any parts of this template not applicable to your Issue.


Configuration

Operating system: Windows 10 x64

PlatformIO Version (platformio --version): version 5.2.0a6

Description of problem

The PlatformIO “test” procedure is such that PlatformIO adds its own Unity library to the build.

https://github.com/platformio/platformio-core/blob/834c7b0defe39ff998cd9dc856657aeb837c86ca/platformio/builder/tools/piomisc.py#L353-L361

this however directly conflicts with any framework which already has Unity compiled into it. Such is the case with the https://github.com/arduino/ArduinoCore-mbed in which the precompiled mbed-os library (libmbed.a) has the Unity object files.

Thus it is impossible to e.g. run unit tests with a board = nano33ble framework = arduino configuration without modifications.

Refer to the community topic here

Steps to Reproduce

  1. Create a new Arduino Nano 33 BLE project
  2. Add test\main.cpp from below
  3. Run the “Test” project task

Actual Results

A bazillion linker errors due a conflict with .pio\build\nano33ble\libUnityTestLib.a and .platformio\packages\framework-arduino-mbed\variants\ARDUINO_NANO33BLE\libs\libmbed.a

c:/users/max/.platformio/packages/toolchain-gccarmnoneeabi@1.80201.190214/bin/../lib/gcc/arm-none-eabi/8.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\Max\.platformio\packages\framework-arduino-mbed\variants\ARDUINO_NANO33BLE\libs\libmbed.a(unity.o): in function `UnityBegin':
unity.c:(.text.UnityBegin+0x0): multiple definition of `UnityBegin'; .pio\build\nano33ble\libUnityTestLib.a(unity.c.o):unity.c:(.text.UnityBegin+0x0): first defined here
c:/users/max/.platformio/packages/toolchain-gccarmnoneeabi@1.80201.190214/bin/../lib/gcc/arm-none-eabi/8.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\Max\.platformio\packages\framework-arduino-mbed\variants\ARDUINO_NANO33BLE\libs\libmbed.a(unity.o): in function `UnityEnd':
unity.c:(.text.UnityEnd+0x0): multiple definition of `UnityEnd'; .pio\build\nano33ble\libUnityTestLib.a(unity.c.o):unity.c:(.text.UnityEnd+0x0): first defined here
c:/users/max/.platformio/packages/toolchain-gccarmnoneeabi@1.80201.190214/bin/../lib/gcc/arm-none-eabi/8.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\Max\.platformio\packages\framework-arduino-mbed\variants\ARDUINO_NANO33BLE\libs\libmbed.a(unity.o):(.rodata.UnityStrErr64+0x0): multiple definition of `UnityStrErr64'; .pio\build\nano33ble\libUnityTestLib.a(unity.c.o):(.rodata.UnityStrErr64+0x0): first defined here
c:/users/max/.platformio/packages/toolchain-gccarmnoneeabi@1.80201.190214/bin/../lib/gcc/arm-none-eabi/8.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\Max\.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
c:/users/max/.platformio/packages/toolchain-gccarmnoneeabi@1.80201.190214/bin/../lib/gcc/arm-none-eabi/8.2.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\Max\.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
c:/users/max/.platformio/packages/toolchain-gccarmnoneeabi@1.80201.190214/bin/../lib/gcc/arm-none-eabi/8.2.1/../../../../arm-none-eabi/bin/ld.exe: warning: size of symbol `Unity' changed from 132 in .pio\build\nano33ble\libUnityTestLib.a(unity.c.o) to 160 in C:\Users\Max\.platformio\packages\framework-arduino-mbed\variants\ARDUINO_NANO33BLE\libs\libmbed.a(unity.o)
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\nano33ble\firmware.elf] Error 1

Expected Results

Running unit tests is possible

If problems with PlatformIO Build System:

The content of platformio.ini:

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

Source file to reproduce issue: 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);
}

Additional info

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:5
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
maxgerhardtcommented, Oct 18, 2022

Unable to reproduce.

$ pio test --without-uploading --without-testing -vvv
Collected 1 tests (test_blink)

Processing test_blink in nano33ble environment
------------------------------------------------------------------------------------------------------------------------------------------------------------
Building...
CONFIGURATION: https://docs.platformio.org/page/boards/nordicnrf52/nano33ble.html
PLATFORM: Nordic nRF52 (9.4.0) > Arduino Nano 33 BLE
HARDWARE: NRF52840 64MHz, 256KB RAM, 960KB Flash
DEBUG: Current (blackmagic) External (blackmagic, cmsis-dap, jlink)
PACKAGES: 
 - framework-arduino-mbed @ 3.1.1 
 - tool-sreccat @ 1.164.0 (1.64) 
 - toolchain-gccarmnoneeabi @ 1.80201.181220 (8.2.1)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 37 compatible libraries
Scanning dependencies...
No dependencies
Building in test mode
MethodWrapper(["checkprogsize"], [".pio/build/nano33ble/firmware.elf"])
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [==        ]  16.2% (used 42576 bytes from 262144 bytes)
Flash: [=         ]   8.2% (used 81012 bytes from 983040 bytes)

Core: 6.1.5a4, Nordic nRF52: 9.4.0.

Grab the project file, unpack it, and run the same above as me in the CLI. Does it still show an error?

unity_tests.zip

0reactions
dudeperf3ctcommented, Oct 19, 2022

Removing .pio folder seemed to do the trick. Thanks! It works in my project as well. I have the same content as mentioned in the documentation for both platformio.ini and test folder. I use 2 libraries TFLite and TinyMLx for running ML models on Arduino.

Is it possible to perform the unit testing for TFLite library using platformIO? A sample unit test can be found here or here for reference.

I wonder if GoogleTest Runner can be of help in this case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What about unit testing? - Unity Forum
Hey guys, I love unit testing because it can help develop better code, serves as code documentation, and also helps to add change...
Read more >
Unit testing impossible for frameworks including Unity ...
Unit testing impossible for frameworks including Unity themselves.
Read more >
Cannot build tests - Unit Testing - PlatformIO Community
I want to add unit tests to my project. ... Issue Unit testing impossible for frameworks including Unity themselves · Issue #3980 ......
Read more >
Unit Testing in Unity3D - YouTube
Check out the Course: https://bit.ly/3i7lLtH-------Learn how to use nsubstitute with your Unity games to mock monobehaviors. It'll make unit ...
Read more >
Unit Tests, How to Write Testable Code, and Why It Matters
Unit testing is an essential instrument in the toolbox of any serious software developer. However, it can sometimes be quite difficult to write...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found