Unit Testing Arduino ESP8266 in CI (Native) with libraries

262 views Asked by At

I am using an Arduino ESP8266 with platformio. I am adding Unit Testing with the Unity framework and I want to run the tests in Gitlab CI. In my project, I use other libraries, such as ESP8266WiFi.

I am using the Native platform to run tests and my platformio.ini file looks like this:

[platformio]
default_envs = esp01

[env]
lib_deps = 
    dancol90/ESP8266Ping@^1.0

[env:esp01]
platform = espressif8266
framework = arduino
board = esp01
; Serial Monitor options
monitor_speed = 115200

[env:native]
; Testing without HW in CI, I am using this
platform = native
lib_deps = 
    ${env.lib_deps}
    fabiobatsilva/ArduinoFake@^0.3.1
; Coverage:
lib_compat_mode = off
build_src_filter = +<*> -<*dirs*> -<to/> -<exclude/>
build_flags =
  -lgcov
  --coverage
extra_scripts = ci/test_coverage.py

My test would be something like this:

#include <unity.h>
#include "data.h"

void test_1(void) {
  uint8_t mode = get_value();
  TEST_ASSERT_EQUAL(mode, 0);
}

int main(int argc, char **argv) {
    UNITY_BEGIN();

    RUN_TEST(test_1);

    return UNITY_END();
}

My simplified code would be (data.cpp file):

#include <ESP8266WiFi.h>

uint8_t mode = 0;
uint8_t get_value(){
  return mode;
}

The compilation is working fine with pio run. I am running this command to run the tests:

pio test -e native -vvv

The error is:

test\test_main.cpp:2:25: fatal error: ESP8266WiFi.h: No such file or directory

Maybe ArduinoFake can't emulate this library? Or should I include the library somehow? Or can't I Unit Test the way I am trying to do it?

If I have this test without importing my code, the test passes:

#include <unity.h>
// #include "data.h"

void test_1(void) {
  //uint8_t mode = get_value();
  TEST_ASSERT_EQUAL(0, 0);
}

int main(int argc, char **argv) {
    UNITY_BEGIN();

    RUN_TEST(test_1);

    return UNITY_END();
}
1

There are 1 answers

0
Asi On BEST ANSWER

As it is recommended in the official documentation, the code should be encapsulated in different components. And some components should not depend on exterior libraries, like that I could add unit tests of some of the components.