1 |
=head1 Unit testing |
2 |
|
3 |
Ermyth provides a simple yet powerful and specialised unit testing framework. |
4 |
Tests are grouped by putting them in files. For example all tests regarding |
5 |
metadata go into F<test/metadata.C>. Tests are compiled to .t files, which are |
6 |
simple executables that get run by make test. |
7 |
|
8 |
=head2 Test boilerplate |
9 |
|
10 |
The very first line each testgroup should be |
11 |
|
12 |
#include "unit/test.h" |
13 |
|
14 |
and the very last line should be |
15 |
|
16 |
#include "unit/runtest.h" |
17 |
|
18 |
=head2 Defining tests |
19 |
|
20 |
Defining tests is very simple: |
21 |
|
22 |
test<number> () |
23 |
{ |
24 |
// do something here |
25 |
} |
26 |
|
27 |
=head2 Assertions |
28 |
|
29 |
Ermyth's unit test framework provides five assertions: |
30 |
|
31 |
=over |
32 |
|
33 |
=item ensure (expr) |
34 |
|
35 |
Writes an error if C<expr> does not evaluate to a true value. |
36 |
|
37 |
=item ensure_streq (str, expected) |
38 |
|
39 |
Checks with strcmp whether C<str> and C<expected> are equal. |
40 |
Both C<str> and C<expected> need to be one of C<char *>, |
41 |
C<char const *>, C<char const * const> or an C<unsigned> variant of one. |
42 |
|
43 |
=item ensure_null (expr) |
44 |
|
45 |
Checks whether expr is NULL or 0. |
46 |
This check requires C<expr> to be |
47 |
L<EqualityComparable|http://www.generic-programming.org/languages/conceptcpp/concept_web.php?header=concepts#EqualityComparable>. |
48 |
|
49 |
=item ensure_equals (actual, expected) |
50 |
|
51 |
Checks whether C<actual> evaluates to C<expected>. |
52 |
This check requires C<actual> and C<expected> to be |
53 |
L<EqualityComparable|http://www.generic-programming.org/languages/conceptcpp/concept_web.php?header=concepts#EqualityComparable>. |
54 |
|
55 |
=item ensure_distance (actual, expected, tolerance) |
56 |
|
57 |
Checks whether C<actual> is at most C<tolerance> away from C<expected>. |
58 |
To allow this comparison, C<actual> and C<tolerance> need to be |
59 |
L<Addable|http://www.generic-programming.org/languages/conceptcpp/concept_web.php?header=concepts#Addable> |
60 |
and their sum together with C<expected> needs to be |
61 |
L<LessThanComparable|http://www.generic-programming.org/languages/conceptcpp/concept_web.php?header=concepts#LessThanComparable>. |
62 |
|
63 |
|
64 |
=head2 Example |
65 |
|
66 |
The following example can be found in F<test/metadata.C>: |
67 |
|
68 |
#include "unit/test.h" |
69 |
|
70 |
test<1> () |
71 |
{ |
72 |
metadata *md; |
73 |
myuser_t *mu = myuser_add ("name", "pass", "email", 0); |
74 |
|
75 |
mu->add_metadata ("cow", "moo"); |
76 |
ensure_streq (md->value, "moo"); |
77 |
mu->del_metadata ("cow"); |
78 |
mu->add_metadata ("cow2", "moo2"); |
79 |
mu->clear_metadata (); |
80 |
ensure_null (md); |
81 |
} |
82 |
|
83 |
#include "unit/runtest.h" |