Skip to content

Commit c85f64b

Browse files
committed
Merge branch 'greentea-test-update' of github.com:janjongboom/mbed-os-5-docs into greentea-test-update
2 parents 2545e82 + 3ae40d2 commit c85f64b

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

docs/tools/testing/testing_greentea.md

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
## Greentea testing applications
22

3-
Greentea is the automated testing tool for Arm Mbed OS development. It's a test runner that automates the process of flashing development boards, starting tests, and accumulating test results into test reports. Developers use it for local development as well as for automation in a continuous integration environment.
3+
Greentea is the automated testing tool for Arm Mbed OS development. It's a test runner that automates the process of flashing development boards, starting tests and accumulating test results into test reports. You can use it for local development, as well as for automation in a continuous integration environment.
44

5-
Greentea tests run on embedded devices, but Greentea also supports 'host tests'. These are Python scripts that run on a computer and can communicate back to the embedded device. You can for example verify that a value was actually written to the cloud when the device said it did so.
5+
Greentea tests run on embedded devices, but Greentea also supports `host tests`. These are Python scripts that run on a computer and can communicate back to the embedded device. You can, for example, verify that a value wrote to the cloud when the device said it did.
66

7-
This document should help you start using Greentea. Please see the [`htrun` documentation](https://github.com/ARMmbed/mbed-os-tools/tree/master/packages/mbed-host-tests), the tool Greentea uses to drive tests, for the technical details of the interactions between the platform and the host machine.
7+
This document will help you start using Greentea. Please see the [`htrun` documentation](https://github.com/ARMmbed/mbed-os-tools/tree/master/packages/mbed-host-tests), the tool Greentea uses to drive tests, for the technical details of the interactions between the platform and the host machine.
88

99
### Using tests
1010

@@ -73,15 +73,15 @@ If you need to test with multiple configurations, then you can use the `--app-co
7373

7474
### Writing your first test
7575

76-
You can write tests for your own project or add more tests to Mbed OS. You can write tests by using the [Greentea client](https://github.com/ARMmbed/mbed-os/tree/master/features/frameworks/greentea-client), and the [UNITY](https://github.com/ARMmbed/mbed-os/tree/master/features/frameworks/unity) and [utest](https://github.com/ARMmbed/mbed-os/tree/master/features/frameworks/utest) frameworks, which are located in `/features/frameworks`.
76+
You can write tests for your own project or add more tests to Mbed OS. You can write tests by using the [Greentea client](https://github.com/ARMmbed/mbed-os/tree/master/features/frameworks/greentea-client) and the [UNITY](https://github.com/ARMmbed/mbed-os/tree/master/features/frameworks/unity) and [utest](https://github.com/ARMmbed/mbed-os/tree/master/features/frameworks/utest) frameworks, which are located in `/features/frameworks`.
7777

78-
Let's write your first test. Use Mbed CLI to create a new project:
78+
To write your first test, use Mbed CLI to create a new project:
7979

8080
```
8181
$ mbed new first-greentea-test
8282
```
8383

84-
As specified above, there is a convention where all tests live in the `TESTS/` directory. In the `first-greentea-test` folder create a folder `TESTS/test-group/simple-test/`.
84+
By convention, all tests live in the `TESTS/` directory. In the `first-greentea-test` folder, create a folder `TESTS/test-group/simple-test/`.
8585

8686
```
8787
first-greentea-test/
@@ -93,7 +93,7 @@ first-greentea-test/
9393

9494
*Test structure for Greentea tests*
9595

96-
In this folder, create a file `main.cpp`. In here you can use UNITY, utest and the Greentea Client to write your test:
96+
In this folder, create a file `main.cpp`. You can use UNITY, utest and the Greentea client to write your test:
9797

9898
```cpp
9999
#include "mbed.h"
@@ -103,7 +103,7 @@ In this folder, create a file `main.cpp`. In here you can use UNITY, utest and t
103103

104104
using namespace utest::v1;
105105

106-
// this is how a test case looks
106+
// This is how a test case looks
107107
static control_t simple_test(const size_t call_count) {
108108
/* test content here */
109109
TEST_ASSERT_EQUAL(4, 2 * 2);
@@ -112,7 +112,7 @@ static control_t simple_test(const size_t call_count) {
112112
}
113113

114114
utest::v1::status_t greentea_setup(const size_t number_of_cases) {
115-
// here we specify the timeout (60s) and the host test (a built-in host test or the name of our Python file)
115+
// Here, we specify the timeout (60s) and the host test (a built-in host test or the name of our Python file)
116116
GREENTEA_SETUP(60, "default_auto");
117117

118118
return greentea_test_setup_handler(number_of_cases);
@@ -132,14 +132,16 @@ int main() {
132132
133133
#### Running the test
134134
135-
Let's run this test (tip: to see all tests, run `mbed test --compile-list`):
135+
<span class="tips">**Tip:** To see all tests, run `mbed test --compile-list`.</span>
136+
137+
Run the test:
136138
137139
```
138140
# run the test with the GCC_ARM toolchain, automatically detect the target, and run in verbose mode (-v)
139141
$ mbed test -t GCC_ARM -m auto -v -n tests-test-group-simple-test
140142
```
141143
142-
This will yield (on a NUCLEO F411RE):
144+
This yields (on a NUCLEO F411RE):
143145
144146
```
145147
mbedgt: test suite report:
@@ -159,17 +161,17 @@ mbedgt: test case results: 1 OK
159161
mbedgt: completed in 18.64 sec
160162
```
161163
162-
Victory! Now change the test in a way that it should fail (for example, expect 6 instead of 4), re-run the test, and observe the difference.
164+
Change the test in a way that it fails (for example, expect 6 instead of 4), rerun the test and observe the difference.
163165
164166
### Writing integration tests using host tests
165167
166-
The previous test was simple because it was all self-contained. Everything that ran only impacted the microcontroller. But typical test cases involve things in the real world. Things like: did my device actually get an internet connection, or did my device actually register with my cloud service? (We have [a lot of these](https://github.com/ARMmbed/simple-mbed-cloud-client/tree/master/TESTS) for Pelion Device Management.) To test these scenarios you can use a 'host test' that runs on your computer. After the device says it did something you can verify that it actually happened, and then pass or fail the test accordingly.
168+
The previous test was self-contained. Everything that ran only affected the microcontroller. However, typical test cases involve peripherals in the real world. This raises questions such as: Did my device actually get an internet connection, or did my device actually register with my cloud service? (We have [a lot of these](https://github.com/ARMmbed/simple-mbed-cloud-client/tree/master/TESTS) for Pelion Device Management.) To test these scenarios, you can use a host test that runs on your computer. After the device says it did something, you can verify that it happened and then pass or fail the test accordingly.
167169
168-
To interact with the host test from the device you have two functions: `greentea_send_kv` and `greentea_parse_kv`. The latter blocks until it gets a message back from the host. Let's write an integration test that sends 'hello' to the host, and waits until it gets 'world' back.
170+
To interact with the host test from the device, you can use two functions: `greentea_send_kv` and `greentea_parse_kv`. The latter blocks until it gets a message back from the host.
169171
170172
#### Creating the host test
171173
172-
Create a file called `hello_world_tests.py` in the `TESTS/host_tests` folder, and fill it with:
174+
This example writes an integration test that sends `hello` to the host and waits until it receives `world`. Create a file called `hello_world_tests.py` in the `TESTS/host_tests` folder, and fill it with:
173175
174176
```py
175177
from mbed_host_tests import BaseHostTest
@@ -205,11 +207,11 @@ class HelloWorldHostTests(BaseHostTest):
205207
self.logger = HtrunLogger('TEST')
206208
```
207209

208-
This registers one function you can call from the device: `init`. The function will check whether the value was `hello`, and if so, return `world` back to the device, via the `send_kv` function.
210+
This registers one function you can call from the device: `init`. The function checks whether the value was `hello`, and if so, returns `world` back to the device using the `send_kv` function.
209211

210212
#### Creating the Greentea test
211213

212-
Now let's write the embedded part of this test. Create a new file `main.cpp` in `TESTS/tests/integration-test`, and fill it with:
214+
This example writes the embedded part of this test. Create a new file `main.cpp` in `TESTS/tests/integration-test`, and fill it with:
213215

214216
```cpp
215217
#include "mbed.h"
@@ -224,7 +226,7 @@ static control_t hello_world_test(const size_t call_count) {
224226
greentea_send_kv("init", "hello");
225227

226228
// wait until we get a message back
227-
// if this takes too long the timeout will trigger, so no need to handle this here
229+
// if this takes too long, the timeout will trigger, so no need to handle this here
228230
char _key[20], _value[128];
229231
while (1) {
230232
greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
@@ -240,7 +242,7 @@ static control_t hello_world_test(const size_t call_count) {
240242
}
241243

242244
utest::v1::status_t greentea_setup(const size_t number_of_cases) {
243-
// here we specify the timeout (60s) and the host runner (the name of our Python file)
245+
// here, we specify the timeout (60s) and the host runner (the name of our Python file)
244246
GREENTEA_SETUP(60, "hello_world_tests");
245247
return greentea_test_setup_handler(number_of_cases);
246248
}
@@ -256,21 +258,21 @@ int main() {
256258
}
257259
```
258260
259-
You see the calls to/from the host through the `greentea_send_kv` and `greentea_parse_kv` functions. Also take a note of the `GREENTEA_SETUP` call. Here we specify which host test we want to use, and the test is then automatically loaded when running (based on the Python name).
261+
You see the calls to and from the host through the `greentea_send_kv` and `greentea_parse_kv` functions. Note the `GREENTEA_SETUP` call. This specifies which host test to use, and the test is then automatically loaded when running (based on the Python name).
260262
261-
Let's run the test, and see if everything works:
263+
Run the test:
262264
263265
```
264266
$ mbed test -v -n tests-test-group-integration-test
265267
```
266268
267269
### Debugging tests
268270
269-
Debugging tests is a crucial part of the development and porting process. This section covers exporting the test, then driving the test with the test tools while the target is attached to a debugger.
271+
Debugging tests is a crucial part of the development and porting process. This section covers exporting the test and driving the test with the test tools while the target is attached to a debugger.
270272
271273
#### Exporting tests
272274
273-
Currently, the easiest way to export a test is to copy the test's source code from its test directory to your project's root. This way, the tools treat it like a normal application.
275+
The easiest way to export a test is to copy the test's source code from its test directory to your project's root. This way, the tools treat it like a normal application.
274276
275277
You can find the path to the test that you want to export by running the following command:
276278
@@ -322,7 +324,7 @@ This detects your attached target and drives the test. If you need to rerun the
322324
323325
For an explanation of the arguments used in this command, please run `mbedhtrun --help`.
324326
325-
### Command-line usage
327+
### Command-line use
326328
327329
This section highlights a few of the capabilities of the Greentea command-line interface. For a full list of the available options, please run `mbed test --help`.
328330
@@ -343,11 +345,11 @@ Test Case:
343345
...
344346
```
345347
346-
After compilation you can use the `--run-list` argument to list all tests that are ready to be ran.
348+
After compilation, you can use the `--run-list` argument to list all tests that are ready to be ran.
347349
348350
#### Executing all tests
349351
350-
The default action of Greentea using `mbed test` is to execute all tests that are found. You can also add `-v` to make the output more verbose.
352+
The default action of Greentea using `mbed test` is to execute all tests found. You can also add `-v` to make the output more verbose.
351353
352354
#### Limiting tests
353355
@@ -432,20 +434,20 @@ mbed test --report-text text_report.text --run
432434
433435
### Test specification JSON format
434436
435-
The Greentea test specification format decouples the tool from your build system. It provides important data, such as test names, paths to test binaries and the platform on which the binaries should run. This file is automatically generated when running tests through Mbed CLI, but you can also provide it yourself. This way you have fine-grained control over exactly which tests are run, and through which compilers.
437+
The Greentea test specification format decouples the tool from your build system. It provides important data, such as test names, paths to test binaries and the platform on which the binaries should run. This file is automatically generated when running tests through Mbed CLI, but you can also provide it yourself. This way you can control exactly which tests are run and through which compilers.
436438
437439
Greentea automatically looks for files called `test_spec.json` in your working directory. You can also use the `--test-spec` argument to direct Greentea to a specific test specification file.
438440
439-
When you use the `-t` / `--target` argument with the `--test-spec` argument, you can select which "build" should be used. In the example below, you could provide the arguments `--test-spec test_spec.json -t K64F-ARM` to only run that build's tests.
441+
When you use the `-t` / `--target` argument with the `--test-spec` argument, you can select which "build" to use. In the example below, you could provide the arguments `--test-spec test_spec.json -t K64F-ARM` to only run that build's tests.
440442
441443
#### Example of test specification file
442444
443-
In the below example, there are two defined builds:
445+
The below example uses two defined builds:
444446
445447
- Build `K64F-ARM` for NXP `K64F` platform compiled with `ARMCC` compiler.
446448
- Build `K64F-GCC` for NXP `K64F` platform compiled with `GCC ARM` compiler.
447449
448-
Place this file in your root folder and name it `test_spec.json`.
450+
Place this file in your root folder, and name it `test_spec.json`.
449451
450452
```json
451453
{
@@ -494,7 +496,7 @@ Place this file in your root folder and name it `test_spec.json`.
494496
}
495497
```
496498

497-
If you now run `mbed test --run-list` this will now list only these tests:
499+
If you run `mbed test --run-list`, this will now list only these tests:
498500

499501
```
500502
mbedgt: greentea test automation tool ver. 1.2.5
@@ -511,4 +513,4 @@ mbedgt: available tests for built 'K64F-ARM', location 'BUILD/tests/K64F/ARM'
511513

512514
### Known issues
513515

514-
There cannot be a `main()` function outside of a `TESTS` directory when building and running tests. This is because this function will be included in the non-test code build, as described in the [Building process](#building-process) section. When the test code is compiled and linked with the non-test code build, a linker error will occur, due to their being multiple `main()` functions defined. This is why you should either rename your main application file, if you need to build and run tests, or use a different project. Note that this does not affect building projects or applications, only building and running tests.
516+
There cannot be a `main()` function outside of a `TESTS` directory when building and running tests. This is because this function will be included in the nontest code build, as described in the [building process](#building-process) section. When the test code is compiled and linked with the nontest code build, a linker error will occur, due to there being multiple `main()` functions defined. This is why you should either rename your main application file if you need to build and run tests, or use a different project. Note that this does not affect building projects or applications, only building and running tests.

0 commit comments

Comments
 (0)