Unity Testing Not working *unless* I attach the debugger?

Hello, I’ve been struggling with the testing framework on my ESP32-S3 board. I’m connected to it over the JTAG/Serial USB port and normally serial and debugging work fine. I added a test folder to my project, and added the following to it:

#include "unity.h"
#include "CAN.h"
#include <unity.h>
#include <stdio.h>

void setUp(void)
{
  // set stuff up here
}

void tearDown(void)
{
  // clean stuff up here
}

void test_testing(void)
{
  TEST_ASSERT_EQUAL(0, give_zero());
}



extern "C" void app_main()
{
  printf("this is kinda getting absurd\n");
  UNITY_BEGIN();

  RUN_TEST(test_testing);

  UNITY_END();
}

when i hit the test button it builds and uploads, but all i get in the terminal is:
Building & Uploading... Testing... If you don't see any output for the first 10 secs, please reset board (press reset button)

Clicking my reset button (cycling the EN pin) just disconnects the device from my computer and the test fails.

It never seems to actually run the test code as my print statement doesn’t print. Strangely though, If I click on “pio debug (without uploading)” to attach to the JTAG while keeping the testing terminal open, it will break on the app_main() function in the test file above, and bam, the test will run and pass:

------------------------------------------------------------------------------------------------------------------------------------------Building & Uploading...
Testing...
If you don't see any output for the first 10 secs, please reset board (press reset button)

test/test_CAN.cpp:28: test_testing      [PASSED]
------------------------------------------- esp32-s3-devkitm-1:* [PASSED] Took 414.34 seconds ------------------------------------------- 

================================================================ SUMMARY ================================================================ 
Environment         Test    Status    Duration
------------------  ------  --------  ------------
esp32-s3-devkitm-1  *       PASSED    00:06:54.338
=============================================== 1 test cases: 1 succeeded in 00:06:54.338

which makes absolutely no sense to me other than that perhaps connecting the debugger triggers some kinda soft-reset that wasn’t being triggered otherwise and without that reset the test never runs properly. I’m wondering how I can fix this problem?

Even more interesting update:

I changed my main to:

extern "C" void app_main()
{
  while(true){
    printf("this is kinda getting absurd\n");
    UNITY_BEGIN();

    RUN_TEST(test_testing);

    UNITY_END();
  }
  
}

Just out of curiosity, maybe it was finishing the test before it had a chance to open the serial monitor and strangely enough not only does it work, but it only runs a few times before it seems to shut itself off?

Testing...
If you don't see any output for the first 10 secs, please reset board (press reset button)

test/test_CAN.cpp:29: test_testing      [PASSED]
test/test_CAN.cpp:29: test_testing      [PASSED]
test/test_CAN.cpp:29: test_testing      [PASSED]
test/test_CAN.cpp:29: test_testing      [PASSED]
test/test_CAN.cpp:29: test_testing      [PASSED]
test/test_CAN.cpp:29: test_testing      [PASSED]
test/test_CAN.cpp:29: test_testing      [PASSED]
test/test_CAN.cpp:29: test_testing      [PASSED]
test/test_CAN.cpp:29: test_testing      [PASSED]
test/test_CAN.cpp:29: test_testing      [PASSED]
test/test_CAN.cpp:29: test_testing      [PASSED]
test/test_CAN.cpp:29: test_testing      [PASSED]
test/test_CAN.cpp:29: test_testing      [PASSED]
-------------------------------------------- esp32-s3-devkitm-1:* [PASSED] Took 16.77 seconds --------------------------------------------
================================================================ SUMMARY ================================================================ 
Environment         Test    Status    Duration
------------------  ------  --------  ------------
esp32-s3-devkitm-1  *       PASSED    00:00:16.766
============================================== 13 test cases: 13 succeeded in 00:00:16.766 

Try adding a delay before UNITY_BEGIN(). It can just be that, by the time PlatformIO opens the serial monitor, the test output has already been produced with no one connected.

For ESP-IDF, I guess that’s

vTaskDelay(2000 / portTICK_PERIOD_MS);