How do I interpolate the evaluated `test_dir` in `platformio.ini`?

I am building my src files for my tests and wanted to implement mock implementations for headers defined in include/ via test/native/include. I figured that the following lets me do it.

[env:native]
platform = native
test_filter = native/*
test_build_src = true
build_flags =
  ${env.build_src_flags}
  -Itest/native/include

What I noticed is that using ${platformio.test_dir}/native/include does not work. Am I doing something wrong? What would be a recommended way to avoid the hardcode?

Hello,

You’re on the right track with using environment variables to avoid hardcoding paths in your PlatformIO configuration. Here’s how to achieve your goal:

  1. Define Include Directory Variable:

In your file, define a new environment variable named that points to your mock header location:platformio.iniTEST_INCLUDE_DIR

[env:native]
platform = native
test_filter = native/*
test_build_src = true
build_flags =
  ${env.build_src_flags}
  -I${TEST_INCLUDE_DIR}

Set the Variable Value:

There are two ways to set the value for :TEST_INCLUDE_DIR

Project-specific: Add a line outside any environment block in your file. This sets the variable for the entire project.TEST_INCLUDE_DIR=path/to/test/native/includeplatformio.ini/HealthCareGov

Environment-specific: Set the variable outside the project directory (e.g., in your terminal before running PlatformIO commands). This can be useful for different build environments:

export TEST_INCLUDE_DIR=path/to/test/native/include
pio test  # Now the variable is set for this PlatformIO command

Remember to replace path/to/test/native/include with the actual location of your mock header files.

By following these steps, you can avoid hardcoding the include path and maintain a more flexible configuration for your tests.

Thanks for the suggestion. However, my main question is whether PlatformIO supports querying the test_dir field in the platformio section.

I don’t want to redefine fields that already exist. In my case, I’m just curious as to why -I${platformio.test_dir}/native/include doesn’t work when -Itest/native/include works.

Could we ask you to report an issue to Issues · platformio/platformio-core · GitHub ? Thanks!