docs/Writerside/topics/test.h.md

changeset 1277
637d4775e79e
parent 1174
ee473780cc0d
equal deleted inserted replaced
1276:91a64ba395f7 1277:637d4775e79e
20 In UCX this is realized by the `CX_TEST_DO` macro with which you can define a scope for the actual test. 20 In UCX this is realized by the `CX_TEST_DO` macro with which you can define a scope for the actual test.
21 Whenever a test assertion fails, the scope is exited and the tear down code is executed. 21 Whenever a test assertion fails, the scope is exited and the tear down code is executed.
22 22
23 <tabs> 23 <tabs>
24 <tab title="Test"> 24 <tab title="Test">
25 <code-block lang="C"> 25 <code-block lang="C"><![CDATA[
26 CX_TEST(test_foo) { 26 CX_TEST(test_foo) {
27 struct mystruct *x = malloc(sizeof(*x)); 27 struct mystruct *x = malloc(sizeof(*x));
28 x->d = 42; 28 x->d = 42;
29 CX_TEST_DO { 29 CX_TEST_DO {
30 int y = foo(x); 30 int y = foo(x);
36 CX_TEST_ASSERTM(y == 42, 36 CX_TEST_ASSERTM(y == 42,
37 "foo does not return correct value"); 37 "foo does not return correct value");
38 } 38 }
39 free(x); 39 free(x);
40 } 40 }
41 </code-block> 41 ]]></code-block>
42 </tab> 42 </tab>
43 <tab title="Implementation"> 43 <tab title="Implementation">
44 <code-block lang="C"> 44 <code-block lang="C"><![CDATA[
45 struct mystruct { 45 struct mystruct {
46 int d; 46 int d;
47 }; 47 };
48 48
49 int foo(struct mystruct *s) { 49 int foo(struct mystruct *s) {
50 return s->d; 50 return s->d;
51 } 51 }
52 </code-block> 52 ]]></code-block>
53 </tab> 53 </tab>
54 </tabs> 54 </tabs>
55 55
56 Once you have registered all test cases, you can run the test suite with `cx_test_run()` 56 Once you have registered all test cases, you can run the test suite with `cx_test_run()`
57 or one of the convenience macros. 57 or one of the convenience macros.
91 For parameterized testing you can define and call test subroutines. 91 For parameterized testing you can define and call test subroutines.
92 It is _not_ possible to call arbitrary functions and use the `CX_TEST_ASSERT` macro. 92 It is _not_ possible to call arbitrary functions and use the `CX_TEST_ASSERT` macro.
93 Instead, you define a callable test subroutine with `CX_TEST_SUBROUTINE` and call it with `CX_TEST_CALL_SUBROUTINE`. 93 Instead, you define a callable test subroutine with `CX_TEST_SUBROUTINE` and call it with `CX_TEST_CALL_SUBROUTINE`.
94 The following example illustrates this with an adaption of the above test case. 94 The following example illustrates this with an adaption of the above test case.
95 95
96 <code-block lang="C"> 96 <code-block lang="C"><![CDATA[
97 CX_TEST_SUBROUTINE(test_foo_params, struct mystruct *x, int d) { 97 CX_TEST_SUBROUTINE(test_foo_params, struct mystruct *x, int d) {
98 x->d = d; 98 x->d = d;
99 CX_TEST_ASSERT(foo(x) == d); 99 CX_TEST_ASSERT(foo(x) == d);
100 } 100 }
101 101
106 CX_TEST_CALL_SUBROUTINE(test_foo_params, x, -42); 106 CX_TEST_CALL_SUBROUTINE(test_foo_params, x, -42);
107 CX_TEST_CALL_SUBROUTINE(test_foo_params, x, 1337); 107 CX_TEST_CALL_SUBROUTINE(test_foo_params, x, 1337);
108 } 108 }
109 free(x); 109 free(x);
110 } 110 }
111 </code-block> 111 ]]></code-block>
112 112
113 > Any test function, test case or test subroutine, is a normal C function. 113 > Any test function, test case or test subroutine, is a normal C function.
114 > As such you can decide to make them `static` when you do not want external linkage. 114 > As such you can decide to make them `static` when you do not want external linkage.
115 > For example, just define the test as `static CX_TEST(test_foo)` instead of `CX_TEST(test_foo)`. 115 > For example, just define the test as `static CX_TEST(test_foo)` instead of `CX_TEST(test_foo)`.
116 116

mercurial