tests/README.md
1 Running and creating tests {#running-and-creating-tests}
2 ==========================
3 
4 There are a number of tests included in RIOT. They are located in the
5 [tests folder](https://github.com/RIOT-OS/RIOT/tree/master/tests). These tests
6 allow basic functionality to be verified as well as provide an example of
7 usage.
8 
9 
10 Implementing automated tests
11 ----------------------------
12 
13 The goal is to be able to run all tests in a sequential way for as many targets
14 as possible.
15 
16 As some board can't be reset without a manual trigger tests should be implemented
17 with some kind of `synchronization`. This can be done in two ways:
18 
19 - use `test_utils_interactive_sync` when uart input/output does not need to be
20  disabled for the test. This is enabled by default.
21 - set up the test in a loop so the test script will be able so sync with some kind
22  of start condition in the test.
23 
24 The module for the first option is `test_utils_interactive_sync` and is set as a
25 default module in `Makefile.tests_common`. It can be disabled by setting in the
26 application makefile `DISABLE_MODULE += test_utils_interactive_sync`. The python
27 test script will adapt to it automatically.
28 
29 
30 Running automated tests
31 -----------------------
32 
33 Some tests can be performed automatically. The test automation scripts are
34 defined in the `<test_application>/tests/` folder. They are written in python
35 and interact through the uart with the test application code running on a
36 board to do the validation. It is recommended to flash the board with the
37 test just before running it because some platforms cannot be reset while
38 testing.
39 
40 From the test application directory run:
41 
42  BOARD=<board_of_your_choice> make flash test
43 
44 
45 An automated way of knowing if a test is available is to execute the
46 'test/available' target from the test application directory.
47 It executes without error if tests run by 'make test' are present.
48 
49  make test/available
50 
51 
52 Automated Tests Guidelines
53 --------------------------
54 
55 
56 When using `pexpect` `$` is useless for matching the end of a line, instead use
57 `\r\n`([pexpect end-of-line](https://pexpect.readthedocs.io/en/stable/overview.html#find-the-end-of-line-cr-lf-conventions)).
58 
59 Beware of `+` and `*` at the end of patterns. These patterns will always get
60 a minimal match (non-greedy).([pexpect end-of-patterns](https://pexpect.readthedocs.io/en/stable/overview.html#beware-of-and-at-the-end-of-patterns))
61 This can be an issue when matching groups and using the matched groups to verify
62 some kind of behavior since `*` could return an empty match and `+` only a subset.
63 
64 This is especially prevalent since `printf()` is buffered so the output might not
65 arrive in a single read to `pexpect`.
66 
67 To avoid this make sure to match a non-ambiguous character at the end of the
68 pattern like `\r\n`, `\s`, `\)`, etc..
69 
70 **don't**:
71 
72 ~~~~
73  child.expect(r'some string: (\d+)')
74 ~~~~
75 
76 **do**:
77 
78 ~~~
79  child.expect(r'some string: (\d+)\r\n')
80 ~~~
81 ~~~
82  child.expect(r'some string: (\d+)\s')
83 ~~~
84 ~~~
85  child.expect(r'some string: (\d+) ,')
86 ~~~
87 
88 Interaction through the uart
89 ----------------------------
90 
91 Tests implemented with `testrunner` use the `cleanterm` target that
92 provides an interaction without adding extra text output or input handling.
93 It can currently be expected to have unmodified line based interaction with the
94 board.
95 
96 The expected behavior is verified with the test in `tests/test_tools`.
97 
98 Tests cannot rely on having on all boards and terminal programs:
99 * unbuffered input
100 * allowing sending special characters like `ctrl+c/ctrl+d`