PlatformIO Community

Unit test recommended structure multi-test


I am trying to implement unit testing, I have in the past used cpputest.

I am currently testing on native only, and using only a single test file I have successfully got tests working.

Now I want to start testing multiple modules. I am getting ‘multiple definition’ errors for setUp, main etc. if I try to add individual test files under separate folders

I run tests with CLI command
platformio test -e native

My test structure looks like
└── native
├── pulse_protocol
│ └── test_pulse_protocol.cpp
└── util
└── test_util_functions.cpp

Is there a specific way to implement multiple tests like this?


After posting, I tried removing the top level ‘native’ directory, and both tests were run under the native environment without issue.

However, I still can not add multiple test files under a single directory/module - I get the same errors as previous. Is there any way to do this?

├── pulse_protocol
│ ├── test_pulse_protocol_inputs.cpp
│ └── test_pulse_protocol_outputs.cpp
└── util
└── test_util_functions.cpp

1 Like

See example with multi-tests structure

I’m having the same issue where I’m trying to test multiple libraries for the same environment. I can’t use the suggested structure because that is for the same library in multiple environments.

Is there any way to achieve this?


Could you provide a simple project to reproduce your workflow? Maybe lib_ignore could help you.

Thanks @ivankravets, I managed to work out what I was doing wrong.

I had too many levels of nesting:

  • tests
    • desktop
      • test_analyzer
        • test_analyzer.cpp
      • test_endstop
        • test_endstop.cpp

When I removed the desktop level, everything worked as expected:

  • tests
    • test_analyzer
      • test_analyzer.cpp
    • test_endstop
      + test_endstop.cpp

Yes, this is the only supported sturcutre.

1 Like

If you have all your test files in the ‘test’ directory PlatformIO will create duplicates of functions when compiling.

I just split the tests out into another directory and include them inside my main test file like so:

  • test
    • test_main.cpp
  • tests
    • tests_foo
      • foo_tests.cpp
    • tests_bar
      • bar_tests.cpp

Then my test_main.cpp file looks like:

#include <unity.h>

#include "../tests/tests_foo/foo_tests.cpp"
#include "../tests/tests_bar/bar_tests.cpp"

void setup(){



void loop(){


Where run_bar_tests() is in bar_tests.cpp and just runs all the tests for class Bar

There might be a better way to do it, but this allows you to nest as deep as you’d like.