Request for PIO feature test_build_flags

When writing unit tests, it is often handy to be able to have access to private data members of an object.
(I know some die-hard unit testers advocate that you should test only via the public interface, but I think a more pragmatic approach makes more sense)

The way I do this is by conditionally disabling the private: keyword when unit testing

#ifndef unitTesting
  private:
#endif

and then a build flag in platformio.ini can activate this:
build_flags = -D unitTesting

To avoid forgetting to disable this access in normal (production) builds, it would be better if there would be separate build flags for the test builds.

So here is my request for a
test_build_flags = -D unitTesting

Thank you Pio team!

Feature requests should be formally filed in Issues · platformio/platformio-core · GitHub.

But:

  1. PlatformIO automatically activates the PIO_UNIT_TESTING macro when executing tests without you having to specify anything
  1. You can use advanced scripting to check for if you’re currently building for a unit test and add arbitrary flags then. As the trigger, you can use
1 Like

That would be me :wink:

You should test if the car drives, not if it has an engine inside. You should test behaviour, not inner workings.

If you feel that there is too much magic going on inside, maybe you break the Single Responsibility Principle [SRP]? Then a good idea is to extract some code into a new unit and test that separately.

I mean, instead of looking of workarounds to hack your own code, a bit of refactoring might be a better solution.