Skip to content

Commit d0e3d73

Browse files
authored
Merge pull request #640 from AJIOB/docs_param_tests
Create documentation for parameterized tests
2 parents 87dd938 + ad86e15 commit d0e3d73

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

docs/UnityHelperScriptsGuide.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,123 @@ This option specifies the pattern for matching acceptable source file extensions
179179
By default it will accept cpp, cc, C, c, and ino files.
180180
If you need a different combination of files to search, update this from the default `'(?:cpp|cc|ino|C|c)'`.
181181

182+
##### `:use_param_tests`
183+
184+
This option enables parameterized test usage.
185+
That tests accepts arguments from `TEST_CASE` and `TEST_RANGE` macros,
186+
that are located above current test definition.
187+
By default, Unity assumes, that parameterized tests are disabled.
188+
189+
Few usage examples can be found in `/test/tests/test_unity_parameterized.c` file.
190+
191+
You should define `UNITY_SUPPORT_TEST_CASES` macro for tests success compiling,
192+
if you enable current option.
193+
194+
You can see list of supported macros list in the next section.
195+
196+
#### Parameterized tests provided macros
197+
198+
Unity provides support for few param tests generators, that can be combined
199+
with each other. You must define test function as usual C function with usual
200+
C arguments, and test generator will pass what you tell as a list of arguments.
201+
202+
Let's show how all of them works on the following test function definitions:
203+
204+
```C
205+
/* Place your test generators here, usually one generator per one or few lines */
206+
void test_demoParamFunction(int a, int b, int c)
207+
{
208+
TEST_ASSERT_GREATER_THAN_INT(a + b, c);
209+
}
210+
```
211+
212+
##### `TEST_CASE`
213+
214+
Test case is a basic generator, that can be used for param testing.
215+
One call of that macro will generate only one call for test function.
216+
It can be used with different args, such as numbers, enums, strings,
217+
global variables, another preprocessor defines.
218+
219+
If we use replace comment before test function with the following code:
220+
221+
```C
222+
TEST_CASE(1, 2, 5)
223+
TEST_CASE(3, 7, 20)
224+
```
225+
226+
script will generate 2 test calls:
227+
228+
```C
229+
test_demoParamFunction(1, 2, 5);
230+
test_demoParamFunction(10, 7, 20);
231+
```
232+
233+
That calls will be wrapped with `setUp`, `tearDown` and other
234+
usual Unity calls, as for independent unit tests.
235+
The following output can be generated after test executable startup:
236+
237+
```Log
238+
tests/test_unity_parameterizedDemo.c:14:test_demoParamFunction(1, 2, 5):PASS
239+
tests/test_unity_parameterizedDemo.c:14:test_demoParamFunction(10, 7, 20):PASS
240+
```
241+
242+
##### `TEST_RANGE`
243+
244+
Test range is an advanced generator. It single call can be converted to zero,
245+
one or few `TEST_CASE` equivalent commands.
246+
247+
That generator can be used for creating numeric ranges in decimal representation
248+
only: integers & floating point numbers. It uses few formats for every parameter:
249+
250+
1. `[start, stop, step]` is stop-inclusive format
251+
2. `<start, stop, step>` is stop-exclusive formats
252+
253+
Format providers 1 and 2 accept only three arguments:
254+
255+
* `start` is start number
256+
* `stop` is end number (can or cannot exists in result sequence for format 1,
257+
will be always skipped for format 2)
258+
* `step` is incrementing step: can be either positive or negative value.
259+
260+
Let's use our `test_demoParamFunction` test for checking, what ranges
261+
will be generated for our single `TEST_RANGE` row:
262+
263+
```C
264+
TEST_RANGE([3, 4, 1], [10, 5, -2], <30, 31, 1>)
265+
```
266+
267+
Tests execution output will be similar to that text:
268+
269+
```Log
270+
tests/test_unity_parameterizedDemo.c:14:test_demoParamFunction(3, 10, 30):PASS
271+
tests/test_unity_parameterizedDemo.c:14:test_demoParamFunction(3, 8, 30):PASS
272+
tests/test_unity_parameterizedDemo.c:14:test_demoParamFunction(3, 6, 30):PASS
273+
tests/test_unity_parameterizedDemo.c:14:test_demoParamFunction(4, 10, 30):PASS
274+
tests/test_unity_parameterizedDemo.c:14:test_demoParamFunction(4, 8, 30):PASS
275+
tests/test_unity_parameterizedDemo.c:14:test_demoParamFunction(4, 6, 30):PASS
276+
```
277+
278+
As we can see:
279+
280+
| Parameter | Format | Possible values | Total of values | Format number |
281+
|---|---|---|---|---|
282+
| `a` | `[3, 4, 1]` | `3`, `4` | 2 | Format 1 |
283+
| `b` | `[10, 5, -2]` | `10`, `8`, `6` | 3 | Format 1, negative step, end number is not included |
284+
| `c` | `<30, 31, 1>` | `30` | 1 | Format 2 |
285+
286+
_Note_, that format 2 also supports negative step.
287+
288+
We totally have 2 * 3 * 1 = 6 equal test cases, that can be written as following:
289+
290+
```C
291+
TEST_CASE(3, 10, 30)
292+
TEST_CASE(3, 8, 30)
293+
TEST_CASE(3, 6, 30)
294+
TEST_CASE(4, 10, 30)
295+
TEST_CASE(4, 8, 30)
296+
TEST_CASE(4, 6, 30)
297+
```
298+
182299
### `unity_test_summary.rb`
183300
184301
A Unity test file contains one or more test case functions.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "unity.h"
2+
3+
/* Support for Meta Test Rig */
4+
#ifndef TEST_CASE
5+
#define TEST_CASE(...)
6+
#endif
7+
#ifndef TEST_RANGE
8+
#define TEST_RANGE(...)
9+
#endif
10+
11+
TEST_CASE(1, 2, 5)
12+
TEST_CASE(10, 7, 20)
13+
TEST_RANGE([3, 4, 1], [10, 5, -2], <30, 31, 1>)
14+
void test_demoParamFunction(int a, int b, int c)
15+
{
16+
TEST_ASSERT_GREATER_THAN_INT(a + b, c);
17+
}

0 commit comments

Comments
 (0)