Arduino libraries not found when unit testing with Googletest in native platform

I would like to use googletest in order to unit test my arduino due code on the native platform. My platformio.ini looks like this:

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:due]
platform = atmelsam
board = due
framework = arduino
test_framework = unity
test_ignore = native/*
lib_deps = 
	Wire
	SPI
	adafruit/Adafruit ADS1X15@^2.4.0
	adafruit/Adafruit MCP23017 Arduino Library@^2.3.0
	adafruit/Adafruit BusIO@^1.14.1

[env:native]
platform=native
test_framework=googletest
test_ignore = due/*
lib_deps = 
	Wire
	SPI
	adafruit/Adafruit ADS1X15@^2.4.0
	adafruit/Adafruit MCP23017 Arduino Library@^2.3.0
	adafruit/Adafruit BusIO@^1.14.1

Now, the test/native/test_mymodule.cpp file includes <gtest/gtest.h> as well as <mymodule.h> which is located at lib/mymodlue/mymodule.h. The file mymodule.h includes <Adafruit_MCP23X08.h>, which is listed in the lib_deps for the native environment. However, if I run pio test -e native, I get the following error:

lib\mymodule/mymodule.h:5:10: fatal error: Adafruit_MCP23X08.h: No such file or directory

Why is this file not found? Also, is it even possible to test code in the native env that includes Arduino libraries?

  1. On platform = native you by-default don’t an have Arduino framework in the project, best you can do is mock it with https://github.com/FabioBatSilva/ArduinoFake/ per platformio-examples/unit-testing/arduino-mock at develop · platformio/platformio-examples · GitHub
  2. The library dependency finder (LDF) will deem all these Adafruit / Arduino libraries to be incompatible with platform = native, but if you supply the missing Arduino functionality with a third-party (mocking) library, then you can override that check by setting lib_compat_mode to off
2 Likes

Thank you!

That’s actually the reason why I wanted to use googletest, so I can use googlemock for mocking all the Arduino libraries. However, I’ve never done this before so there’s a follow-up question (probably the answer to it is what you mentioned in point 2, but I’m not sure I understand it correctly):

Following the gMock documentation if I want to write a class that mock for example a Adafruit_MCP23X08 object, my class MockMPC23x08 has to inherit from Adafruit_MCP23X08, so I still need to include <Adafruit_MCP23X08.h> and then I still have the compilation error. Or is this the case where I can set lib_compat_mode to off?