Skip to content

Commit 4b1cde8

Browse files
author
Amanda Butler
authored
Merge pull request #677 from lorjala/unittests
Add documentation for unit testing
2 parents fc0d845 + 63d9bbe commit 4b1cde8

File tree

4 files changed

+113
-1
lines changed

4 files changed

+113
-1
lines changed

docs.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,11 @@
835835
},
836836
{
837837
"type": "markdown",
838-
"url": "https://github.com/ARMmbed/mbed-os-5-docs/blob/development/docs/tools/testing/testing_greentea.md"
838+
"url": "https://github.com/ARMmbed/mbed-os-5-docs/blob/development/docs/tools/testing/unit_testing.md"
839+
},
840+
{
841+
"type": "markdown",
842+
"url": "https://github.com/ARMmbed/mbed-os-5-docs/blob/development/docs/tools/testing/testing_greentea.md
839843
},
840844
{
841845
"type": "markdown",

docs/tools/offline/cli-test-debug.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Test and debug
22

3+
### Testing
4+
35
Use the `mbed test` command to compile and run tests.
46

57
There are two testing frameworks: Greentea and Icetea. Greentea provides tests designed for driver porting and target verification. Icetea provides and manages tests for multiple devices at the same time. For example, you can test the network setup for a server and multiple clients, simultaneously controlling them from the test environment.
@@ -210,6 +212,65 @@ As shown above, tests exist inside `TESTS\testgroup\testcase\` directories. Plea
210212

211213
<span class="notes">**Note:** `mbed test` does not work in applications that contain a `main` function that is outside of a `TESTS` directory.</span>
212214

215+
### Unit testing
216+
217+
Use the `mbed unittest` command to build and run unit tests, or to generate files for new unit tests.
218+
219+
Build and run unit tests with `mbed unittest`. The arguments are:
220+
221+
* `--compile` to only compile unit tests.
222+
* `--run` to only run unit tests.
223+
* `-c` or `--clean` to clean build directory.
224+
* `-d` or `--debug` to prepare debug build.
225+
* `--coverage <TYPE>` to generate code coverage report where TYPE can be "html", "xml" or "both".
226+
* `-m <NAME>` or `--make-program <NAME>` to select which make build tool to use where NAME can be "make", "gmake", "mingw32-make" or "ninja".
227+
* `-g <NAME>` or `--generator <NAME>` to select which CMake generator to use where NAME can be "Unix Makefiles", "MinGW Makefiles" or "Ninja".
228+
* `-r <EXPRESSION>` or `--regex <EXPRESSION>` to run tests matching the regular expression.
229+
* `--build <PATH>` to specify build directory.
230+
* `-v` or `--verbose` for verbose diagnostic output.
231+
232+
Generate files for a new unit test with `mbed unittest --new <FILE>`.
233+
234+
### Building and running unit tests
235+
236+
You can specify to only **build** the unit tests by using the `--compile option:
237+
238+
```
239+
$ mbed unittest --compile
240+
```
241+
242+
You can specify to only **run** the unit tests by using the `--run` option:
243+
244+
```
245+
$ mbed unittest --run
246+
```
247+
248+
If you do not specify any of these, `mbed unittest` builds all available unit tests and runs them.
249+
250+
### Running a subset of tests
251+
252+
You can run a **limited set** of unit tests by using the `-r` or `--regex` option. This takes a regular expression, which it compares against the test names. For example, to run all cellular unit tests, you can specify:
253+
254+
```
255+
$ mbed unittest -r cellular
256+
```
257+
258+
### Getting code coverage
259+
260+
You can generate a code coverage report by using the `--coverage` option. For example, to create an html report, you can specify:
261+
262+
```
263+
$ mbed unittest --coverage html
264+
```
265+
266+
### Creating new unit tests
267+
268+
All unit tests are under the `mbed-os/UNITTESTS` directory. You can **generate** the necessary files for a unit test by using the `--new` option. For example, to create the files for `rtos/Semaphore.cpp`, you can specify:
269+
270+
```
271+
$ mbed unittest --new rtos/Semaphore.cpp
272+
```
273+
213274
### Troubleshooting
214275

215276
#### Import Mercurial (mbed.org) programs or libraries

docs/tools/testing/testing_intro.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Testing is a critical step in the development process. The Arm Mbed ecosystem offers several tools to help you test your code.
44

5+
You can use unit testing tools to build and run Mbed OS unit tests. Each unit test is built into an isolated test executable using a separate build system and build tools native to the development machine without any hardware or software dependencies. You can use the tool with Mbed CLI using the keyword `unittest`.
6+
57
Greentea, Icetea, `htrun` and `mbed-ls` are testing tools written in Python. Greentea tests serve as functional unit tests in C++, as well as integration tests for complex use cases that execute on microcontrollers. The Icetea test tool adds more support for interoperability testing by making it easier to handle multiple devices and external service during testing. Arm Mbed CLI has a verb `test` that drives these tools to form a testing system. These comprise our automated testing framework for Mbed OS development.
68

79
The testing system automates the process of flashing Mbed boards, driving the tests and accumulating test results into test reports. Developers and Mbed Partners use this system for local development, as well as for automation in a Continuous Integration environment.

docs/tools/testing/unit_testing.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## Unit testing
2+
3+
### Introduction
4+
5+
Traditional software testing is defined into three main levels: unit testing, integration testing and system testing. These levels are often pictured as a pyramid to indicate the amount of testing for each level.
6+
7+
```
8+
^ Testing level
9+
|
10+
| /\
11+
| / \ System testing
12+
| /----\
13+
| / \ Integration testing
14+
| /--------\
15+
| / \ Unit testing
16+
| /------------\
17+
|
18+
*-------------------> Amount of tests
19+
```
20+
21+
We perform integration and system testing in an environment where we run the tests with the full Mbed OS. Other testing tools for Mbed OS require specific embedded hardware, which means traditional unit testing is not possible.
22+
23+
Unit testing takes place in a build environment where we test each C or C++ class or module in isolation. This means we build test suites into separate test binaries and stub all access outside to remove dependencies on any specific embedded hardware or software combination. This allows us to complete the testing using native compilers on the build machine.
24+
25+
### Using unit tests
26+
27+
#### Test code structure
28+
29+
Unit tests are located in Mbed OS repository under `UNITTESTS`. Each unit test uses an identical directory tree structure to the file to be tested. This makes it easier to find unit tests for a particular class or a module. For example, if the file to be tested is `rtos/Semaphore.cpp`, then all the test files are in the `UNITTESTS/rtos/Semaphore` directory.
30+
31+
##### Test discovery
32+
33+
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.
34+
35+
For the build system to find and build any test suite automatically, a unit test configuration file named `unittest.cmake` is required to be included with each unit test. This configuration file contains a name for the test and other source files the build requires.
36+
37+
##### Test names
38+
39+
Each test suite requires a name to be configured in the `unittest.cmake` file. This name is used for generated files and when running a subset of tests.
40+
41+
#### Building, running and writing tests
42+
43+
Unit testing requires external tools you need to install. See the developer [documentation](https://github.com/ARMmbed/mbed-os/blob/master/UNITTESTS/README.md) in GitHub for the full installation guide.
44+
45+
You can build and run unit tests through Arm Mbed CLI. You can also use the tool to generate new test files. For information on using Mbed CLI, please see the [CLI documentation](/docs/development/tools/arm-mbed-cli.html).

0 commit comments

Comments
 (0)