Teensy 4.1 Unit Testing Issue

I am trying to run a demo unit test on the teensy 4.1. The unit test stalls after upload with the following message:

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

The platformio.ini file is configured as:

[env:teensy41]
platform = teensy
board = teensy41
framework = arduino
upload_protocol = teensy-cli

The test file is taken from one of the unit test demos, not doing anything fancy:

#include <Arduino.h>
#include <unity.h>

void test_led_builtin_pin_number(void) {
    TEST_ASSERT_EQUAL(13, LED_BUILTIN);
}

void test_led_state_high(void) {
    digitalWrite(LED_BUILTIN, HIGH);
    TEST_ASSERT_EQUAL(HIGH, digitalRead(LED_BUILTIN));
}

void test_led_state_low(void) {
    digitalWrite(LED_BUILTIN, LOW);
    TEST_ASSERT_EQUAL(LOW, digitalRead(LED_BUILTIN));
}

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

    UNITY_BEGIN();    // IMPORTANT LINE!
    RUN_TEST(test_led_builtin_pin_number);

    pinMode(LED_BUILTIN, OUTPUT);
}

uint8_t i = 0;
uint8_t max_blinks = 5;

void loop() {
    if (i < max_blinks)
    {
        RUN_TEST(test_led_state_high);
        delay(500);
        RUN_TEST(test_led_state_low);
        delay(500);
        i++;
    }
    else if (i >= max_blinks) {
      UNITY_END(); // stop unit testing
    }
}

The program successfully uploads, and the board blinks N times as expected but does not print out any test results. If I open the serial monitor while the program is running I can see testing outputs flash repeatedly:

-----------------------
11 Tests 0 Failures 0 Ignored
OK

Any suggestions? I have tried on both a Windows and Linux machine with the same results. Ran the same tests on the esp32 with success.

pio core version is 5.2.0a6
teensy version is 4.12.0

What happens if you restructure setup() and loop() to

uint8_t i = 0;
uint8_t max_blinks = 5;

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

    UNITY_BEGIN();    // IMPORTANT LINE!
    RUN_TEST(test_led_builtin_pin_number);

    pinMode(LED_BUILTIN, OUTPUT);

    for(i = 0; i < max_blinks; i++)
    {
        RUN_TEST(test_led_state_high);
        delay(500);
        RUN_TEST(test_led_state_low);
        delay(500);
    }
    UNITY_END(); // stop unit testing
}

void loop() {}

?

Same thing unfortunately.

I also tried manually setting the test_port to the port of the device. This results in a ā€œcould not open portā€ error

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

could not open port 'COM5': FileNotFoundError(2, 'The system cannot find the file specified.', None, 2)
    ============================================================================================ [FAILED] Took 3.85 seconds ============================================================================================
    Test    Environment    Status    Duration
    ------  -------------  --------  ------------
    *       teensy41       FAILED    00:00:03.854
    ====================================================================================== 1 failed, 0 succeeded in 00:00:03.854 ====================

Hm yes I can reproduce this with my Teensy 4.0 (that I can also treat as a 4.1 with identical config as yoursā€¦)

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

the problem is that after the firmware is uploaded, the MCU reboots and it then initializes its USB peripheral to act as a USB serial device, then Windows has to recognize it and initialize it etc. This takes time, but the Testing.., aka looking at the serial port output, seems to be happening too early. So basically itā€™s the issue as known in Add test start delay option Ā· Issue #3742 Ā· platformio/platformio-core Ā· GitHub.

Boards with a fixed USB-serial adapter chip donā€™t have this problem.

Solution per SigFox MKR1200 Unit Testing - If you don't see any output for the first 10 secs, please reset board - #4 by isa56k works for me.

Aka, when I do

[env:teensy41]
platform = teensy
board = teensy41
framework = arduino
upload_protocol = teensy-cli
extra_scripts = extra_script.py

with extra_script.py in the root as

Import("env")

def after_upload(source, target, env):
    print("Delay while uploading...")
    import time
    time.sleep(5)
    print("Done!")

env.AddPostAction("upload", after_upload)

and increasing the delay(3000); to delay(5000); in the firmware, I getā€¦

> Executing task in folder pico_test_2: C:\Users\Max\AppData\Local\Programs\Python\Python38\Scripts\platformio.exe test --environment teensy41 <

Verbose mode can be enabled via `-v, --verbose` option
Collected 1 items

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

test\test1.cpp:24:test_led_builtin_pin_number   [PASSED]
test\test1.cpp:35:test_led_state_high   [PASSED]        
test\test1.cpp:37:test_led_state_low    [PASSED]
test\test1.cpp:35:test_led_state_high   [PASSED]
test\test1.cpp:37:test_led_state_low    [PASSED]
test\test1.cpp:35:test_led_state_high   [PASSED]
test\test1.cpp:37:test_led_state_low    [PASSED]
test\test1.cpp:35:test_led_state_high   [PASSED]
test\test1.cpp:37:test_led_state_low    [PASSED]
test\test1.cpp:35:test_led_state_high   [PASSED]
test\test1.cpp:37:test_led_state_low    [PASSED]
-----------------------
11 Tests 0 Failures 0 Ignored
========================= [PASSED] Took 16.96 seconds =========================

So itā€™s ā€˜justā€™ a timing issue that will hopefully get resolved in the linked issue.

2 Likes

The only additional change I had to make to your suggestion was to include test_port = COM5

Thanks so much for your help.

Iā€™ve run into this issue on a Teensy4.0. It happens on my desktop PC but not on my laptop. The difference is that my PC has an additional COM port. Presumably the test task tries to connect to this port when the test port disappears. If I use Device Manager to delete the extra port then it all works as expected.

The fix that @maxgerhardt works for me. (Thanks Max!) FWIW I found that the delay in extra_script.py could be much smaller; 0.35s on my PC.

cheers,
Richard

See the solution to this issue at