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 | https://github.com/vhotspur/pcut/wiki
|
---|
26 |
|
---|
27 |
|
---|
28 | Quick-start example
|
---|
29 | -------------------
|
---|
30 |
|
---|
31 | The following code tests the standard ``atoi`` function::
|
---|
32 |
|
---|
33 | #include <pcut/pcut.h>
|
---|
34 | #include <stdlib.h>
|
---|
35 |
|
---|
36 | PCUT_INIT
|
---|
37 |
|
---|
38 | PCUT_TEST(atoi_zero) {
|
---|
39 | PCUT_ASSERT_INT_EQUALS(0, atoi("0"));
|
---|
40 | }
|
---|
41 |
|
---|
42 | PCUT_TEST(atoi_positive) {
|
---|
43 | PCUT_ASSERT_INT_EQUALS(42, atoi("42"));
|
---|
44 | }
|
---|
45 |
|
---|
46 | PCUT_TEST(atoi_negative) {
|
---|
47 | PCUT_ASSERT_INT_EQUALS(-273, atoi("-273"));
|
---|
48 | }
|
---|
49 |
|
---|
50 | PCUT_MAIN()
|
---|
51 |
|
---|
52 | As you can see, there is no manual listing of tests that form the test
|
---|
53 | suite etc, only the tests and ``PCUT_INIT`` at the beginning and
|
---|
54 | ``PCUT_MAIN`` at the end.
|
---|
55 |
|
---|
56 | This code has to be linked with ``libpcut`` to get an executable that runs
|
---|
57 | the tests and reports the results.
|
---|
58 |
|
---|
59 | More examples, in the form of self-tests, are available in the ``tests/``
|
---|
60 | subdirectory.
|
---|
61 | Other examples can be found on the Wiki.
|
---|
62 |
|
---|
63 |
|
---|
64 | Building and installing
|
---|
65 | -----------------------
|
---|
66 |
|
---|
67 | PCUT uses CMake (http://www.cmake.org/).
|
---|
68 | On Unix systems, following commands build the library and execute the
|
---|
69 | built-in tests::
|
---|
70 |
|
---|
71 | mkdir build
|
---|
72 | cd build
|
---|
73 | cmake .. && make all test
|
---|
74 |
|
---|
75 | More details can be found on https://github.com/vhotspur/pcut/wiki/Building.
|
---|