Setting up custom test framework fails


I’m trying to set up a custom test framework (which is simply a different version of Unity than what’s hardcoded into PlatformIO, see my previous issue).

Using the official test runner example I cannot make it work.

The compilation error I’m getting is:

g++ -o .pio\build\test_native\test\testMain.o -c -std=c++11 -DPLATFORMIO=60105 -DPIO_UNIT_TESTING -DUNIT_TEST -Iinclude -Isrc -Ilib\AbstractSensor\src -I.pio\libdeps\test_native\Unity\src -I.pio\libdeps\test_native\ArduinoFake\src -Itest -I.pio\libdeps\test_native\Unity\extras\fixture\src -I.pio\libdeps\test_native\Unity\extras\memory\src test\testMain.cpp
g++ -o .pio\build\test_native\program.exe .pio\build\test_native\test\testMain.o -L.pio\build\test_native -Wl,--start-group .pio\build\test_native\lib274\libArduinoFake.a .pio\build\test_native\lib19b\libUnity.a -Wl,--end-group
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: .pio\build\test_native\test\testMain.o:testMain.cpp:(.text+0xc8): undefined reference to `UnityTestRunner'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\test_native\program.exe] Error 1

UnityTestRunner seems to be implemented in unity_fixture.c and defined in unity_fixture.h which is included into my test source.

Not sure if it’s relevant here, but my platformio.ini contains this environment for the unit tests:

platform = native
test_framework = custom
lib_ignore = 
lib_extra_dirs = 
build_flags = -std=c++11 -I.pio/libdeps/test_native/Unity/extras/fixture/src -I.pio/libdeps/test_native/Unity/extras/memory/src
lib_deps = 
lib_compat_mode = off
lib_ldf_mode = chain

Any suggestions on how to make it work? I’ve run out of ideas.
Thanks in advance!

You cannot add to-be-compiled source files in the build system via build_flags. I have however seen people successfully hacking around this using +<path> with src_filter.

Given the library structure at GitHub - ThrowTheSwitch/Unity: Simple Unit Testing for C and the way PlatformIO works, there can only be one source folder for the library, everything else must be beneath it. In your case you need PlatformIO to compile sources from both src/ and extras/memory and extras/fixture – PlatformIO will, with this folder structure, only compile src/, leading you to the undefined references.

You can fix this by reordering the file structure of your custom unity library: Just move extras/memory and extras/fixture in src/, and in the library.json, make an addition for "build": { "flags": [ "-Iextras/memory", "-Iextras/ficture"] } as documented, then you can drop those -I flags from your platformio.ini.

1 Like

Thank you so much for the detailed answer.

Do I understand correctly, that using this method, I should manage the Unity library manually?

Currently, it is specified in my platformio.ini and the library gets overwritten on every build.

lib_deps =

So if I have no choice but to rearrange the library structure, I have to clone the repository manually, change the directory structure and add it to my project.

Otherwise, my changes would be lost at the next build, correct?

Yes. You can just put the files in the lib/ folder of the project (so that lib/Unity/<files here> exist) and remove the .git link from the lib_deps. Remove the .pio folder additionally to get a clean rebuild.

1 Like