PlatformIO has good documentation on it: Redirecting.... Especially see the examples that are linked in there. (platformio-examples/unit-testing at develop · platformio/platformio-examples · GitHub)
Basically, the native
platform attempts to use the toolchain of the host computer to compile the ‘firmware’ or run tests. There are even mocking libraries for Arduino so that libraries depending on Arduino.h
and the functionality therein (see arduino-mock
example).
For your library this is also possible. The only place where you really depend on Arduino.h
is in the code in src/helpers
using String
, in other places you do #include <Arduino.h>
just to get the standard fixed-width integer types, which are also available in just #include <stdint.h>
.
If I add
[env:native]
platform = native
build_flags = -std=gnu++11
lib_deps =
ArduinoFake
test_build_project_src = yes
to the bottom of the platformio.ini
, and further rewrite these parts
so that the standard main()
function is compiled for desktop, by using the code
#ifdef ARDUINO
void setup() {}
void loop() {
#else
int main() {
#endif
to replace the setup()
and loop()
with just main()
(same for here), I can use
> pio test -e native
Verbose mode can be enabled via `-v, --verbose` option
Collected 2 items
Processing test_converter in native environment
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Building...
Testing...
test\test_converter\test_converters.cpp:234:test_ms2s_lt_quocient [PASSED]
test\test_converter\test_converters.cpp:235:test_ms2s_lt_remainder [PASSED]
test\test_converter\test_converters.cpp:236:test_ms2s_eq_quocient [PASSED]
test\test_converter\test_converters.cpp:237:test_ms2s_eq_remainder [PASSED]
test\test_converter\test_converters.cpp:238:test_ms2s_gt_quocient [PASSED]
test\test_converter\test_converters.cpp:239:test_ms2s_gt_remainder [PASSED]
test\test_converter\test_converters.cpp:241:test_s2min_lt_quocient [PASSED]
test\test_converter\test_converters.cpp:242:test_s2min_lt_remainder [PASSED]
test\test_converter\test_converters.cpp:243:test_s2min_eq_quocient [PASSED]
test\test_converter\test_converters.cpp:244:test_s2min_eq_remainder [PASSED]
test\test_converter\test_converters.cpp:245:test_s2min_gt_quocient [PASSED]
test\test_converter\test_converters.cpp:246:test_s2min_gt_remainder [PASSED]
test\test_converter\test_converters.cpp:248:test_min2h_lt_quocient [PASSED]
test\test_converter\test_converters.cpp:249:test_min2h_lt_remainder [PASSED]
test\test_converter\test_converters.cpp:250:test_min2h_eq_quocient [PASSED]
test\test_converter\test_converters.cpp:251:test_min2h_eq_remainder [PASSED]
test\test_converter\test_converters.cpp:252:test_min2h_gt_quocient [PASSED]
test\test_converter\test_converters.cpp:253:test_min2h_gt_remainder [PASSED]
test\test_converter\test_converters.cpp:255:test_ms2time_999_ms [PASSED]
test\test_converter\test_converters.cpp:256:test_ms2time_1_s [PASSED]
test\test_converter\test_converters.cpp:257:test_ms2time_1_s_1_ms [PASSED]
test\test_converter\test_converters.cpp:258:test_ms2time_59_s [PASSED]
test\test_converter\test_converters.cpp:259:test_ms2time_1_min [PASSED]
test\test_converter\test_converters.cpp:260:test_ms2time_1_h [PASSED]
test\test_converter\test_converters.cpp:262:test_stringify_time [PASSED]
-----------------------
25 Tests 0 Failures 0 Ignored
OK
=============== [PASSED] Took 3.17 seconds ===============
Processing test_helpers in native environment
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Building...
Testing...
test\test_helpers\test_helpers.cpp:50:test_left_zeros_gt [PASSED]
test\test_helpers\test_helpers.cpp:51:test_left_zeros_eq [PASSED]
test\test_helpers\test_helpers.cpp:52:test_left_zeros_lt [PASSED]
test\test_helpers\test_helpers.cpp:53:test_n_chars [PASSED]
-----------------------
4 Tests 0 Failures 0 Ignored
OK
=============== [PASSED] Took 4.43 seconds ===============
Test Environment Status Duration
-------------- ------------- -------- ------------
test_converter native PASSED 00:00:03.172
test_helpers native PASSED 00:00:04.435
=============== 2 succeeded in 00:00:07.607 ===============
so all tests are compilable and have executed correctly by using the native compiler, no Arduino Uno needed.
Of course I just use pio test
, it will run on both environments, testing the embedded and native case.
Test Environment Status Duration
-------------- ------------- -------- ------------
test_converter uno PASSED 00:00:10.633
test_converter native PASSED 00:00:03.109
test_helpers uno PASSED 00:00:08.295
test_helpers native PASSED 00:00:03.132
=============== 4 succeeded in 00:00:25.169 ===============
Note that that ofc needs a host compiler. I use Windows 10 with the MinGW toolchain installed to give me a GCC and friends.