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
@@ -104,75 +104,148 @@ You can also set custom compiler flags and other configurations supported by CMa
104
104
105
105
#### Example
106
106
107
-
With the following steps, you can write a simple unit test. In this example, `rtos/Semaphore.cpp` is a class under test.
107
+
With the following steps, you can write a simple unit test. This example creates dummy classes to be tested, creates and configures unit tests for a class and stubs all external dependencies.
108
108
109
-
1. Create a directory for unit test files in `UNITTESTS/rtos/Semaphore`.
110
-
1. Create a test configuration file `UNITTESTS/rtos/Semaphore/unittest.cmake` with the following content:
109
+
1. Create the following dummy classes in `mbed-os/example`:
111
110
112
-
```
113
-
set(unittest-sources
114
-
../rtos/Semaphore.cpp
115
-
)
116
-
117
-
set(unittest-test-sources
118
-
stubs/mbed_assert_stub.c
119
-
stubs/Kernel_stub.cpp
120
-
rtos/Semaphore/test_Semaphore.cpp
121
-
)
122
-
```
123
-
1. Stub all external dependencies. Create stubs `UNITTESTS/stubs/mbed_assert_stub.c` and `UNITTESTS/stubs/Kernel_stub.cpp` if they don't already exist.
124
-
1. Update header stubs with any missing type or function declarations.
125
-
1. Create a test source file `UNITTESTS/rtos/Semaphore/test_Semaphore.cpp` with the following content:
1. Create a directory for MyClass unit tests in `UNITTESTS/example/MyClass`.
178
+
1. Create a configuration file and a source file for MyClass unit tests in `UNITTESTS/example/MyClass`:
179
+
180
+
**unittest.cmake**
181
+
182
+
```
183
+
# Add here additional test specific include paths
184
+
set(unittest-includes ${unittest-includes}
185
+
../example
186
+
)
187
+
188
+
# Add here classes under test
189
+
set(unittest-sources
190
+
../example/MyClass.cpp
191
+
)
192
+
193
+
# Add here test classes and stubs
194
+
set(unittest-test-sources
195
+
example/MyClass/test_MyClass.cpp
196
+
stubs/OtherClass_stub.cpp
197
+
)
198
+
```
199
+
200
+
**test_MyClass.cpp**
201
+
202
+
```
203
+
#include "gtest/gtest.h"
204
+
#include "example/MyClass.h"
205
+
206
+
class TestMyClass : public testing::Test {
207
+
protected:
208
+
example::MyClass *obj;
209
+
210
+
virtual void SetUp()
211
+
{
212
+
obj = new example::MyClass();
213
+
}
214
+
215
+
virtual void TearDown()
216
+
{
217
+
delete obj;
218
+
}
219
+
};
220
+
221
+
TEST_F(TestMyClass, constructor)
161
222
{
162
-
sem = new rtos::Semaphore();
223
+
EXPECT_TRUE(obj);
163
224
}
164
225
165
-
virtual void TearDown()
226
+
TEST_F(TestMyClass, myfunction)
166
227
{
167
-
delete sem;
228
+
EXPECT_EQ(obj->myFunction(), 0);
229
+
}
230
+
```
231
+
232
+
1. Stub all external dependencies. Create the following stub in `UNITTESTS/stubs`:
233
+
234
+
**OtherClass_stub.cpp**
235
+
236
+
```
237
+
#include "example/OtherClass.h"
238
+
239
+
namespace example {
240
+
241
+
int OtherClass::otherFunction() {
242
+
return 0;
243
+
}
244
+
168
245
}
169
-
};
246
+
```
170
247
171
-
TEST_F(TestSemaphore, constructor)
172
-
{
173
-
EXPECT_TRUE(sem);
174
-
}
175
-
```
248
+
This example does not use any Mbed OS code, but if your unit tests do, then remember to update header stubs in `UNITTESTS/target_h` and source stubs in `UNITTESTS/stubs` with any missing type or function declarations.
176
249
177
250
### Building and running unit tests
178
251
@@ -187,6 +260,7 @@ Use Mbed CLI to build and run unit tests. For advanced use, you can run CMake an
187
260
* Add `-DCMAKE_MAKE_PROGRAM=<value>`, `-DCMAKE_CXX_COMPILER=<value>` and `-DCMAKE_C_COMPILER=<value>` to use a specific Make program and compilers.
188
261
* Add `-DCMAKE_BUILD_TYPE=Debug` for a debug build.
189
262
* Add `-DCOVERAGE=True` to add coverage compiler flags.
263
+
* Add `-Dgtest_disable_pthreads=ON` to run in a single thread.
190
264
* See the [CMake manual](https://cmake.org/cmake/help/v3.0/manual/cmake.1.html) for more information.
0 commit comments