The PlatformIO IDE allows unit testing with the Unity framework. A basic project template for unit testing is already available.

As shown on this GitHub feature request, the IDE does not currently make coverage data the easiest to obtain.

First, I used the lcov Linux test coverage project (the options and process for installing them on other systems may vary).

sudo apt-get install lcov

This project can summarize C++ code coverage data, which is produced when gcc is called with the --coverage -lgcov flags. In PlatformIO, this can be combined with your existing unit test environment. Here is one for PC-side unit testing:

platform = native
lib_compat_mode = off
src_filter = +<*> -<*dirs*> -<to/> -<exclude/>
; basically lib and test folders
build_flags =
extra_scripts =

The extra_scripts setting triggers a Python script that executes the generated code, collects an initial coverage report, then removes irrelevant library/test files:

import os


def generateCoverageInfo(source, target, env):
    for file in os.listdir("test"):
        os.system(".pio/build/native/program test/"+file)
    os.system("lcov -d .pio/build/native/ -c -o")
    os.system("lcov --remove '*/tool-unity/*' '*/test/*' '*/MockArduino/*' -o")
    os.system("genhtml -o cov/ --demangle-cpp")

env.AddPostAction(".pio/build/native/program", generateCoverageInfo)

Mostly the work of GitHub user jcw, with some cleanup.

The test C++ suite, stored in the test/ subdirectory and structured like:

void setup() {
    // NOTE!!! Wait for >2 secs if board doesn't support software reset via Serial.DTR/RTS
    // delay(2000);


void loop() {
    UNITY_BEGIN(); // stop unit testing

    // Many more of these

    UNITY_END(); // stop unit testing

can then be run with pio test -e native. Output is generated in the cov/ subdirectory of your project, accessible at cov/index.html.

Browser view of genhtml report based on lcov output for unit test

Clicking the individual links allows you to view the per-line execution details. Best of luck!