FabioBatSilva / ArduinoFake

Arduino mocking made easy
https://platformio.org/lib/show/1689/ArduinoFake
MIT License
105 stars 48 forks source link
arduino mock testing

ArduinoFake

Build Status

ArduinoFake is a simple mocking framework for Arduino. ArduinoFake is based on FakeIt and can be used for testing your arduino project natively. No arduino required ! #

Quickstart

Includes

You should include the following header in your test file:

#include <ArduinoFake.h>

Stubbing

Assuming we have the following arduino code:

// src/main.cpp
void loop()
{
    // turn the LED on (HIGH is the voltage level)
    digitalWrite(LED_BUILTIN, HIGH);
    // wait for a second
    delay(100);
    // turn the LED off by making the voltage LOW
    digitalWrite(LED_BUILTIN, LOW);
     // wait for a second
    delay(100);
}

It can be tested using ArduinoFake:

#include <ArduinoFake.h>

using namespace fakeit;

// test/test_main.cpp
void test_loop(void)
{
    When(Method(ArduinoFake(), digitalWrite)).AlwaysReturn();
    When(Method(ArduinoFake(), delay)).AlwaysReturn();

    loop();

    Verify(Method(ArduinoFake(), digitalWrite).Using(LED_BUILTIN, HIGH)).Once();
    Verify(Method(ArduinoFake(), digitalWrite).Using(LED_BUILTIN, LOW)).Once();
    Verify(Method(ArduinoFake(), delay).Using(100)).Exactly(2_Times);
}

Checkout the examples for many more examples! Or take a look at the tests

Troubleshooting

If you get a segfault while running your unit tests, eg:

Program errored with 3221225477 code

Check to make sure you have stubbed all the Arduino methods you are calling.

Contributing

If you want to extend ArduinoFake library to add missing functions (for example attachInterrupt) see contribution guidelines.