You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: manual.markdown
+70-79Lines changed: 70 additions & 79 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -137,15 +137,26 @@ The failure of one of these macros causes the current test to immediately exit:
137
137
138
138
* CHECK(boolean condition) - checks any boolean result.
139
139
* CHECK_TEXT(boolean condition, text) - checks any boolean result and prints text on failure.
140
+
* CHECK_FALSE(condition) - checks any boolean result
141
+
* CHECK_FALSE_TEXT(condition, text) - checks any boolean result and prints text on failure.
140
142
* CHECK_EQUAL(expected, actual) - checks for equality between entities using ==. So if you have a class that supports operator==() you can use this macro to compare two instances. You will also need to add a StringFrom() function like those found in SimpleString. This is for printing the objects when the check failed.
141
143
* CHECK_THROWS(expected_exception, expression) - checks if expression throws expected_exception (e.g. std::exception). CHECK_THROWS is only available if CppUTest is built with the Standard C++ Library (default).
142
-
* STRCMP_EQUAL(expected, actual) - check const char* strings for equality using strcmp().
143
-
* LONGS_EQUAL(expected, actual) - Compares two numbers.
144
-
* BYTES_EQUAL(expected, actual) - Compares two numbers, eight bits wide.
145
-
* POINTERS_EQUAL(expected, actual) - Compares two pointers.
146
-
* DOUBLES_EQUAL(expected, actual, tolerance) - Compares two doubles within some tolerance
144
+
* STRCMP_EQUAL(expected, actual) - checks const char* strings for equality using strcmp().
145
+
* STRNCMP_EQUAL(expected, actual, length) - checks const char* strings for equality using strncmp().
146
+
* STRCMP_NOCASE_EQUAL(expected, actual) - checks const char* strings for equality, not considering case.
* LONGS_EQUAL(expected, actual) - compares two numbers.
149
+
* UNSIGNED_LONGS_EQUAL(expected, actual) - compares two positive numbers.
150
+
* BYTES_EQUAL(expected, actual) - compares two numbers, eight bits wide.
151
+
* POINTERS_EQUAL(expected, actual) - compares two pointers.
152
+
* DOUBLES_EQUAL(expected, actual, tolerance) - compares two floating point numbers within some tolerance
153
+
* FUNCTIONPOINTERS_EQUAL_TEXT(expected, actual, text) - compares two void (*)() function pointers
154
+
* MEMCMP_EQUAL(expected, actual, size) - compares two areas of memory
155
+
* BITS_EQUAL(expected, actual, mask) - compares expected to actual bit by bit, applying mask
147
156
* FAIL(text) - always fails
148
157
158
+
*NOTE* Many macros have _TEXT() equivalents, which are not explicitly listed here.
159
+
149
160
<aid="setup_teardown"> </a>
150
161
151
162
*CHECK_EQUAL Warning:*
@@ -222,16 +233,22 @@ The test execution of this will *likely* (no guarantee of order in CppUTest) be:
222
233
223
234
## Command line Switches
224
235
225
-
**-v* verbose, print each test name as it runs
226
236
**-c* colorize output, print green if OK, or red if failed
227
-
**-r#* repeat the tests some number (#) of times, or two times if # is not specified. This is handy if you are experiencing memory leaks. A second run that has no leaks indicates that someone is allocating statics and not releasing them.
228
-
**-g* group only run test whose group contains the substring group
229
-
**-sg* group only run test whose group exactly matches the string group
230
-
**-n* name only run test whose name contains the substring name
231
-
**-sn* name only run test whose name exactly matches the string name
232
-
**"TEST(group, name)"* only run test whose group and name matches the strings group and name. This can be used to copy-paste output from the -v option on the command line.
233
-
**-ojunit* output to JUnit ant plugin style xml files (for CI systems)
237
+
**-g group* only run test whose group contains the substring *group*
234
238
**-k* package name, Add a package name in JUnit output (for classification in CI systems)
239
+
**-lg* print a list of group names, separated by spaces
240
+
**-ln* print a list of test names in the form of *group.name*, separated by spaces
241
+
**-n name* only run test whose name contains the substring *name*
242
+
**-ojunit* output to JUnit ant plugin style xml files (for CI systems)
243
+
**-oteamcity* output to xml files (as the name suggests, for TeamCity)
244
+
**-p* run tests in a separate process.
245
+
**-r#* repeat the tests some number (#) of times, or twice if # is not specified. This is handy if you are experiencing memory leaks. A second run that has no leaks indicates that someone is allocating statics and not releasing them.
246
+
**-sg group* only run test whose group exactly matches the string *group*
247
+
**-sn name* only run test whose name exactly matches the string *name*
248
+
**-v* verbose, print each test name as it runs
249
+
**-xg group* exclude tests whose group contains the substring *group* (v3.8)
250
+
**-xn name* exclude tests whose name contains the substring *name* (v3.8)
251
+
**"TEST(group, name)"* only run test whose group and name matches the strings group and name. This can be used to copy-paste output from the -v option on the command line.
235
252
236
253
You can specify multiple -s|sg, -s|sn and "TEST(group, name)" parameters:
237
254
@@ -241,6 +258,27 @@ Specifying only test names with multiple -s|sn parameters will run all test
241
258
242
259
Mixing multiple -s|sg and -s|sn parameters (or using "TEST(group, name)" will only run tests whose groups match as well as their names.
243
260
261
+
Combining one -xg parameter with one -xn parameter will run only those tests that satisfy both criteria.
262
+
263
+
Combining -s|sg with -xn, or -s|sn with -xg will run only those tests that satisfy both criteria.
264
+
265
+
Specifying several -xg or -xn with each other or in other combinations has no effect.
266
+
267
+
*NOTE* Be careful with *-p*:
268
+
269
+
* Some systems do not support this feature, in which case tests will fail
270
+
with a suitable message.
271
+
* Using *-p* to run tests in a separate process can have unexpected side
272
+
effects.
273
+
* While running tests in a separate process can help to get more information
274
+
about an unexpected crash, when an expected crash is part of the test scenario,
275
+
the *-p* command line option should not be used, but running in a separate
276
+
process should be enabled on a per-test basis like this:
Examples for this can be found in CppUTests's own tests.
281
+
244
282
<aid="memory_leak_detection"> </a>
245
283
246
284
## Memory Leak Detection
@@ -353,73 +391,10 @@ Test plugins let you add a pre-action and a post-action to each test case. Plug
353
391
354
392
* Memory leak detector (provided)
355
393
* Pointer restore mechanism (provided) - helpful when tests overwrite a pointer that must be restored to its original value after the test. This is especially helpful when a pointer to a function is modified for test purposes.
356
-
* All Mutex's released - you could write a plugin that checks that any Mutexs or other shared resource is released before the test exists.
357
-
358
-
Example of a main with a SetPointerPlugin:
359
-
360
-
{% highlight c++ %}
361
-
int main(int ac, char** av)
362
-
{
363
-
TestRegistry* r = TestRegistry::getCurrentRegistry();
* IEEE754 Floating point exceptions (provided; v3.8) - automatically checks whether any floating point exception flags are set at the end of every test and if so, fails the test.
395
+
* All Mutex's released - you could write a plugin that checks that any Mutexs or other shared resource is released before the test exits.
Complete Documentation for provided plugins can be found on the [Plugins Manual](plugins_manual.html) page.
423
398
424
399
<aid="scripts"> </a>
425
400
@@ -553,6 +528,22 @@ int main(int ac, char** av)
553
528
554
529
You can leave out TEST_GROUP_C_SETUP() / TEST_GROUP_C_TEARDOWN() and TEST_GROUP_C_SETUP_WRAPPER() / TEST_GROUP_C_TEARDOWN_WRAPPER(), if you don't need them.
555
530
531
+
The following assertion macros are supported in the pure C interface:
Copy file name to clipboardExpand all lines: mocking_manual.markdown
+89-27Lines changed: 89 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,12 +21,14 @@ The main idea is to make manual mocking easier, rather than to make automated mo
21
21
*[Using Parameters](#parameters)
22
22
*[Objects as Parameters](#objects_as_parameters)
23
23
*[Output Parameters](#output_parameters)
24
+
*[Output Parameters Using Objects](#output_parameters_using_objects)
24
25
*[Return Values](#return_values)
25
26
*[Passing other data](#other_data)
26
27
*[Other MockSupport](#other_mock_support)
27
28
*[MockSupport Scope](#mock_scope)
28
-
*[MockPlugin](#mock_plugin)
29
+
*[MockSupportPlugin](#mock_plugin)
29
30
*[C Interface](#c_interface)
31
+
*[Miscellaneous](#miscellaneous)
30
32
31
33
<aid="simple_scenario"> </a>
32
34
@@ -99,6 +101,12 @@ If the call to productionCode wouldn't happen, then the test would fail with the
99
101
ACTUAL calls that did happen:
100
102
<none>
101
103
104
+
Sometimes you expect *several identical calls* to the same function, for example five calls to productionCode. There is a convenient shorthand for that situation:
105
+
106
+
{% highlight c++ %}
107
+
mock().expectNCalls(5, "productionCode");
108
+
{% endhighlight %}
109
+
102
110
<aid="objects"> </a>
103
111
104
112
### Using Objects
@@ -213,10 +221,10 @@ public:
213
221
214
222
The isEqual is called to compare the two parameters. The valueToString is called when an error message is printed and it needs to print the actual and expected values. If you want to use normal C functions, you can use the MockFunctionComparator which accepts pointers to functions in the constructor.
215
223
216
-
To remove the comparators, all you needs to do is call removeAllComparators, like:
224
+
To remove the comparators, all you needs to do is call removeAllComparatorsAndCopiers, like:
217
225
218
226
{% highlight c++ %}
219
-
mock().removeAllComparators();
227
+
mock().removeAllComparatorsAndCopiers();
220
228
{% endhighlight %}
221
229
222
230
Comparators sometimes lead to surprises, so a couple of warnings on its usage:
@@ -251,7 +259,7 @@ mock().expectOneCall("Foo").withOutputParameterReturning("bar", &outputValue, si
* When a char, int, etc. array is passed to withOutputParameter, you must use the generic withOutputParameterReturning and provide the actual size of the array or only one element will be copied.
289
297
298
+
<aid="output_parameters_using_objects"> </a>
299
+
300
+
### Output Parameters Using Objects
301
+
302
+
By far the best way to handle output parameters is by using a custom type copier (v3.8). The general principle is similar to the custom comparators described above:
When using withOutputParameterOfTypeReturning, the mocking framework needs to know how to copy the type and therefore a Copier has to be installed before using parameters of this type. This is done using installCopier, as below:
319
+
320
+
{% highlight c++ %}
321
+
MyTypeCopier copier;
322
+
mock().installCopier("myType", copier);
323
+
{% endhighlight %}
324
+
325
+
MyTypeCopier is a custom copier, which implements the MockNamedValueCopier interface. For example:
326
+
327
+
{% highlight c++ %}
328
+
class MyTypeCopier : public MockNamedValueCopier
329
+
{
330
+
public:
331
+
virtual void copy(void* out, const void* in)
332
+
{
333
+
*(MyType*)out = *(const MyType*)in;
334
+
}
335
+
};
336
+
{% endhighlight %}
337
+
338
+
To remove the copier, you need to call removeAllComparatorsAndCopiers, like:
339
+
340
+
{% highlight c++ %}
341
+
mock().removeAllComparatorsAndCopiers();
342
+
{% endhighlight %}
343
+
344
+
*Warning 1* and *Warning 2* above apply to copiers as well.
CppUTest plugins can be installed in the main and 'extent' the unit test framework. It is a place where you can put work that needs to be done in all unit tests. There is a MockPlugin to make the work with mocks easier. It does the following work:
453
-
454
-
* checkExpectations at the end of every test (on global scope, which goes recursive over all scopes)
455
-
* clear all expectations at the end of every test
456
-
* install all comparators that were configured in the plugin at the beginning of every test
457
-
* remove all comparators at the end of every test
506
+
### MockSupportPlugin
458
507
459
-
Installing the MockPlugin means you'll have to add to main something like:
This code creates a comparator for MyDummy and installs it at the plugin. This means the comparator is available for all test cases. It creates the plugin and installs it at the current test registry. After installing the plugin, you don't have to worry too much anymore about calling checkExpectations or cleaning your MockSupport.
508
+
There is a MockSupportPlugin to make the work with mocks easier. Complete Documentation for MockSupportPlugin can be found on the [Plugins Manual](plugins_manual.html) page.
473
509
474
510
<aid="c_interface"> </a>
475
511
@@ -481,7 +517,7 @@ Sometimes it is useful to access the mocking framework from a .c file rather tha
0 commit comments