Dynamically exclude source files from a library for PIO Unit Testing

I recently installed Atom and platformIO, created a project using the platformIO Home and created a little main.cpp, but unfortunately, when trying to build the project, it throws the the following error:

fatal error: Arduino.h: No such file or directory

on line: #include “Arduino.h”

I couldnt find any help for this problem, and ive already tried around a bit in the settings but i couldnt get it to work.

I am running Windows 10, and I have the original Arduino IDE installed on version 1.8.3

Compiling the sketch with the ArduinoIDE works perfectly fine.

Please share here your main.cpp and platformio.ini files.

main.cpp: https://hastebin.com/epucamicip.cpp
platformio.ini: https://hastebin.com/ruvimoduda.ini

If you need any more information just let me know.

Do you see files here %HOMEDIR%/.platformio/packages/framework-arduinoavr/cores/arduino?

Yes, the whole library seems to be in there.

I apologize for my late response :slight_smile:

Have you resolved this issue?

No I unfortunately have not.

Could you archive and share the somewhere whole project?

Same issue for me. PlatformIO 3.5.0a16 for VS-Code 1.17.0 on Windows 10 x64

Please remove %HOMEDIR%/.platformio/packages/framework-arduinoavr folder and restart a build.

I got the same issue. Removing framework-arduinoavr folder didn’t help
My .ini file

[env:uno]
platform = atmelavr
board = uno
framework = arduino
test_ignore = test_desktop

[env:native]
platform = native

My test

#include <unity.h>    
#ifdef UNIT_TEST
void test_led_builtin_pin_number(void) {
    TEST_ASSERT_EQUAL(13, 13);
}


void setup() {
    UNITY_BEGIN();    // IMPORTANT LINE!
    RUN_TEST(test_led_builtin_pin_number);


}

void loop() {
    UNITY_END(); // stop unit testing
}

#endif

To simplify: I want to write unit tests that don’t upload anything to my board.

Also,

If I got it correctly, calculator unit test example from pio GitHub page, uses #ifndef UNIT_TEST to basically disable everything that uses Arduino.h.

Although it works in this example, it is not practical to put such conditions into every cpp file that wants to use Arduino functionality.

Is there any other way of creating and running unit tests?

You can ignore Arduino’s files using Redirecting...

It looks like ignoring files with Arduino dependencies can be a difficult task. Is there any way to add Arduino dependencies for native tasting configuration?

To be honest. I don’t understand your problem. Could you provide a simple project and describe which issues do you have? We would be glad to see your workflow.

I have something like this:
MotorDriver.h - interface for motor driver with generic methods.
ArduinoMotorDriver.cpp - class that implements MotorDriver and uses arduino lib to control motor.
NoopMotorDriver.cpp - stub implementation for MotorDriver that is used in test
DriverManager.cpp - some class that runs business logic that controls motors. It takes instance of MotorDriver.

I am writing unit test for DriverManager where I will put noop implementation of MotorDriver. I don’t want to test it with Arduino for now, I just want to test Arduino-independent code.

Any suggestions? Is there a way to add Arduino libraries and run local tests

Try this:

  1. Create a library.json file in the root of a library
  2. Write a custom extra script and dynamically replace srcFilter value with your a custom.

See example (I didn’t test it, just a hint)

library.json

{
    "name": "MotorDriver",
    "version": "0.0.0",
    "build": {
        "extraScript": "extra_script.py"
    }
}

extra_script.py

Import('env')

from SCons.Script import COMMAND_LINE_TARGETS

src_filter = ["+<*>", "-<NoopMotorDriver.cpp>"]  # default
if "__debug" in COMMAND_LINE_TARGETS:
    env.Replace(SRC_FILTER=["+<*>", "-<ArduinoMotorDriver.cpp>"])