• Documentation
  • PlatformIO
  • v6.0.0
PlatformIO
v6.0.0
  • What is PlatformIO?

Getting Started

  • PlatformIO IDE
  • PlatformIO Core (CLI)
  • PlatformIO Home
  • PlatformIO Account
  • Tutorials and Examples
    • Official
      • Tutorials
        • Get started with Arduino and ESP32-DevKitC: debugging and unit testing
        • Get started with ESP-IDF and ESP32-DevKitC: debugging, unit testing, project analysis
        • STM32Cube HAL and Nucleo-F401RE: debugging and unit testing
        • Arduino and Nordic nRF52-DK: debugging and unit testing
        • Zephyr and Nordic nRF52-DK: debugging, unit testing, project analysis
        • Unit Testing of a “Blink” Project
        • RISC-V ASM Video Tutorial
      • Project Examples
    • Community

Configuration

  • platformio.ini
  • Build Configurations
  • Environment variables

Instruments

  • Library Management
  • Platforms
  • Frameworks
  • Boards
  • Custom Platform & Board

Advanced

  • Scripting
  • Debugging
  • Unit Testing
  • Static Code Analysis
  • Remote Development

Integration

  • Cloud & Desktop IDE
  • Continuous Integration
  • Compilation database compile_commands.json

Miscellaneous

  • FAQ
  • Migrating from 5.x to 6.0

Unit Testing of a “Blink” Project¶

The goal of this tutorial is to demonstrate how simple it is to use Unit Testing.

  • Level: Beginner

  • Platforms: Windows, macOS, Linux

Contents

  • Setting Up the Project

  • Project structure

  • Source files

  • Test results

Setting Up the Project¶

  1. Please navigate to the Quick Start section and create the “Blink Project”.

  2. Create the root test directory in the project (on the same level as src)

  3. Create a test test_blink directory (name must be prefixed with test_) and place a test_main.cpp file in it (the source code is located below).

  4. Run tests using the pio test command.

Project structure¶

project_dir
├── platformio.ini
└── test
    └── test_blink
        └── test_main.cpp

Source files¶

  • “platformio.ini” (Project Configuration File)

    [env:uno]
    platform = atmelavr
    framework = arduino
    board = uno
    
  • test/test_blink/test_main.cpp

    #include <Arduino.h>
    #include <unity.h>
    
    void setUp(void)
    {
      // set stuff up here
    }
    
    void tearDown(void)
    {
      // clean stuff up here
    }
    
    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(2000);
    
      pinMode(LED_BUILTIN, OUTPUT);
    
      UNITY_BEGIN(); // IMPORTANT LINE!
      RUN_TEST(test_led_builtin_pin_number);
    }
    
    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
      }
    }
    

Test results¶

> pio test

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

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

test/test_blink/test_main.cpp:34: test_led_builtin_pin_number [PASSED]
test/test_blink/test_main.cpp:43: test_led_state_high [PASSED]
test/test_blink/test_main.cpp:45: test_led_state_low  [PASSED]
test/test_blink/test_main.cpp:43: test_led_state_high [PASSED]
test/test_blink/test_main.cpp:45: test_led_state_low  [PASSED]
test/test_blink/test_main.cpp:43: test_led_state_high [PASSED]
test/test_blink/test_main.cpp:45: test_led_state_low  [PASSED]
test/test_blink/test_main.cpp:43: test_led_state_high [PASSED]
test/test_blink/test_main.cpp:45: test_led_state_low  [PASSED]
test/test_blink/test_main.cpp:43: test_led_state_high [PASSED]
test/test_blink/test_main.cpp:45: test_led_state_low  [PASSED]
----------------- uno:test_blink [PASSED] Took 16.51 seconds -----------------

Environment    Test        Status    Duration
-------------  ----------  --------  ------------
uno            test_blink  PASSED    00:00:16.514

=================== 11 test cases: 11 succeeded in 00:00:16.514 ===================