Unit testing of Wire using ArduinoFake

0

I’m creating unit tests in PlatformIO using Unity with the ArduinoFake library I’m trying to test a piece of code that uses the Wire library

When I try to run the unit test (env:native), it complains that the build cannot find <Wire.h>

my platformio.ini file has the following…

[env:native]
lib_deps = fabiobatsilva/ArduinoFake@^0.3.1

If I comment out #include <Wire.h> in the code under test, then the unit test works, so I assume the <Arduino.h> supplied by the ArduinoFake library also provides the necessary references for Wire - but then the build for the Uno platform fails because there’s no reference to Wire.h

Anyone used ArduinoFake to test code using Wire? - how did you resolve this?

1 Like

But Wire.h is right there in ArduinoFake/src at master · FabioBatSilva/ArduinoFake · GitHub. Also supplied in the 0.3.1 version that PIO supplies.

What is your test code with Wire.h and platformio.ini?

My code to be tested is very simple, it’s just to try the concept
But the test fails unless I comment out #include <Wire.h> in the code under test
The error reported is…

    2 | #include <Wire.h>
      |          ^~~~~~~~
compilation terminated.
*** [.pio\build\native\lib6d9\proto\protoWire.o] Error 1
Building stage has failed, see errors above. Use `pio test -vvv` option to enable verbose output.

Code to be tested… (protoWire.cpp)

#include <Arduino.h>
#include <Wire.h>

#include "protoWire.h"

void startWire(void) {
    Wire.begin();
};

My test code is…

#include <Arduino.h>
#include <unity.h>

using namespace fakeit;

#include "protoWire.h"

void setUp (void) {} /* Is run before every test, put unit init calls here. */
void tearDown (void) {} /* Is run after every test, put unit clean-up calls here. */

void test_startWire(void){
    ArduinoFakeReset();
    When(OverloadedMethod(ArduinoFake(Wire), begin, void(void))).AlwaysReturn();
    startWire();
    Verify(OverloadedMethod(ArduinoFake(Wire), begin, void())).Once();
}

int main(int argc, char **argv)
{
   UNITY_BEGIN();
    printf("***** starting WIRE tests *****\n\n");

    RUN_TEST(startWire);

    return UNITY_END();
}

Platform.ini

[env:uno]

platform = atmelavr

board = uno

framework = arduino

monitor_speed = 115200

lib_deps =

mikalhart/Streaming@^1.0.0

[env:native]

platform = native

build_flags = -std=gnu++11

lib_ldf_mode = chain+

lib_deps =

mikalhart/Streaming@^1.0.0

fabiobatsilva/ArduinoFake@^0.3.1

throwtheswitch/Unity@^2.5.2

lib_compat_mode = off

test_ignore =

Examples/test_SPI
1 Like

Ok - found a solution - wrapped the include in an #ifndef
Not pretty, but does the job

#ifndef PIO_UNIT_TESTING
    #include <Wire.h>
#endif