Skip to content

Unit testing with unity fails on Windows because of Serial.end #5359

Closed

Description

@aaronfultonnz

I've been using the Unity framework for testing code for the RP2040 MCU on Arduino, however I have run into an issue with tests completing cleanly. I've narrowed this down to the call to Serial.end() in void unityOutputComplete(void) { Serial.end(); } [platformio/test/runners/unity.py] which then gets compiled into unity_config.cpp

This causes:

  • Final test results to not always come through
  • A permission denied error (due to closed serial port)
  • Prevents the MCU from software reboots (because the serial is not running), and thus requires manual intervention to run subsequent tests.

I want to know if Serial.end() is necessary here at all? Is there any downside to leaving the Serial running at the end of the test (for Arudino this will sit in loop(), which is normal behavior).

Activity

  1. maxgerhardt commented on Jan 18, 2026

    @maxgerhardt
    Contributor

    Just FYI, the default Arduino implementation of the Unity handlers are in

    #include <Arduino.h>
    void unityOutputStart(unsigned long baudrate) { Serial.begin(baudrate); }
    void unityOutputChar(unsigned int c) { Serial.write(c); }
    void unityOutputFlush(void) { Serial.flush(); }
    void unityOutputComplete(void) { Serial.end(); }

    And you can always supply your own ones that don't do this:

    https://docs.platformio.org/en/latest/advanced/unit-testing/frameworks/unity.html#custom-unity-config-h

    https://github.com/platformio/platformio-examples/tree/develop/unit-testing/stm32cube/test

  2. dzid26 commented on Apr 13, 2026

    @dzid26

    Similar with ESP32-C6 with Arduino HWCDC (ARDUINO_USB_CDC_ON_BOOT=1).

    Serial.end() in the generated unity_config.cpp calls HWCDC::end() → HWCDC::deinit(), which pulls USB D+/D- low and fully de-enumerates the device. After the test finishes the board is gone from /dev/ttyACM*, and even after a reset the bootloader's USB CDC stays up for only ~2 seconds - which makes re-flashing more difficult.

  3. added this to the 6.1.20 milestone on Apr 14, 2026
  4. self-assigned this
    on Apr 14, 2026
  5. added a commit that references this issue on Apr 14, 2026
    4d4fb00
  6. ivankravets commented on Apr 14, 2026

    @ivankravets
    Member

    Thanks for the report. Please re-test with pio upgrade --dev.

  7. aaronfultonnz commented on Apr 21, 2026

    @aaronfultonnz
    Author

    Thanks for the fix! I can confirm that this works a treat.

  8. added a commit that references this issue on Sep 15, 2026
    c80dad5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions