Skip to content

Commit 2c8b2b9

Browse files
committed
tests: minor fixes, add README.md
1 parent e712711 commit 2c8b2b9

File tree

4 files changed

+160
-5
lines changed

4 files changed

+160
-5
lines changed

tests/README.md

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# Testing Arduino ESP8266 Core
2+
3+
## Testing on host
4+
5+
Some features of this project can be tested by compiling and running the code on the PC, rather than running it on the ESP8266. Tests and testing infrastructure for such features is located in `tests/host` directory of the project.
6+
7+
Some hardware features, such as Flash memory and HardwareSerial, can be emulated on the PC. Others, such as network, WiFi, and other hardware (SPI, I2C, timers, etc) are not yet emulated. This limits the amount of features which can be tested on the host.
8+
9+
### Adding a test case
10+
11+
Tests are written in C++ using [Catch framework](https://github.com/catchorg/Catch2).
12+
13+
See .cpp files under tests/host/core/ for a few examples how to write test cases.
14+
15+
When adding new test files, update `TEST_CPP_FILES` variable in tests/host/Makefile to compile them.
16+
17+
If you want to add emulation of a certain feature, add it into tests/host/common/ directory.
18+
19+
20+
### Running test cases
21+
22+
To run test cases, go to tests/host/ directory and run `make`. This will compile and run the tests.
23+
24+
If all tests pass, you will see "All tests passed" message and the exit code will be 0.
25+
26+
Additionally, test coverage info will be generated using `gcov` tool. You can use some tool to analyze coverage information, for example `lcov`:
27+
28+
lcov -c -d . -d ../../cores/esp8266 -o test.info
29+
genhtml -o html test.info
30+
31+
This will generate an HTML report in `html` directory. Open html/index.html in your browser to see the report.
32+
33+
**Note to macOS users:** you will need to install GCC using Homebrew or MacPorts. Before running `make`, set `CC`, `CXX`, and `GCOV` variables to point to GCC tools you have installed. For example, when installing gcc-5 using Homebrew:
34+
35+
export CC=gcc-5
36+
export CXX=g++-5
37+
export GCOV=gcov-5
38+
39+
When running `lcov` (which you also need to install), specify `gcov` binary using `--gcov-tool $(which $GCOV)` (assuming you have already set `GCOV` environment variable).
40+
41+
## Testing on device
42+
43+
Most features and libraries of this project can not be tested on host. Therefore testing on an ESP8266 device is required. Such tests and the test infrastructure are located in tests/device directory of this project.
44+
45+
### Test cases
46+
47+
Tests are written in the form of Arduino sketches, and placed into tests/device/test_xxx directories. These tests are compiled using Arduino IDE, so test file name should match the name of the directory it is located in (e.g. test_foobar/test_foobar.ino). Tests use a very simple BSTest library, which handles test registration and provides `TEST_CASE`, `CHECK`, `REQUIRE`, and `FAIL` macros, similar to [Catch](https://github.com/catchorg/Catch2).
48+
49+
*Note: we should migrate to Catch framework with a custom runner.*
50+
51+
Here is a simple test case written with BSTest:
52+
53+
```c++
54+
#include <BSTest.h>
55+
#include <test_config.h>
56+
57+
BS_ENV_DECLARE();
58+
59+
void setup()
60+
{
61+
Serial.begin(115200);
62+
BS_RUN(Serial);
63+
}
64+
65+
66+
TEST_CASE("this test runs successfully", "[bs]")
67+
{
68+
CHECK(1 + 1 == 2);
69+
REQUIRE(2 * 2 == 4);
70+
}
71+
```
72+
73+
BSTest is a header-only library, so necessary static data is injected into the sketch using `BS_ENV_DECLARE();` macro.
74+
75+
`BS_RUN(Serial)` passes control to the test runner, which uses `Serial` stream to communicate with the host. If you need to do any preparation before starting tests, for example connect to an AP, do this before calling `BS_RUN`.
76+
77+
`TEST_CASE` macro defines a test case. First argument is human-readable test name, second contains optional set of tags (identifiers with square brackets). Currently only one tag has special meaning: `[.]` can be used to mark the test case as ignored. Such tests will not be skipped by the test runner (see below).
78+
79+
### Test execution
80+
81+
Once `BS_RUN` is called, BSTest library starts by printing the *menu*, i.e. the list of tests defined in the sketch. For example:
82+
83+
```
84+
>>>>>bs_test_menu_begin
85+
>>>>>bs_test_item id=1 name="this test runs successfully" desc="[bs]"
86+
>>>>>bs_test_menu_end
87+
```
88+
89+
Then it waits for the test index to be sent by the host, followed by newline.
90+
91+
Once the line number is received, the test is executed, and feedback is printed:
92+
```
93+
>>>>>bs_test_start file="arduino-esp8266/tests/device/test_tests/test_tests.ino" line=13 name="this test runs successfully" desc="[bs]"
94+
>>>>>bs_test_end line=0 result=1 checks=2 failed_checks=0
95+
```
96+
97+
Or, in case the test fails:
98+
```
99+
>>>>>bs_test_start file="arduino-esp8266/tests/device/test_tests/test_tests.ino" line=19 name="another test which fails" desc="[bs][fail]"
100+
>>>>>bs_test_check_failure line=22
101+
>>>>>bs_test_check_failure line=24
102+
>>>>>bs_test_end line=0 result=0 checks=4 failed_checks=2
103+
```
104+
105+
BSTest library also contains a Python script which can "talk" to the ESP8266 board and run the tests, tests/device/libraries/BSTest/runner.py. Normally it is not necessary to use this script directly, as the top level Makefile in tests/device/ directory can call it automatically (see below).
106+
107+
### Test configuration
108+
109+
Some tests need to connect to WiFi AP or to the PC running the tests. This configuration should be set in tests/device/libraries/test_config/test_config.h. This file is not tracked in Git. Create it by copying from tests/device/libraries/test_config/test_config.h.template, and modifying settings in that file.
110+
111+
### Building and running the tests
112+
113+
Makefile in tests/device/ directory handles compiling, uploading, and executing test cases.
114+
115+
Here are some of the supported targets:
116+
117+
- `virtualenv`: prepares Python virtual environment inside tests/device/libaries/BSTest/virtualenv/. This has to be run once on each computer where tests are to be run. This target will use `pip` to install several Python libraries required by the test runner (see tests/device/libaries/BSTest/requirements.txt).
118+
119+
- `test_xxx/test_xxx.ino`: compiles, uploads, and runs the tests defined in `test_xxx/test_xxx.ino` sketch. Some extra options are available, these can be passed as additional arguments to `make`:
120+
- `NO_BUILD=1`: don't compile the test.
121+
- `NO_UPLOAD=1`: don't upload the test.
122+
- `NO_RUN=1`: don't run the test.
123+
- `V=1`: enable verbose output from compilation, upload, and test runner.
124+
125+
For example, `make test_newlib/test_newlib.ino V=1` will compile, upload, and run all tests defined in `test_newlib/test_newlib.ino`.
126+
127+
For each test sketch, test results are stored in `tests/device/.build/test_xxx.ino/test_result.xml`. This file is an xUnit XML file, and can be read by a variety of tools, such as Jenkins.
128+
129+
- `test_report`: Generate HTML test report from xUnit XML files produced by test runs.
130+
131+
- `all` (or just `make` without a target): Run tests from all the .ino files, and generate HTML test report.
132+
133+
### Host-side helpers
134+
135+
Some tests running on the device need a matching part running on the host. For example, HTTP client test might need a web server running on the host to connect to. TCP server test might need to be connected to by TCP client running on the host. To support such use cases, for each test file, an optional Python test file can be provided. This Python file defines setup and teardown functions which have to be run before and after the test is run on the device. `setup` and `teardown` decorators bind setup/teardown functions to the test with specified name:
136+
137+
```python
138+
from mock_decorators import setup, teardown
139+
140+
@setup('WiFiClient test')
141+
def setup_wificlient_test(e):
142+
# create a TCP server
143+
144+
@teardown('WiFiClient test')
145+
def teardown_wificlient_test(e):
146+
# delete TCP server
147+
```
148+
149+

tests/device/Makefile

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ UPLOAD_BAUD ?= 921600
1010
UPLOAD_BOARD ?= nodemcu
1111
BS_DIR ?= libraries/BSTest
1212
DEBUG_LEVEL ?= DebugLevel=None____
13-
FQBN ?= esp8266com:esp8266:generic:CpuFrequency=80,FlashFreq=40,FlashMode=DIO,UploadSpeed=115200,FlashSize=4M1M,ResetMethod=none,Debug=Serial,$(DEBUG_LEVEL)
14-
BUILD_TOOL = $(ARDUINO_IDE_PATH)/arduino-builder
15-
TEST_CONFIG = libraries/test_config/test_config.h
13+
FQBN ?= esp8266com:esp8266:generic:CpuFrequency=80,FlashFreq=40,FlashMode=dio,UploadSpeed=115200,FlashSize=4M1M,LwIPVariant=v2mss536,ResetMethod=none,Debug=Serial,$(DEBUG_LEVEL)
14+
BUILD_TOOL := $(ARDUINO_IDE_PATH)/arduino-builder
15+
TEST_CONFIG := libraries/test_config/test_config.h
16+
TEST_REPORT_XML := test_report.xml
17+
TEST_REPORT_HTML := test_report.html
1618

1719
ifeq ("$(UPLOAD_PORT)","")
1820
$(error "Failed to detect upload port, please export UPLOAD_PORT manually")
@@ -57,6 +59,7 @@ ifneq ("$(NO_BUILD)","1")
5759
$@
5860
endif
5961
ifneq ("$(NO_UPLOAD)","1")
62+
@echo Uploading binary
6063
$(SILENT)$(ESPTOOL) $(UPLOAD_VERBOSE_FLAG) \
6164
-cp $(UPLOAD_PORT) \
6265
-cb $(UPLOAD_BAUD) \
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#pragma once
22

3+
// Tests which use STA mode will connect to this AP:
34
#define STA_SSID "test_wifi"
45
#define STA_PASS "test_wifi_pass"
56

7+
// Tests which use AP mode will set up a SoftAP with these parameters:
68
#define AP_SSID "test_wifi_ap"
79
#define AP_PASS "test_wifi_ap_pass"
810

11+
// IP address of the PC running the tests (needed for HTTP client and HTTP server tests)
912
#define SERVER_IP "192.168.10.1"

tests/host/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ CORE_PATH := ../../cores/esp8266
44

55
# I wasn't able to build with clang when -coverage flag is enabled, forcing GCC on OS X
66
ifeq ($(shell uname -s),Darwin)
7-
CC := gcc
8-
CXX := g++
7+
CC ?= gcc
8+
CXX ?= g++
99
endif
1010
GCOV ?= gcov
1111

0 commit comments

Comments
 (0)