PlatformIO Community

Native Unit Test on M1 Mac

I want to unit test natively on an M1 Mac. Therefore I have the following simple code located at /test/test_main.cpp:

#include <unity.h>

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

void setup() {
  UNITY_BEGIN();  // IMPORTANT LINE!
  RUN_TEST(simple_test);
}

void loop() {
  UNITY_END();  // stop unit testing
}

Now I get the following error when trying to execute the test natively:

Building...
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
g++ -o .pio/build/latest_stable/test/test_main.o -c -DPLATFORMIO=50203 -DUNIT_TEST -DUNITY_INCLUDE_CONFIG_H -Isrc -I.pio/build/latest_stable/UnityTestLib -I/Users/kaikuklok/.platformio/packages/tool-unity test/test_main.cpp
g++ -o .pio/build/latest_stable/program .pio/build/latest_stable/test/test_main.o .pio/build/latest_stable/test/tmp_pio_test_transport.o -L.pio/build/latest_stable .pio/build/latest_stable/libUnityTestLib.a
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
*** [.pio/build/latest_stable/program] Error 1

My settings platformio.ini looks like the following for native:

; Latest stable version
[env:latest_stable]
platform = native
test_ignore = test_embedded

Has somebody an idea how I can get this to work?

The error message is correct, you have not defined main() – just setup() and loop(), which is Arduino specific and thus don’t get called in the test environment for a desktop.

Per official example you should do it in the style

If you want to use the same test file for both embedded + desktop tests, see here.

2 Likes

Thanks for clarifying that. It works now as expected.
Sometimes you just don’t get it …