SimpleArduinoFake is a simple mocking framework for Arduino,, based on FakeIt. Its goal is to allow unit testing of your Arduino project natively, with no Arduino. This supports:
- Complex testing that might exceed physical board limits. E.g. RAM
- Unit test coverage, which is not possible on a physical board.
This project is a hard fork of ArduinoFake, with some simplifications and bug fixes:
- No proxies. Arduino classes (E.g.
Stream) are mocked directly rather than via a proxy object, so code is DRY - No object slicing
- No memory leaks
- More modern code style for a simpler code base: no macros, use of namespaces & no dynamic memory allocation
- Compiles on Windows, using mingw64.
You should include the following header in your test file:
#include <SimpleArduinoFake.h>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 SimpleArduinoFake:
#include <SimpleArduinoFake.h>
using namespace fakeit;
// test/test_main.cpp
void test_loop(void)
{
auto &functionFake = SimpleArduinoFake::getContext()._Function;
When(Method(functionFake, digitalWrite)).AlwaysReturn();
When(Method(functionFake, delay)).AlwaysReturn();
loop();
Verify(Method(functionFake, digitalWrite).Using(LED_BUILTIN, HIGH)).Once();
Verify(Method(functionFake, digitalWrite).Using(LED_BUILTIN, LOW)).Once();
Verify(Method(functionFake, delay).Using(100)).Exactly(2_Times);
}Look at the examples or the tests for examples.
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.
Contributions are welcome:
- The project uses PlatformIO for building, unit testing and CI.
- Make sure your changes are covered by unit tests.