Googletest build issues with platformio


I am trying to perform unit testing with googletest. However, I encountered build issues and was not able to perform testing.

Not sure what is the exact platformio.ini settings with googletest.

I tried to read the paltfomio community but was not able to find the solution.

Here are the settings of my platformio:

platform = atmelavr
board = megaatmega1280
framework = arduino
lib_deps = greiman/SdFat @ ^2.2.0, 4dsystems/genieArduino @ ^1.5.3, SPI
monitor_speed = 115200
build_src_flags = -w
test_framework = googletest
check_tool = cppcheck
check_skip_packages = yes
check_severity = high, medium, low
check_flags =
  cppcheck: --enable=warning
  cppcheck: --inline-suppr
lib_compat_mode = off
build_flags = -std=c++11
                     -D TESTING ;Build flag for testing
                     -Igoogletest/include -Igoogletest
test_build_src = true ;Build from the sourde code folder

I created a sample google test under test> googletest_sample.cpp

#include <gtest/gtest.h>
// TEST(...)
// TEST_F(...)
//#if defined(ARDUINO)
#include <Arduino.h>

void setup()

    // should be the same value as for the `test_speed` option in "platformio.ini"
    // default value is test_speed=115200
    // if you plan to use GMock, replace the line above with
    // ::testing::InitGoogleMock();

void loop()
  // Run tests
  if (RUN_ALL_TESTS();
  // sleep for 1 sec

int main(int argc, char **argv)
    ::testing::InitGoogleTest(&argc, argv);
    // if you plan to use GMock, replace the line above with
    // ::testing::InitGoogleMock(&argc, argv);
    if (RUN_ALL_TESTS();
    // Always return zero-code and allow PlatformIO to parse results
    return 0;

Also I have encountered below errors:
Building & Uploading…

In file included from test\googletest_sample.cpp:1:0:
.pio\libdeps\megaatmega1280\googletest\googletest\include/gtest/gtest.h:52:10: fatal error: cstddef: No such file or directory
 #include <cstddef>
compilation terminated.
*** [.pio\build\megaatmega1280\test\googletest_sample.cpp.o] Error 1
Uploading stage has failed, see errors above. Use `pio test -vvv` option to enable verbose output.

Please let me know for any ideas.

Thank you very much.

No, google test needs a well-working C++ STL library to compile with a lot of headers present, and the AVR toolchain just doesn’t supply these – it’s really bad. See Testing Frameworks — PlatformIO latest documentation for the compatibility chart, atmelavr is not listed in there.

Even with the latest-greatest AVR-G++ 12.x (here) and supplemental files like GitHub - mike-matera/ArduinoSTL: An STL and iostream implementation based on uClibc++ that supports my CS-11M class. and GitHub - modm-io/avr-libstdcpp: Subset of the C++ standard library for AVR targets etc., it does not compile because it’s always missing some header / functionality, I personally tried this a month ago.

On AtmelAVR, the only testing framework available to you is the simplest Unity framework.

1 Like