Status of support for debug of unit tests?

Continuing the discussion from Debug Unit Tests?:

I was looking for some informations, because I face the problem that a test gives unexpected results (aka fails :wink:) and I would really like to debug it to find the reason for the failure. During research I found that old discussion and as far I could see, the issue is still open.

Are there any plans to support debugging of tests in a native environment, too?

I would find this very helpful too! I’m interested in debugging unit tests in the native environment only.

I don’t know whether this will help. I can’t get it to run myself as I can’t work out how to specify the simulator environment in the build task.

Even if I could get it working, I’m not sure it will help with debugging unit tests though.

Should be resolved per Debugging with Unit Testing · Issue #948 · platformio/platformio-core · GitHub already, no? It’s also in the docs.

1 Like

Hi Max,

I might be doing something wrong that you can spot. I’ve included the contents of platformio.ini, tasks.json and launch.json below.

It tries to build the debug environment but I get two issues:

Warning! Ignore unknown configuration option debug_test in section [env:debug]

src\HardwareMicrophone.h:5:10: fatal error: Arduino.h: No such file or directory

I’m hoping to be able to debug through the VS Code UI. Is there anything I can try?

Thanks!

platformio.ini

[env:debug]
platform = native
build_type = debug
debug_test = yes
test_filter = test_analyzer

launch.json

{
“name”: “Debug simulator”,
“type”: “cppdbg”,
“request”: “launch”,
“program”: “${workspaceFolder}/.pio/build/simulator/program”,
“args”: ,
“environment”: ,
“cwd”: “${workspaceFolder}”,
“preLaunchTask”: “PlatformIO: Build (simulator)”
},

task.json

{
“type”: “PlatformIO”,
“task”: “Build”,
“problemMatcher”: [
“$platformio”
],
“group”: “build”,
“label”: “PlatformIO: Build (simulator)”
}

Are you sure you have the latest core version? Try opening a CLI and execute pio upgrade --dev.

Is your test really named yes? Per linked documentation you must use the name of the test to debug here.

A name of a unit test to be debugged.

Great thanks. I was on 5.1.1 which I thought was enough. I’m now on version 5.2.0a4 and the first issue is fixed.

I’m still getting build errors like this:

src\HardwareStepper.h:4:10: fatal error: Arduino.h: No such file or directory

I have references to Arduino.h in my src directory. I’m assuming that is ok? Should I be using an option to exclude the src directory somehow when I’m debugging?

Thanks for your help!

Do you #include <HardwareStepper.h in one of the tests? Weird.

The unit test by itself runs okay? (pio test)

Yes, the tests work using pio test and I’ve got them working in Github Actions too.

Bear in mind I’m not an expert in C++ but I believe all my dependencies are clean and I don’t (directly or indirectly) reference hardware related code from my unit tests.

My first thought is to wonder how the build task I’ve set up knows not to build the src folder. Do I need a special build task?

I saw this thread but I’ve tried looking at articles linked from the solution.

The option that controls whether the src/ directory is built is by default set to no. (docs).

It’s also entirely possible that there is still a bug and the feature test was done without any content in src/. I’ll try and reproduce the issue – if it’s reproducable, that’s gonna be a bug report Issues · platformio/platformio-core · GitHub then.

Ah you just gave me a thought… I had tried removing all code from src directory but I got an error about having nothing in src so I gave up.

I’ve just retried deleting everything in src but I recreated a blank main.cpp with empty setup and loop methods.

This now gets further giving me this:

Building in debug mode
Linking .pio\build\debug\program.exe
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/8.1.0/…/…/…/…/x86_64-w64-mingw32/lib/…/lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain’
collect2.exe: error: ld returned 1 exit status

I don’t see the program.exe in that folder so I’m guessing it did not complete the linking process.

Is there anything I can do to help with reproducing or anything I can try next?

I’ve just created a int main() method in the main.cpp and it builds and links.

I can set a breakpoint in that main method and it almost stops as expected. I’m guessing I’m going down the wrong path here though.

Do you mean that it works well now?

Does the test program in test/test_analyzer.cpp contain the main function, e.g. as in example? If it’s still not working as expected , please post the full platformio.ini and source code.

Yes, test/test_analyzer.cpp contains the main function. My repo is public here. This is a branch with the empty src directory with just a main.cpp with empty main, setup and loop methods.

Really appreciate your help with this!

platform.io

[platformio]
default_envs = debug

[env:nano]
platform = atmelavr
board = nanoatmega328new
framework = arduino
monitor_filters = time, default
monitor_speed = 9600
lib_deps =
waspinator/AccelStepper @ ^1.61

[env:native]
platform = native

[env:debug]
platform = native
build_type = debug
debug_test = test_analyzer

test/test_analyzer.cpp (truncated)

#include <unity.h>
#include “Analyzer.h”

Analyzer analyzer;
Summary summary;

void setUp(void)
{
analyzer.setSoundDurationThreshold(40);
analyzer.setSilenceDurationThreshold(350, 800);
analyzer.clear();
}

void test_no_rhythmic_sound_detected_empty(void)
{
analyzer.analyze(&summary);
TEST_ASSERT_FALSE(summary.RhythmDetected);
}

int main(int argc, char **argv)
{
UNITY_BEGIN();
RUN_TEST(test_no_rhythmic_sound_detected_empty);
UNITY_END();
return 0;
}

Hi @ivankravets, sorry, no it’s not working as expected.

Expected behavior
Build code in the lib directory and the specific test_xyz.cpp and use the main method in there.

Actual behavior
It builds the code in the src directory and tries to run the main method I’ve defined in there (which I just did for testing purposes).

Is there anything I can try?

Could you try test_build_project_src?

Hi @ivankravets, @maxgerhardt, many thanks for your help. I’ve worked out what my problem was and have created a cut-down, basic example with this working in this repo.

I had made an assumption that the unit test for the classes in lib would not build the code in the src directory. The issue was that my debug environment in platformio.ini did not include the lib_deps section that I had defined in the other environments (native, nano etc). Once I added this, everything built and I can set a breakpoint in tests defined in debug_test.

Just wanted to add that PlatformIO is incredible - keep up the good work!