Native Unit Testing trying to import internal modules - ESP8266Wifi

Hey there. I’m trying to create a unit testing workflow for my code.

The problem is: When I try testing my code on native environment importing my modules. Some of those modules require native libraries such as ESP8266Wifi so the test fails (not found headers)

It does the same for MQTT.h and other libraries that run only on the embedded device.

There is any way to mock those headers or make them run properly on my local machine?

1 Like

At least for the esp8266-libs I have had good experiences with this library

HTH

I was about to suggest that, but it mainly mocks the standard Arduino headers, not the ESP8266 libraries. I haven’t yet seen anything that does that. Usually the components are rewritten so that if they’re compiled for native, these headers are not used but input / output is coming from some mock object elsewhere.

I think Arduino Fake doesn’t work for this purpose.

Imagine an example, I must test a Network.cpp class. The class needs some datatypes from MQTT.h library which compiles only for the esp environment.

If I try to include the header Network.h it doesn’t compile because the MQTT.h is missing.

There’s any way to do it?

1 Like

I can only think that you need abstraction yourself away from Arduino related code.

I mean, make your own interface [„abstract base class”] with methods you need in your code [for example I created Servo abstract base class with virtual methods like getAngle() and `setAngle().

In your “consumer” code, point to this “interface”. Now you can test your consumer code with some some implementation of Servo [extend it and implement the methods as you like for tests: DummyServo].

Finally, create “real” implementation that you will use in your device environment [ArduinoServo extending Servo].

Dependency injection FTW.


I know, you end up making ALL the abstractions by hand, by yourself. Yeah. Arduino’s lib sucks that way. No abstractions, global var space pollution.