Skip to content

Commit 3fa7796

Browse files
Add a readme to unit tests
1 parent 8557529 commit 3fa7796

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

UNITTESTS/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
## Stubs, mocks and fakes in Mbed OS
2+
3+
To make unit testing easier, Mbed OS provides some ready-made CMake libraries you can add in your `target_link_libraries`.
4+
Libraries can be object files that contain stubs, mocks and fakes but also sets of include paths - this way you can gain access to normal headers from Mbed OS.
5+
6+
All the libraries available are in the `CMakeLists.txt` files in `mbed-os/UNITTESTS/stubs` and `mbed-os/UNITTESTS/fakes`.
7+
8+
The most common ones you might need are `mbed-os-fakes-ble` and `mbed-os-fakes-event-queue`.
9+
10+
### Fakes
11+
12+
While stubs are self explanatory and don't offer anything beyond empty implementations, fakes allow for more complex unittests that can simulate whole subsystems.
13+
14+
#### mbed-os-fakes-ble
15+
16+
This library provides a fake BLE implementation that uses mocks instead of real BLE components for `Gap`, `GattServer`, `GattClient`, `SecurityManager`.
17+
18+
There is no need to initialise a fake BLE instance; it is ready to go and can be used just like a normal BLE instance:
19+
20+
```c++
21+
BLE *ble = &BLE::Instance();
22+
```
23+
24+
This call also initialises mocks.
25+
Do no cache the BLE instance pointer, or pointer to `GAP`, `GattServer` etc. between tests.
26+
You must get the instance fresh at the start of the test.
27+
28+
You can retrieve all the BLE APIs from the instance just like with a normal one:
29+
30+
```c++
31+
Gap &gap = ble->gap();
32+
GattClient &client = ble->gattClient();
33+
GattServer &server = ble->gattServer();
34+
SecurityManager &sm = ble->securityManager();
35+
```
36+
37+
Whenever an API call is made, the implementation will be called.
38+
These are replaced in the fake BLE with google mocks.
39+
This means you can set expectations on them.
40+
41+
```c++
42+
EXPECT_CALL(ble::gap_mock(), reset());
43+
```
44+
45+
This will set up an expectation that at some point during the test the `Gap::reset` method will be called.
46+
47+
The way GoogleTest works means that if you set expectations on your mocks they must be destroyed at the end of each test.
48+
This is done through the fake BLE instance special method:
49+
50+
```c++
51+
ble::delete_mocks();
52+
```
53+
54+
#### mbed-os-fakes-event-queue
55+
56+
This is a fake event queue that doesn't bring in any dependencies from mbed-os.
57+
Its API is simplified and it only offers limited functionality.
58+
59+
If you choose to use it you must not also include a library that brings in real headers for the event queue as they would conflict.
60+
61+
The API calls supported are for simple calls `call`, `call_in` and the `cancel` method.
62+
63+
The event queue is not run in real time and must be progressed manually.
64+
You may use `dispatch(int milliseconds)` and `dispatch_forever()` to process events in the queue.
65+
This way you can simulate the passage of time in your test.

0 commit comments

Comments
 (0)