Dynamically exclude source files from a library for PIO Unit Testing

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>"])