1 | PCUT: Plain C Unit Testing mini-framework
|
---|
2 | =========================================
|
---|
3 |
|
---|
4 | PCUT is a very simple framework for unit testing of C code.
|
---|
5 | Unlike many other frameworks where you need to specify manually which
|
---|
6 | functions belong to a particular test, PCUT provides several smart
|
---|
7 | macros that hides this and lets you focus on the most important
|
---|
8 | part of testing only: that is, coding the test cases.
|
---|
9 |
|
---|
10 | This mini-framework is definitely not complete but it offers the basic
|
---|
11 | functionality needed for writing unit tests.
|
---|
12 | This includes the possibility to group tests into test suites, optionally
|
---|
13 | having set-up and tear-down functions.
|
---|
14 | There are several assert macros for evaluating the results, their highlight
|
---|
15 | is very detailed information about the problem.
|
---|
16 |
|
---|
17 | The output of the test can come in two forms: either as an XML output suited
|
---|
18 | for later processing or in the form of Test-Anything-Protocol.
|
---|
19 | PCUT is able to capture standard output and display it together with test
|
---|
20 | results.
|
---|
21 | And by running each test in a separate process, the whole framework is pretty
|
---|
22 | safe against unexpected crashes, such as null pointer dereference.
|
---|
23 |
|
---|
24 | More details can be found on PCUT wiki on GitHub.
|
---|
25 |
|
---|
26 | https://github.com/vhotspur/pcut/wiki
|
---|
27 |
|
---|
28 |
|
---|
29 | Main goal - simple to use
|
---|
30 | -------------------------
|
---|
31 |
|
---|
32 | Let's illustrate how PCUT aims to be simple when creating unit tests for
|
---|
33 | function ``intmin`` that ought to return smaller of its two arguments.::
|
---|
34 |
|
---|
35 | int intmin(int a, int b) {
|
---|
36 | return a > b ? b : a;
|
---|
37 | }
|
---|
38 |
|
---|
39 | Individual test-cases for such method could cover following cases: getting
|
---|
40 | minimal of
|
---|
41 |
|
---|
42 | - two same numbers
|
---|
43 | - negative and positive number
|
---|
44 | - two negative numbers
|
---|
45 | - two positive numbers
|
---|
46 | - "corner case" numbers, such as minimal and maximal represented number
|
---|
47 |
|
---|
48 | In PCUT, that would be translated to the following code:::
|
---|
49 |
|
---|
50 | #include <pcut/test.h>
|
---|
51 | #include <limits.h>
|
---|
52 | /* Other include to have declaration of intmin */
|
---|
53 |
|
---|
54 | PCUT_INIT
|
---|
55 |
|
---|
56 | PCUT_TEST_SUITE(intmin_tests);
|
---|
57 |
|
---|
58 | PCUT_TEST(same_number) {
|
---|
59 | PCUT_ASSERT_INT_EQUALS(719, intmin(719, 719) );
|
---|
60 | PCUT_ASSERT_INT_EQUALS(-4589, intmin(-4589, -4589) );
|
---|
61 | }
|
---|
62 |
|
---|
63 | PCUT_TEST(positive_and_negative) {
|
---|
64 | PCUT_ASSERT_INT_EQUALS(-5, intmin(-5, 71) );
|
---|
65 | PCUT_ASSERT_INT_EQUALS(-17, intmin(423, -17) );
|
---|
66 | }
|
---|
67 |
|
---|
68 | PCUT_TEST(same_sign) {
|
---|
69 | PCUT_ASSERT_INT_EQUALS(22, intmin(129, 22) );
|
---|
70 | PCUT_ASSERT_INT_EQUALS(-37, intmin(-37, -1) );
|
---|
71 | }
|
---|
72 |
|
---|
73 | PCUT_TEST(corner_cases) {
|
---|
74 | PCUT_ASSERT_INT_EQUALS(INT_MIN, intmin(INT_MIN, -1234) );
|
---|
75 | PCUT_ASSERT_INT_EQUALS(9876, intmin(9876, INT_MAX) );
|
---|
76 | }
|
---|
77 |
|
---|
78 | PCUT_MAIN()
|
---|
79 |
|
---|
80 | And that's all.
|
---|
81 | You do not need to manually specify which tests to run etc.,
|
---|
82 | everything is done magically via the ``PCUT_INIT``, ``PCUT_MAIN`` and
|
---|
83 | ``PCUT_TEST`` macros.
|
---|
84 | All you need to do is to compile this code and link it with ``libpcut``.
|
---|
85 | Result of the linking would be an executable that runs the tests and
|
---|
86 | reports the results.
|
---|
87 |
|
---|
88 |
|
---|
89 | Examples
|
---|
90 | --------
|
---|
91 |
|
---|
92 | More examples, in the form of self-tests, are available in the ``tests/``
|
---|
93 | subdirectory.
|
---|
94 |
|
---|
95 |
|
---|
96 | Building and installing
|
---|
97 | -----------------------
|
---|
98 |
|
---|
99 | On Unix systems, running ``make`` and ``make install`` shall do the job.
|
---|
100 |
|
---|
101 | More details can be found on https://github.com/vhotspur/pcut/wiki/Building.
|
---|