• Documentation
  • PlatformIO
  • v3.5.0
PlatformIO
v3.5.0
  • What is PlatformIO?

Getting Started

  • PlatformIO IDE
  • PlatformIO Core
  • PlatformIO Home
  • Tutorials and Examples
    • Tutorials
      • Get started with Unit Testing of “Blink” Project
        • Setting Up the Project
        • Project structure
        • Source files
        • Test results
      • Get started with STM32Cube HAL and Nucleo-F401RE: debugging and unit testing
      • Get started with Arduino and Nordic nRF52-DK: debugging and unit testing
    • Project Examples

Configuration

  • platformio.ini
  • Environment variables
  • Advanced Scripting

Instruments

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

PlatformIO Plus

  • PIO Account
  • PIO Remote™
  • PIO Unified Debugger
  • PIO Unit Testing

Integration

  • Cloud & Standalone IDE
  • Continuous Integration

Miscellaneous

  • Articles about us
  • FAQ
  • Migrating from 2.x to 3.0

Get started with Unit Testing of “Blink” Project¶

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

  • Level: Beginner

  • Platforms: Windows, Mac OS X, Linux

Contents

  • Setting Up the Project

  • Project structure

  • Source files

  • Test results

Setting Up the Project¶

  1. Please navigate to Quick Start section and create the “Blink Project”. According to the Unit Testing Design it is the Main program.

  2. Create a test directory in the project (on the same level as src) and place test_main.cpp file in it (the source code is located below).

  3. Wrap setup() and loop() methods of the main program in a UNIT_TEST guard.

  4. Run tests using platformio test command.

Project structure¶

project_dir
├── lib
│   └── readme.txt
├── platformio.ini
├── src
│   └── main.cpp
└── test
    └── test_main.cpp

Source files¶

  • platformio.ini

    ; PlatformIO Project Configuration File
    ;
    ;   Build options: build flags, source filter, extra scripting
    ;   Upload options: custom port, speed and extra flags
    ;   Library options: dependencies, extra library storages
    ;
    ; Please visit documentation for the other options and examples
    ; http://docs.platformio.org/page/projectconf.html
    
    
    [env:uno]
    platform = atmelavr
    framework = arduino
    board = uno
    
    [env:nodemcu]
    platform = espressif8266
    framework = arduino
    board = nodemcu
    
    [env:teensy31]
    platform = teensy
    framework = arduino
    board = teensy31
    
  • src/main.cpp

    /*
     * Blink
     * Turns on an LED on for one second,
     * then off for one second, repeatedly.
     */
    
    #include "Arduino.h"
    
    #ifndef UNIT_TEST  // IMPORTANT LINE!
    
    void setup()
    {
      // initialize LED digital pin as an output.
      pinMode(LED_BUILTIN, OUTPUT);
    }
    
    void loop()
    {
      // turn the LED on (HIGH is the voltage level)
      digitalWrite(LED_BUILTIN, HIGH);
      // wait for a second
      delay(1000);
      // turn the LED off by making the voltage LOW
      digitalWrite(LED_BUILTIN, LOW);
       // wait for a second
      delay(1000);
    }
    
    #endif    // IMPORTANT LINE!
    
  • test/test_main.cpp

    #include <Arduino.h>
    #include <unity.h>
    
    #ifdef UNIT_TEST
    
    // void setUp(void) {
    // // set stuff up here
    // }
    
    // void tearDown(void) {
    // // clean stuff up here
    // }
    
    void test_led_builtin_pin_number(void) {
        TEST_ASSERT_EQUAL(LED_BUILTIN, 13);
    }
    
    void test_led_state_high(void) {
        digitalWrite(LED_BUILTIN, HIGH);
        TEST_ASSERT_EQUAL(digitalRead(LED_BUILTIN), HIGH);
    }
    
    void test_led_state_low(void) {
        digitalWrite(LED_BUILTIN, LOW);
        TEST_ASSERT_EQUAL(digitalRead(LED_BUILTIN), LOW);
    }
    
    void setup() {
        // NOTE!!! Wait for >2 secs
        // if board doesn't support software reset via Serial.DTR/RTS
        delay(2000);
    
        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
        }
    }
    
    #endif
    

Test results¶

> platformio test -e nodemcu --verbose

PlatformIO Plus (https://pioplus.com) v0.1.0
Verbose mode can be enabled via `-v, --verbose` option
Collected 1 items

============================== [test::*] Building... (1/3) ==============================
[Wed Sep  7 15:16:55 2016] Processing nodemcu (platform: espressif8266, board: nodemcu, framework: arduino)
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
Collected 34 compatible libraries
Looking for dependencies...
Project does not have dependencies
Compiling .pioenvs/nodemcu/src/main.o
Compiling .pioenvs/nodemcu/test/output_export.o
Compiling .pioenvs/nodemcu/test/test_main.o
Compiling .pioenvs/nodemcu/UnityTestLib/unity.o
Archiving .pioenvs/nodemcu/libFrameworkArduinoVariant.a
Indexing .pioenvs/nodemcu/libFrameworkArduinoVariant.a
Compiling .pioenvs/nodemcu/FrameworkArduino/Esp.o
Compiling .pioenvs/nodemcu/FrameworkArduino/FS.o
Compiling .pioenvs/nodemcu/FrameworkArduino/HardwareSerial.o
Compiling .pioenvs/nodemcu/FrameworkArduino/IPAddress.o
Archiving .pioenvs/nodemcu/libUnityTestLib.a
Indexing .pioenvs/nodemcu/libUnityTestLib.a
Compiling .pioenvs/nodemcu/FrameworkArduino/MD5Builder.o
...
Compiling .pioenvs/nodemcu/FrameworkArduino/umm_malloc/umm_malloc.o
Archiving .pioenvs/nodemcu/libFrameworkArduino.a
Indexing .pioenvs/nodemcu/libFrameworkArduino.a
Linking .pioenvs/nodemcu/firmware.elf
Calculating size .pioenvs/nodemcu/firmware.elf
text       data     bss     dec     hex filename
223500     2408   29536  255444   3e5d4 .pioenvs/nodemcu/firmware.elf
Building .pioenvs/nodemcu/firmware.bin

============================== [test::*] Uploading... (2/3) ==============================
[Wed Sep  7 15:17:01 2016] Processing nodemcu (platform: espressif8266, board: nodemcu, framework: arduino)
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
Collected 34 compatible libraries
Looking for dependencies...
Project does not have dependencies
Linking .pioenvs/nodemcu/firmware.elf
Checking program size .pioenvs/nodemcu/firmware.elf
text       data     bss     dec     hex filename
223500     2408   29536  255444   3e5d4 .pioenvs/nodemcu/firmware.elf
Calculating size .pioenvs/nodemcu/firmware.elf
text       data     bss     dec     hex filename
223500     2408   29536  255444   3e5d4 .pioenvs/nodemcu/firmware.elf
Looking for upload port...
Auto-detected: /dev/cu.SLAB_USBtoUART
Uploading .pioenvs/nodemcu/firmware.bin
Uploading 230064 bytes from .pioenvs/nodemcu/firmware.bin to flash at 0x00000000
................................................................................ [ 35% ]
................................................................................ [ 71% ]
.................................................................                [ 100% ]

=============================== [test::*] Testing... (3/3) ===============================
If you don't see any output for the first 10 secs, please reset board (press reset button)

test/test_main.cpp:41:test_led_state_high       [PASSED]
test/test_main.cpp:43:test_led_state_low        [PASSED]
test/test_main.cpp:41:test_led_state_high       [PASSED]
test/test_main.cpp:43:test_led_state_low        [PASSED]
test/test_main.cpp:41:test_led_state_high       [PASSED]
test/test_main.cpp:43:test_led_state_low        [PASSED]
test/test_main.cpp:41:test_led_state_high       [PASSED]
test/test_main.cpp:43:test_led_state_low        [PASSED]
-----------------------
11 Tests 1 Failures 0 Ignored

===================================== [TEST SUMMARY] =====================================
test:*/env:nodemcu      [PASSED]
================================ [PASSED] Took 38.15 seconds ================================