Blame view

RIOT/tests/unittests/README.md 7.36 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
  # Unittests
  
  ## Building and running tests
  Tests can be built by calling:
  
  ```bash
  cd tests/unittests
  make
  ```
  
  If there are tests for a module you even can build tests specifically for this module:
  
  ```bash
  make tests-<module>
  # e.g.
  make tests-core
  ```
  
  You then can run the tests by calling
  
  ```bash
  make term
  ```
  
  or flash them to your board as you would flash any RIOT application to the board (see [board documentation|RIOT-Platforms](https://github.com/RIOT-OS/RIOT/wiki/RIOT-Platforms)).
  
  You can debug your tests by running
  
  ```bash
  make debug
  ```
  and using GDB as usual.
  
  ### Other output formats
  Other output formats using [*embUnit*](http://embunit.sourceforge.net/)'s ``textui`` library are available by setting the environment variable ``OUTPUT``:
  
  * Compiler: ``OUTPUT="COMPILER"``
  * Text: ``OUTPUT="TEXT"``
  * XML: ``OUTPUT="XML"``
  * Color: ``OUTPUT="COLOR"`` (like default, but with red/green output)
  * Colored-Text: ``OUTPUT="COLORTEXT"`` (like ``TEXT``, but with red/green output)
  
  #### Compile example
  ```bash
  OUTPUT="COMPILER" make tests-core
  make term
  ```
  
  (only outputs in case of test failures)
  
  #### Text example
  ```bash
  OUTPUT="TEXT" make tests-core
  make term
  ```
  
  ```
  - core_bitarithm_tests
  1) OK test_SETBIT_null_null
  2) OK test_SETBIT_null_limit
  3) ...
  - core_clist_tests
  25) ...
  - ...
  
  OK (... tests)
  ```
  
  #### XML example
  ```bash
  OUTPUT="XML" make tests-core
  make term
  ```
  
  ```XML
  <?xml version="1.0" encoding='shift_jis' standalone='yes' ?>
  <TestRun>
  <core_bitarithm_tests>
  <Test id="1">
  <Name>test_SETBIT_null_null</Name>
  </Test>
  <Test id="2">
  <Name>test_SETBIT_null_limit</Name>
  </Test>
  ...
  </core_bitarithm_tests>
  <core_clist_tests>
  <Test id="25">
  <Name>test_clist_add_one</Name>
  </Test>
  ...
  </core_clist_tests>
  <Statistics>
  <Tests>...</Tests>
  </Statistics>
  </TestRun>
  ```
  
  ## Writing unit tests
  ### File struture
  RIOT uses [*embUnit*](http://embunit.sourceforge.net/) for unit testing.
  All unit tests are organized in ``tests/unittests`` and can be built module-wise, if needed.
  For each module there exists a ``tests-<modulename>/tests-<modulename>.h`` file, at least one C file in ``tests-<modulename>/`` and a ``tests-<modulename>/Makefile``.
  It is recommended to add a C file named ``tests-<modulename>/tests-<modulename>-<headername>.c`` for every header file that defines functions (or macros) implemented in the module.
  If there is only one such header file ``tests-<modulename>/tests-<modulename>.c`` should suffice.
  
  Each ``*.c`` file should implement a function defined in ``tests-<modulename>/tests-<modulename>.h``, named like
  
  ```C
  Test *tests_<modulename>_<headername>_tests(void);
  
  /* or respectively */
  
  Test *tests_<modulename>_tests(void);
  ```
  
  ### Testing a module
  To write new tests for a module you need to do three things:
  
  1. **[Create a Makefile](#create-a-makefile)**: add a file ``tests-<modulename>/Makefile``
  2. **[Define a test header](#define-a-test-header)**: add a file ``tests-<modulename>/tests-<modulename>.h``
  3. **[Implement tests](#implement-tests)**: for each header file, that defines a function or macro implemented or related to the module, add a file ``tests-<modulename>/tests-<modulename>-<headername>.c`` or ``tests-<modulename>/tests-<modulename>.c`` if there is only one header.
  
  #### Create a Makefile
  The Makefile should have the following content:
  
  ```Makefile
  include $(RIOTBASE)/Makefile.base
  ```
  
  #### Define a test header.
  The test header ``tests-<modulename>/tests-<module>.h`` of a module you add to ``tests/unittests/`` should have the following structure
  
  ```C
  /*
   * Copyright (C) <year> <author>
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @addtogroup  unittests
   * @{
   *
   * @file
   * @brief       Unittests for the ``module`` module
   *
   * @author      <author>
   */
  #ifndef TESTS_<MODULE>_H
  #define TESTS_<MODULE>_H
  #include "embUnit/embUnit.h"
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  /**
   * @brief   Generates tests for <header1>.h
   *
   * @return  embUnit tests if successful, NULL if not.
   */
  Test *tests_<module>_<header1>_tests(void);
  
  /**
   * @brief   Generates tests for <header2>.h
   *
   * @return  embUnit tests if successful, NULL if not.
   */
  Test *tests_<module>_<header2>_tests(void);
  
  /* ... */
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif /* TESTS_<MODULE>_H */
  /** @} */
  ```
  
  #### Implement tests
  Every ``tests-<modulename>/tests-<module>*.c`` file you add to ``tests/unittests/`` should have the following structure:
  
  ```C
  /*
   * Copyright (C) <year> <author>
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /* clib includes */
  
  #include "embUnit.h"
  
  #include "<header>.h"
  
  #include "tests-<module>.h"
  
  /* your macros */
  
  /* your global variables */
  
  static void set_up(void)
  {
      /* omit if not needed */
  }
  
  static void tear_down(void)
  {
      /* omit if not needed */
  }
  
  static void test_<function1>_<what1>(void) {
      /* ... */
  
      TEST_ASSERT(/* ... */);
  }
  
  static void test_<function1>_<what2>(void) {
      /* ... */
  
      TEST_ASSERT(/* ... */);
  }
  
  /* ... */
  
  static void test_<function2>_<what1>(void) {
      /* ... */
  
      TEST_ASSERT(/* ... */);
  }
  
  static void test_<function2>_<what2>(void) {
      /* ... */
  
      TEST_ASSERT(/* ... */);
  }
  
  /* ... */
  
  Test *tests_<module>_<header>_tests(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          new_TestFixture(test_<function1>_<what1>),
          new_TestFixture(test_<function1>_<what2>),
          new_TestFixture(test_<function2>_<what1>),
          new_TestFixture(test_<function2>_<what2>),
          /* ... */
      };
  
      EMB_UNIT_TESTCALLER(<module>_<header>_tests,
                          tests_<module>_<header>_set_up,
                          tests_<module>_<header>_tear_down, fixtures);
      /* set up and tear down function can be NULL if omitted */
  
      return (Test *)&<module>_<header>;
  }
  ```
  
  The following assertion macros are available via *embUnit*
  
  <table>
    <thead>
      <tr>
        <th>Assertion</th>
        <th>Description</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          <code>TEST_ASSERT_EQUAL_STRING(expected,actual)</code>
        </td>
        <td>
          Assert that strings actual and expected are equivalent
        </td>
      </tr>
      <tr>
        <td>
          <code>TEST_ASSERT_EQUAL_INT(expected,actual)</code>
        </td>
        <td>
          Assert that integers actual and expected are equivalent
        </td>
      </tr>
      <tr>
        <td>
          <code>TEST_ASSERT_NULL(pointer)</code>
        </td>
        <td>
          Assert that <code>pointer == NULL</code>
        </td>
      </tr>
      <tr>
        <td>
          <code>TEST_ASSERT_NOT_NULL(pointer)</code>
        </td>
        <td>
          Assert that <code>pointer != NULL</code>
        </td>
      </tr>
      <tr>
        <td>
          <code>TEST_ASSERT_MESSAGE(condition, message)</code>
        </td>
        <td>
          Assert that condition is <code>TRUE</code> (non-zero) or output customized <code>message</code> on failure.
        </td>
      </tr>
      <tr>
        <td>
          <code>TEST_ASSERT(condition)</code>
        </td>
        <td>
          Assert that condition is <code>TRUE</code> (non-zero)
        </td>
      </tr>
      <tr>
        <td>
          <code>TEST_FAIL(message)</code>
        </td>
        <td>
          Register a failed assertion with the specified message. No logical test is performed.
        </td>
      </tr>
    </tbody>
  </table>