| 1 |
<meta charset="utf-8"><link rel="stylesheet" href="apidoc.css"/> |
| 2 |
|
| 3 |
**µTest — A small C testing library** |
| 4 |
|
| 5 |
* [Main](./mutest.md.html) |
| 6 |
|
| 7 |
## Hooks |
| 8 |
|
| 9 |
µTest allows calling custom functions before and after each test suite, |
| 10 |
as well as before and after each specification. |
| 11 |
|
| 12 |
Typically, these functions are used to perform environment checks that |
| 13 |
may lead to different testing conditions; or to set up mock objects and |
| 14 |
state that will be used in the specifications and expectations. For |
| 15 |
instance, a hook called before all specifications can set up a server |
| 16 |
connection, and a hook called after all specifications can close it |
| 17 |
gracefully, instead of relying on the end of the process. |
| 18 |
|
| 19 |
Additionally, you can use hook functions to decide whether or not a suite |
| 20 |
or a specification need to run at all; in the case above, if the server |
| 21 |
connection is not available, you could decide to skip the whole test suite |
| 22 |
without necessarily failing the test. |
| 23 |
|
| 24 |
### Includes |
| 25 |
|
| 26 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 27 |
#include <mutest.h> |
| 28 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 29 |
|
| 30 |
### Functions |
| 31 |
|
| 32 |
#### `mutest_before` |
| 33 |
|
| 34 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 35 |
void |
| 36 |
mutest_before (mutest_hook_func_t hook); |
| 37 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 38 |
|
| 39 |
Sets the function to be called once before a suite |
| 40 |
defined by `mutest_describe()`. |
| 41 |
|
| 42 |
For instance: |
| 43 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 44 |
static void |
| 45 |
before_func (void) |
| 46 |
{ |
| 47 |
} |
| 48 |
|
| 49 |
static void |
| 50 |
hooks_suite (mutest_suite_t *suite) |
| 51 |
{ |
| 52 |
// test cases |
| 53 |
} |
| 54 |
|
| 55 |
MUTEST_MAIN ( |
| 56 |
// runs once before all tests cases |
| 57 |
mutest_before (before_func); |
| 58 |
|
| 59 |
mutest_describe ("hooks", hooks_suite); |
| 60 |
) |
| 61 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 62 |
|
| 63 |
hook |
| 64 |
: a `mutest_hook_func_t` function |
| 65 |
|
| 66 |
---- |
| 67 |
|
| 68 |
#### `mutest_after` |
| 69 |
|
| 70 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 71 |
void |
| 72 |
mutest_after (mutest_hook_func_t hook); |
| 73 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 74 |
|
| 75 |
Sets the function to be called once after a suite |
| 76 |
defined by `mutest_describe()`. |
| 77 |
|
| 78 |
For instance: |
| 79 |
|
| 80 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 81 |
static void |
| 82 |
after_func (void) |
| 83 |
{ |
| 84 |
} |
| 85 |
|
| 86 |
static void |
| 87 |
hooks_suite (mutest_suite_t *suite) |
| 88 |
{ |
| 89 |
// test cases |
| 90 |
} |
| 91 |
|
| 92 |
MUTEST_MAIN ( |
| 93 |
// runs once after all tests cases |
| 94 |
mutest_after (after_func); |
| 95 |
|
| 96 |
mutest_describe ("hooks", hooks_suite); |
| 97 |
) |
| 98 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 99 |
|
| 100 |
hook |
| 101 |
: a `mutest_hook_func_t` function |
| 102 |
|
| 103 |
---- |
| 104 |
|
| 105 |
#### `mutest_before_each` |
| 106 |
|
| 107 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 108 |
void |
| 109 |
mutest_before_each (mutest_hook_func_t hook); |
| 110 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 111 |
|
| 112 |
Sets the function to be called before every specification |
| 113 |
defined by `mutest_it()`. |
| 114 |
|
| 115 |
For instance: |
| 116 |
|
| 117 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 118 |
static void |
| 119 |
before_each_func (void) |
| 120 |
{ |
| 121 |
} |
| 122 |
|
| 123 |
static void |
| 124 |
hooks_suite (mutest_suite_t *suite) |
| 125 |
{ |
| 126 |
// runs before each tests case |
| 127 |
mutest_before_each (before_each_func); |
| 128 |
|
| 129 |
// test cases |
| 130 |
} |
| 131 |
|
| 132 |
MUTEST_MAIN ( |
| 133 |
mutest_describe ("hooks", hooks_suite); |
| 134 |
) |
| 135 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 136 |
|
| 137 |
hook |
| 138 |
: a `mutest_hook_func_t` function |
| 139 |
|
| 140 |
---- |
| 141 |
|
| 142 |
#### `mutest_after_each` |
| 143 |
|
| 144 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 145 |
void |
| 146 |
mutest_after_each (mutest_hook_func_t hook); |
| 147 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 148 |
|
| 149 |
Sets the function to be called after each specification |
| 150 |
defined by `mutest_it()`. |
| 151 |
|
| 152 |
For instance: |
| 153 |
|
| 154 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 155 |
static void |
| 156 |
after_each_func (void) |
| 157 |
{ |
| 158 |
} |
| 159 |
|
| 160 |
static void |
| 161 |
hooks_suite (mutest_suite_t *suite) |
| 162 |
{ |
| 163 |
// runs after each tests case |
| 164 |
mutest_after_each (after_each_func); |
| 165 |
|
| 166 |
// test cases |
| 167 |
} |
| 168 |
|
| 169 |
MUTEST_MAIN ( |
| 170 |
mutest_describe ("hooks", hooks_suite); |
| 171 |
) |
| 172 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 173 |
|
| 174 |
hook |
| 175 |
: a `mutest_hook_func_t` function |
| 176 |
|
| 177 |
### Types |
| 178 |
|
| 179 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 180 |
typedef void (* mutest_hook_func_t) (void) |
| 181 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 182 |
|
| 183 |
The prototype of a function to pass to `mutest_before()`, `mutest_before_each()`, |
| 184 |
`mutest_after_each()`, or `mutest_after()`. |
| 185 |
|
| 186 |
<style class="fallback">body{visibility:hidden}</style><script>markdeepOptions={tocStyle:'medium'};</script> |
| 187 |
<!-- Markdeep: --><script src="markdeep.min.js" charset="utf-8"></script> |