Library with buildable example AND unit tests

Hi!
I’m creating a library using platformio, and I want it:

  1. to be arduino compatible
  2. to have unit tests you can actually build and run
  3. to have examples you also can build and run

So far, here is what I came up with. My source are organized this way:

.
├── README.md
├── examples
│   └── Simple
│       └── Simple.cpp
├── include
├── library.json
├── library.properties
├── platformio.ini
├── src
│   ├── nmeaparser.cpp
│   └── nmeaparser.h
└── test
    └── tests.cpp

My platformio.ini file is the following:

[platformio]
#lib_dir = .
#src_dir = examples/Simple
include_dir = src

[env:arduinomega2560]
platform = atmelavr
framework = arduino
board = megaatmega2560
test_build_project_src = yes

[env:esp32dev]
platform = espressif32
framework = arduino
board = esp32dev
test_build_project_src = yes
upload_speed = 3000000

If I want to run unit tests, [platformio] section is the one above. If I want to build the example, I must switch commented lines to this:

[platformio]
lib_dir = .
src_dir = examples/Simple
#include_dir = src

Is there a better way to have both?

1 Like