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
Copy file name to clipboardExpand all lines: docs/debugging-testing/testing/unit_testing.md
+20-21Lines changed: 20 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -63,7 +63,7 @@ In a terminal window:
63
63
1. Download CMake binaries from https://cmake.org/download/, and run the installer.
64
64
1. Download Python 2.7 or Python 3 from https://www.python.org/getit/, and run the installer.
65
65
1. Add MinGW, CMake and Python into system PATH.
66
-
1. Install the addition Python package `gcovr` for code coverage report:
66
+
1. Install the additional Python package `gcovr` for code coverage report:
67
67
```
68
68
pip install "gcovr>=4.1"
69
69
```
@@ -78,7 +78,7 @@ All the stub source files are built in a stub CMake library target (e.g `mbed-st
78
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
79
80
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.
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` directory 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
82
83
83
All the stub libraries and header libraries are defined under `UNITTESTS/stubs/` directory.
84
84
@@ -94,7 +94,7 @@ For the build system to build a unit test, pass the `ON` value to `BUILD_TESTING
94
94
95
95
### Writing unit tests
96
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.
97
+
A unit test suite consists of one or more test cases, which should cover all the functions in a class under test. Any external dependencies should be 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
98
99
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
100
@@ -133,17 +133,17 @@ With the following steps, you can write a unit test. This example creates dummy
133
133
134
134
1. Add a new `mbed-headers-example` interface library:
135
135
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
-
)
136
+
**mbed-os/stubs/CMakeLists.txt**
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
+
)
147
147
```
148
148
149
149
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 +198,7 @@ With the following steps, you can write a unit test. This example creates dummy
198
198
199
199
```
200
200
201
-
### CMake input source file
201
+
**CMakeLists.txt**
202
202
203
203
```
204
204
add_library(mbed-stubs-example-interface)
@@ -268,7 +268,7 @@ With the following steps, you can write a unit test. This example creates dummy
268
268
}
269
269
```
270
270
271
-
### CMake input source file for UUT (Unit Under Test)
271
+
**CMakeLists.txt**
272
272
```
273
273
set(TEST_NAME myclass-unittest)
274
274
@@ -298,7 +298,7 @@ This example does not use any Mbed OS code. If your example depends on Mbed OS c
298
298
299
299
#### Mbed CLI 1
300
300
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.
301
+
- 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
302
303
303
- 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
304
@@ -327,10 +327,10 @@ Run a test binary in the build directory to run a unit test suite. To run multip
327
327
1. Create a build directory from Mbed OS root: `mkdir cmake_build`.
328
328
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
329
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`.
330
+
- `--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
331
332
332
- `--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.
333
+
- `--test-command [command]` to specify the command to invoke after the CMake build.
334
334
- See the [CTest manual](https://cmake.org/cmake/help/v3.19/manual/ctest.1.html) for more information.
335
335
336
336
#### Run tests with GUI test runner
@@ -366,5 +366,4 @@ Use Mbed CLI to generate code coverage reports. For advanced use, follow these s
366
366
- **Solution**: Remove sh.exe from the system path.
367
367
368
368
**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
-
369
+
- **Solution**: Make sure gnm (binutils) is not installed. Uninstall binutils with `brew uninstall binutils`.
0 commit comments