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: UNITTESTS/README.md
+26-8Lines changed: 26 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,7 +49,7 @@ Detailed instructions for supported operating systems are below.
49
49
50
50
#### Installing dependencies on Windows
51
51
52
-
1. Download and install [MinGW-W64](http://mingw-w64.org/).
52
+
1. Download and install MinGW-W64 from [SourceForge](https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/).
53
53
1. Download CMake binaries from https://cmake.org/download/, and run the installer.
54
54
1. Download Python 2.7 or Python 3 from https://www.python.org/getit/, and run the installer.
55
55
1. Add MinGW, CMake and Python into system PATH.
@@ -59,11 +59,13 @@ Detailed instructions for supported operating systems are below.
59
59
60
60
Unit tests are located in the Mbed OS repository under the `UNITTESTS` folder. We recommend unit test files use an identical directory path to the file under test. This makes it easier to find unit tests for a particular class or a module. For example, if the file under test 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 `unittest.cmake` file for test configuration.
61
61
62
+
All the class stubs should be located in the `UNITTESTS/stubs` directory. A single stub class can be used by multiple test suites and therefore should follow the naming convention of `ClassName_stub.cpp` for the source file and `ClassName_stub.h` for the header file. Use the actual header files for the unit tests and try not to stub headers if possible. The stubbed headers reside in the `UNITTESTS/target_h` directory.
63
+
62
64
#### Test discovery
63
65
64
66
Registering unit tests for running is automatic, and the test runner handles registration. However, test files are not automatically assigned to be built. We build unit tests by using a separate build system, which searches for unit tests under the `UNITTESTS` directory.
65
67
66
-
For the build system to find and build any test suite automatically, you must include a unit test configuration file named `unittest.cmake` for each unit test suite. This configuration file contains all the source files required for the build.
68
+
For the build system to find and build any test suite automatically, you must include a unit test configuration file named `unittest.cmake` for each unit test suite. This configuration file lists all the source files required for the test build.
67
69
68
70
#### Test names
69
71
@@ -75,17 +77,27 @@ Mbed CLI supports unit tests through `mbed test --unittests` command. For inform
75
77
76
78
### Writing unit tests
77
79
80
+
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 the code coverage to ensure all the code is tested and no dead code is found.
81
+
82
+
Please see the [documentation for Google Test](https://github.com/google/googletest/blob/master/googletest/docs/primer.md) to learn how to write unit tests using the framework. See the [documentation for Google Mock](https://github.com/google/googletest/blob/master/googlemock/docs/Documentation.md) if you want to write and use C++ mock classes instead of stubs.
83
+
84
+
#### Test suite configuration
85
+
78
86
Create two files in the test directory for each test suite:
79
87
80
88
- Unit test source file (`test_ClassName.cpp`).
81
89
- Unit test configuration file (`unittest.cmake`).
82
90
83
-
List all the files required for the build in the `unittest.cmake` file. We recommend you list the file paths relative to the `UNITTESTS` folder. Use the following variables to list the source files and include paths:
91
+
List all the files required for the build in the `unittest.cmake` file. You will need to list the file paths relative to the `UNITTESTS` folder. Use the following variables to list the source files and include paths:
84
92
85
-
-**unittest-includes** - List of header include paths. You can use this to extend or overwrite default paths listed in CMakeLists.txt.
93
+
-**unittest-includes** - List of header include paths. You can use this to extend or overwrite default paths listed in `UNITTESTS/CMakeLists.txt`.
86
94
-**unittest-sources** - List of files under test.
87
95
-**unittest-test-sources** - List of test sources and stubs.
88
96
97
+
You can also set custom compiler flags and other configurations supported by CMake in `unittest.cmake`.
98
+
99
+
#### Example
100
+
89
101
With the following steps, you can write a simple unit test. In this example, `rtos/Semaphore.cpp` is a class under test.
90
102
91
103
1. Create a directory for unit test files in `UNITTESTS/rtos/Semaphore`.
@@ -97,11 +109,13 @@ With the following steps, you can write a simple unit test. In this example, `rt
97
109
)
98
110
99
111
set(unittest-test-sources
100
-
stubs/mbed_assert.c
112
+
stubs/mbed_assert_stub.c
113
+
stubs/Kernel_stub.cpp
101
114
rtos/Semaphore/test_Semaphore.cpp
102
115
)
103
116
```
104
-
117
+
1. Stub all external dependencies. Create stubs `UNITTESTS/stubs/mbed_assert_stub.c` and `UNITTESTS/stubs/Kernel_stub.cpp` if they do not exist.
118
+
1. Update header stubs with any missing type or function declarations.
105
119
1. Create a test source file `UNITTESTS/rtos/Semaphore/test_Semaphore.cpp` with the following content:
106
120
107
121
```
@@ -185,6 +199,7 @@ Run a test binary in the build directory to run a unit test suite. To run multip
185
199
186
200
1. Use Mbed CLI to build a debug build. For advanced use, run CMake directly with `-DCMAKE_BUILD_TYPE=Debug`, and then run a Make program.
187
201
1. Run GDB with a test executable as an argument to debug unit tests.
202
+
1. Run tests with Valgrind to analyze test memory profile.
188
203
189
204
### Get code coverage
190
205
@@ -200,8 +215,11 @@ Use Mbed CLI to generate code coverage reports. For advanced use, follow these s
200
215
**Problem:** Generic problems with CMake or with the build process.
201
216
***Solution**: Delete the build directory. Make sure that CMake, g++, GCC and a Make program can be found in the path and are correct versions.
202
217
203
-
**Problem:** Virus protection identifies files generated by CMake as malicious and quarantines the files on Windows.
218
+
**Problem:**(Windows) Virus protection identifies files generated by CMake as malicious and quarantines the files.
204
219
***Solution**: Restore the false positive files from the quarantine.
205
220
206
-
**Problem:** CMake compiler check fails on Mac OS Mojave when using GCC-8.
221
+
**Problem:** (Windows) Git with shell installation adds sh.exe to the path and then CMake throws an error: sh.exe was found in your PATH. For MinGW make to work correctly sh.exe must NOT be in your path.
222
+
***Solution**: Remove sh.exe from the system path.
223
+
224
+
**Problem:** (Mac OS) CMake compiler check fails on Mac OS Mojave when using GCC-8.
207
225
***Solution**: Make sure gnm (binutils) is not installed. Uninstall binutils with `brew uninstall binutils`.
0 commit comments