You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -6,107 +6,6 @@ This document describes how to write and test unit tests for Mbed OS. To prevent
6
6
7
7
Unit tests exercise code in small functional "units", such as a single function or a class. The unit tests are designed to be run on your host development machine and should not have any dependency on the underlying platform or external libraries. To achieve this, unit tests make extensive use of "stubs", "mocks" and "fakes" to isolate the code under test from any external dependencies. Unit tests are quick to run and provide targeted testing of your code during development. Mbed OS provides mock and stub implementations of the Mbed OS components to make unit testing easier.
8
8
9
-
## Prerequisites
10
-
11
-
Please install the following dependencies to use Mbed OS unit testing.
12
-
13
-
- GNU toolchains.
14
-
- GCC 6 or later. We recommend you use MinGW-W64 on Windows, but any Windows port of the above GCC versions works.
15
-
- CMake 3.19.0 or newer.
16
-
- Python 2.7.x, 3.5 or newer.
17
-
- Pip 10.0 or newer.
18
-
- Gcovr 4.1 or newer.
19
-
20
-
Detailed instructions for supported operating systems are below.
1. Install the additional Python package `gcovr` for code coverage report:
32
-
```
33
-
pip install "gcovr>=4.1"
34
-
```
35
-
36
-
### Installing dependencies on macOS
37
-
38
-
In a terminal window:
39
-
40
-
1. Install [Homebrew](https://brew.sh/).
41
-
1. Install Xcode command-line tools with `xcode-select --install`.
42
-
1. Install CMake with: `brew install cmake`.
43
-
1. Install Python and Pip:
44
-
45
-
```
46
-
brew install python
47
-
```
48
-
49
-
1. Install the additional Python package `gcovr` for code coverage report:
50
-
```
51
-
pip install "gcovr>=4.1"
52
-
```
53
-
1. Install the GNU Compiler collection if required:
54
-
```
55
-
brew install gcc
56
-
```
57
-
58
-
### Installing dependencies on Windows
59
-
60
-
In a terminal window:
61
-
62
-
1. Download and install MinGW-W64 from [SourceForge](https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/).
63
-
1. Download CMake binaries from https://cmake.org/download/, and run the installer.
64
-
1. Download Python 2.7 or Python 3 from https://www.python.org/getit/, and run the installer.
65
-
1. Add MinGW, CMake and Python into system PATH.
66
-
1. Install the addition Python package `gcovr` for code coverage report:
67
-
```
68
-
pip install "gcovr>=4.1"
69
-
```
70
-
71
-
## Test code structure
72
-
73
-
Unit tests are located in the `tests/UNITTESTS` subdirectory of each library. We recommend that unit test files use an identical directory path as the file under test. This makes it easier to find unit tests for a particular class or a module. For example, if the file you are testing is `some/example/path/ClassName.cpp`, then all the test files are in the `UNITTESTS/some/example/path/ClassName` directory. Each test suite needs to have its own `CMakeLists.txt` file for test CMake configuration.
74
-
75
-
All the stub source files are built in a stub CMake library target (e.g `mbed-stubs-rtos`) and linked to the `mbed-stubs` CMake target. The CMake target of the library unit under test is expected to link with the required stub libraries or `mbed-stubs` if it requires multiple stub libraries.
76
-
77
-
78
-
The new stub file should follow the naming convention `<CLASS_NAME>_stub.cpp` for the source file and `<CLASS_NAME>_stub.h` for the header file. They should be built as part of their respective stub CMake library. Alternatively, create a stub library if `<CLASS_NAME>_stub` is an implementation of an external source, not part of Mbed OS.
79
-
80
-
81
-
All the Mbed OS header files are built with CMake `INTERFACE` libraries (e.g `mbed-headers-platform`). Stubbed header files reside in the `UNITTESTS/target_h` and are built with the `mbed-headers-base` CMake library. All CMake libraries containing header files are linked with `mbed-headers`. The CMake target of the library unit under test is expected to link with the required header file libraries or `mbed-headers` in case of requiring multiple header libraries.
82
-
83
-
All the stub libraries and header libraries are defined under `UNITTESTS/stubs/` directory.
84
-
85
-
Libraries for fakes are under the `UNITTESTS/fakes` directory. These provide mock implementations that are meant to replace the stub version that does nothing. Usually, these will replace the header files as well as the source files and cannot be used together with their stub equivalents.
86
-
87
-
The upcoming [Example](#Example) section describes how to write a unit test for a dummy example class (Unit Under Test). The example walks you through creating a header library and a stub interface library using CMake.
88
-
89
-
### Test discovery
90
-
91
-
Every unit test suite CMake calls `add_test()` which adds a test to the project at CMake configuration time and to run the unit tests happens automatically by invoking the `ctest` command. However, test suites do not automatically build.
92
-
93
-
For the build system to build a unit test, pass the `ON` value to `BUILD_TESTING` in the CMake or CTest command-line argument
94
-
95
-
### Writing unit tests
96
-
97
-
A unit tests suite consists of one or more test cases. The test cases should cover all the functions in a class under test. All the external dependencies are stubbed including the other classes in the same module. Avoid stubbing header files. Finally, analyze code coverage to ensure all code is tested, and no dead code is found.
98
-
99
-
Please see the [documentation for Google Test](https://github.com/google/googletest/blob/master/docs/primer.md) to learn how to write unit tests using its framework. See the [documentation for Google Mock](https://github.com/google/googletest/tree/master/googlemock) if you want to write and use C++ mock classes instead of stubs.
100
-
101
-
#### Test suite configuration
102
-
103
-
Create two files in the test directory for each test suite:
104
-
105
-
- Unit test source file (`test_ClassName.cpp`).
106
-
- Unit test CMake configuration file (`CMakeLists.txt`).
107
-
108
-
List all the required files and libraries for the build in the `CMakeLists.txt` file.
109
-
110
9
#### Example
111
10
112
11
With the following steps, you can write a unit test. This example creates dummy classes to be tested, creates and configures unit tests for a class and stubs all external dependencies.
@@ -133,17 +32,17 @@ With the following steps, you can write a unit test. This example creates dummy
133
32
134
33
1. Add a new `mbed-headers-example` interface library:
135
34
136
-
### Stub CMake input source file
137
-
138
-
```
139
-
add_library(mbed-headers-example INTERFACE)
140
-
141
-
target_include_directories(mbed-headers-example
142
-
INTERFACE
143
-
${mbed-os_SOURCE_DIR}/example
144
-
${mbed-os_SOURCE_DIR}/example/include
145
-
${mbed-os_SOURCE_DIR}/example/include/example
146
-
)
35
+
**mbed-os/stubs/CMakeLists.txt**
36
+
37
+
```
38
+
add_library(mbed-headers-example INTERFACE)
39
+
40
+
target_include_directories(mbed-headers-example
41
+
INTERFACE
42
+
${mbed-os_SOURCE_DIR}/example
43
+
${mbed-os_SOURCE_DIR}/example/include
44
+
${mbed-os_SOURCE_DIR}/example/include/example
45
+
)
147
46
```
148
47
149
48
1. Add the newly created `mbed-headers-example` library into existing `target_link_libraries` of target `mbed-headers` in `mbed-os/UNITTESTS/stubs/CMakeLists.txt`:
@@ -198,7 +97,7 @@ With the following steps, you can write a unit test. This example creates dummy
198
97
199
98
```
200
99
201
-
### CMake input source file
100
+
**CMakeLists.txt**
202
101
203
102
```
204
103
add_library(mbed-stubs-example-interface)
@@ -268,7 +167,7 @@ With the following steps, you can write a unit test. This example creates dummy
268
167
}
269
168
```
270
169
271
-
### CMake input source file for UUT (Unit Under Test)
170
+
**CMakeLists.txt**
272
171
```
273
172
set(TEST_NAME myclass-unittest)
274
173
@@ -298,7 +197,7 @@ This example does not use any Mbed OS code. If your example depends on Mbed OS c
298
197
299
198
#### Mbed CLI 1
300
199
301
-
- Install [Mbed CLI 1](../build-tools/mbed-cli-1.html) using the `pip install mbed-cli` command in Debian, Ubuntu, macOS or Windows and make sure that installed Mbed CLI 1 version 1.8.0 or newer.
200
+
- Install [Mbed CLI 1](../build-tools/mbed-cli-1.html) version 1.8.0 or newer using the `pip install "mbed-cli>=1.8.0"` command in Debian, Ubuntu, macOS or Windows.
302
201
303
202
- Mbed CLI 1 supports unit tests through the `mbed test --unittests` command. To learn how to use unit tests with Mbed CLI 1, please see the [unit testing documentation section](../build-tools/test-and-debug.html). For other information on using Mbed CLI 1, please see the [CLI documentation in handbook](../build-tools/mbed-cli-1.html).
304
203
@@ -327,10 +226,10 @@ Run a test binary in the build directory to run a unit test suite. To run multip
327
226
1. Create a build directory from Mbed OS root: `mkdir cmake_build`.
328
227
1. Run CTest using a relative path to the Mbed OS root directory and created build directory `cmake_build` as an argument. From Mbed OS root use `ctest --build-and-test . cmake_build --build-generator Ninja --build-options -DBUILD_TESTING=ON --test-command ctest`.
329
228
330
-
- `--build-generator [generator]` to specify the [build generator](https://cmake.org/cmake/help/v3.19/manual/cmake-generators.7.html#cmake-generators) and above reference command uses `Ninja`.
229
+
- `--build-generator [generator]` specifies the [build generator](https://cmake.org/cmake/help/v3.19/manual/cmake-generators.7.html#cmake-generators), the example above uses `Ninja`.
331
230
332
231
- `--build-options [options]` to specify for Mbed OS unit tests CMake configuration, not for the build tool (e.g --build-options -DBUILD_TESTING=ON)
333
-
- `--test-command [command]` to specify the command get invoked after the CMake build.
232
+
- `--test-command [command]` to specify the command to invoke after the CMake build.
334
233
- See the [CTest manual](https://cmake.org/cmake/help/v3.19/manual/ctest.1.html) for more information.
335
234
336
235
#### Run tests with GUI test runner
@@ -366,5 +265,4 @@ Use Mbed CLI to generate code coverage reports. For advanced use, follow these s
366
265
- **Solution**: Remove sh.exe from the system path.
367
266
368
267
**Problem:** (Mac OS) CMake compiler check fails on Mac OS Mojave when using GCC-8.
369
-
- **Solution**: Make sure gnm (binutils) is not installed. Uninstall binutils with `brew uninstall binutils`.
370
-
268
+
- **Solution**: Make sure gnm (binutils) is not installed. Uninstall binutils with `brew uninstall binutils`.
0 commit comments