With PlatformIO 6.1.x, unit test builds no longer have the -DUNIT_TEST -DPIO_UNIT_TESTING compiler flags defined. Is there a recommended way to conditionally compile code that is for unit test only? I cannot see a way to add compiler flags specifically for test builds.
It’s still there
So if it isn’t showing up, please file an issue in Issues · platformio/platformio-core · GitHub.
Export Unit Testing flags only to the project build environment (
projenv
, files in “src” folder)
See Release Notes — PlatformIO latest documentation
Do you need testing flags for the libraries?
I suppose so. We decided to put most of our (application) code under lib, and located the unity unity test functions alongside the modules, so;
lib - ble_if - ble_if.c
|- ble_if.h
|- test_unity_ble_if.c
|- test_unity_ble_if.h
- gpio_if - gpio_if.c
|- gpio_if.h
|- test_unity_gpio_if.c
|- test_unity_gpio_if.h
We use #ifdef UNIT_TEST within these source modules to select or exclude sections or whole files.
With no changes other than upgrading from 6.0.2 to 6.1.1, here are the compiler invocations for one lib source file;
PlatformIO 6.0.2
gcc -o .pio\build\native_nonc\lib71f\gpio_if\gpio_interface.o -c -std=c11 -pthread -Wall -Wextra --coverage -fprofile-arcs -ftest-coverage -fprofile-abs-path -Wextra -DPLATFORMIO=60002 -DSSAJ_NONCONNECTED -DUNIT_TEST -DPIO_UNIT_TESTING -DUNITY_INCLUDE_CONFIG_H -Ilib\gpio_if -Ilib\conf_if -I.pio\libdeps\native_nonc\Unity\src -Ilib\utils -Ilib\logging -Ilib\common -Itest\test_cmn -Itest lib\gpio_if\gpio_interface.c
PlatformIO 6.1.1
gcc -o .pio\build\native_nonc\lib71f\gpio_if\gpio_interface.o -c -std=c11 -pthread -Wall -Wextra --coverage -fprofile-arcs -ftest-coverage -fprofile-abs-path -Wextra -DPLATFORMIO=60101 -DSSAJ_NONCONNECTED -Ilib\gpio_if -Ilib\conf_if -I.pio\libdeps\native_nonc\Unity\src -Itest -Ilib\utils -Ilib\logging -Ilib\common lib\gpio_if\gpio_interface.c
Yes, we stopped adding unnecessary macros to the global scope. They can affect frameworks, SDKs, or 3rd party libraries. For example, Arduino for ESP32 and ESP-IDF come with their own Unity library. The -DUNIT_TEST
may affect them. Maybe, it makes sense to pass -DPIO_UNIT_TESTING
to the libraries.
Nevertheless, I don’t like so much how you use Unit Testing, especially putting test_unity_gpio_if*
under the IF guards. How about adding library.json
and controlling SRC_FILTER
in extraScript — PlatformIO latest documentation ?
Import("env")
if "test" not in env.GetBuildType():
env.Append(SRC_FILTER=["-<test_*>"])
P.S: I didn’t test it.
Resolved in
Please re-test with pio upgrade --dev
.
Thanks Ivan, that does work, once I fix our code to work from PIO_UNIT_TEST rather than UNIT_TEST.
I thought that you might completely remove defining “UNIT_TEST” in favour of “PIO_UNIT_TESTING”, but you seem to have retained it when building source files in the test/
directory. Was that your intention?
I prefer using conditional compilation over the library.json
option because #defines should work with any set of IDE/build tools. Perhaps the better option is to move the unit test code under test/
and not keep it in the same directory as the run-time code that it tests…
UNIT_TEST
conflicts with other macros potentially available in SDKs, frameworks, or even unit testing frameworks. PIO_UNIT_TESTING
should bring any problems to the 3rd party components.
Thanks, it makes sense. We will release PIO Core 6.1.2 soon.
What is your general experience using PlatformIO Unit Testing? What could we improve?
Actually pretty good Ivan. A lot seems to work “out of the box” natively and with the couple of target boards I’ve used it on (nRF52-DK + Zephyr & ST723ZG + FreeRTOS). Having support for Unity and GoogleTest is great for my projects.
I think that the documentation you had with 5.x.x was a bit light, but I see you’ve added a lot to it with 6.x.x along with quite a bit of new capability. Unfortunately I haven’t had the time to do much with that yet, so I can’t give you much feedback there. When I do get to grips with it I’ll let you know.
There does seem to be an issue with 6.x.x finding tests under the tests/
folder. The documentation talks about the “names” of tests for test_filter
and test_ignore
, but doesn’t say what that name actually is. I think it is the test_xxx
directory name containing the test main, not the name of the test main .c file, nor the names of the test functions. However, when I tried 6.0.2 on one project which has test/native/test_abc
, test/native/test_def
and test/target/test_xyz
folders, pio test -e target
was always finding and building the native tests as well, no matter what I did. I need to go back and confirm the behaviour so I can properly characterise the problem, but I think there is some kind of problem there.
Thanks, I’ve just improved docs
I hope it is more clear now on how to use these options.
You need to use filter/ignore
options. -e
- means, processing specified environment from platformio.ini
.
Yes, that is clearer now, thanks Ivan. I’ll get back to you if I see there really are problems with building unexpected tests, but it will probably be a week or so before I get back to this. You are so responsive and quick, I sorry to slow you down!