組み込みProgrammerのチラシの裏

ESP32 NodeMCU-32S Unit Test

| Comments

platformioを使って単体テスト環境まで構築する方法ができてきていたので、 構築するところまで進めていく。

前提

[ESP32 NodeMCU-32S Hello World]を終えていること。

環境

  • nodemcu-32s : 基板用の環境
  • native : PC上で完結する単体テスト向けの環境

フォルダ構成

以下のような単体テスト環境を構築していく。 platformioでは基本的にunityを使っているので、それに従う。 なお、単体テスト環境でテストするコードはlibフォルダに格納することを推奨とのこと。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
├── LICENSE
├── README.md
├── include
│   └── README
├── lib
│   └── README
├── platformio.ini
├── src
│   └── main.cpp
├── test
│   ├── README
│   ├── embedded
│   │   └── test_main.cpp
│   └── native
│       └── test_main.cpp
└── upload
    └── README
  • src : 製品コード
  • test/embedded : 基板に書き込んで行う単体テスト
  • test/native : PC上で完結する単体テスト
  • upload : native環境でuploadを成功させるため

構築手順

platformio.ini

1
2
3
4
5
6
7
8
9
10
11
12
13
14
diff --git a/platformio.ini b/platformio.ini
index 827a3e3..dd5b54b 100644
--- a/platformio.ini
+++ b/platformio.ini
@@ -13,3 +13,9 @@ platform = espressif32
 board = nodemcu-32s
 framework = arduino
 monitor_speed = 115200
+test_ignore = native
+
+[env:native]
+platform = native
+test_ignore = embedded
+target =

test_ignoreを使わなかった場合、

  • nodemcu-32s環境 : test/embedded, test/nativeを実行する
  • native環境 : test/embedded, test/nativeを実行する

となってしまう。 それぞれテストの実行を排他したいため、test_ignoreを使用している。

test/embedded

基板に書き込んで行う単体テストコードを書く。 ここでは単にGPIOに書いて読んでいる。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <unity.h>
#include <Arduino.h>

const int LED_PIN = 2;
const int LED_INTERVAL_MS = 500;

void setUp(void)
{
    // set stuff up here
    pinMode(LED_PIN, OUTPUT);
}

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

void test_led_high()
{
    digitalWrite(LED_PIN, HIGH);
    TEST_ASSERT_EQUAL(HIGH, digitalRead(LED_PIN));
    delay(LED_INTERVAL_MS);
}

void test_led_low()
{
    digitalWrite(LED_PIN, LOW);
    TEST_ASSERT_EQUAL(LOW, digitalRead(LED_PIN));
    delay(LED_INTERVAL_MS);
}

void setup()
{
    delay(2000);

    UNITY_BEGIN();

    RUN_TEST(test_led_high);
    RUN_TEST(test_led_low);

    UNITY_END();
}

void loop()
{
    // nothing to be done here.
}

test/native

PC上で完結する単体テストコードを書く。 ここでは単に0が0と等しいことを確認している。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <unity.h>
#include <iostream>

void test_example()
{
    TEST_ASSERT_EQUAL(0, 0);
}

int main(int argc, char **argv)
{
    std::cout << "Hello platformio UT" << std::endl;
    UNITY_BEGIN();
    RUN_TEST(test_example);
    return UNITY_END();
}

upload

uploadフォルダがない場合、native環境でuploadに失敗してしまう。 無視しても良いのだが、エラーメッセージが出ているので回避する。

1
2
$ mkdir upload
$ touch upload/.gitkeep

ちなみに、そのエラーメッセージは以下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ pio run --target upload -e native
Warning! Ignore unknown configuration option `target` in section [env:native]
Processing native (platform: native)
--------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
*** Do not know how to make File target `upload' (example/upload).  Stop.
============================ [FAILED] Took 0.45 seconds ========================

Environment    Status    Duration
-------------  --------  ------------
nodemcu-32s    IGNORED
native         FAILED    00:00:00.454
==================== 1 failed, 0 succeeded in 00:00:00.454 =====================

使い方

1
2
3
4
5
$ pio run --target upload                # upload nodemcu-32s and native
$ pio run --target upload -e nodemcu-32s # upload nodemcu-32s
$ pio test                               # test nodemcu-32s and native
$ pio test -e nodemcu-32s                # test nodemcu-32s
$ pio test -e native                     # test native

関連リンク

Comments