Skip to content
Vojtech Horky edited this page May 30, 2014 · 9 revisions

PCUT: Plain C unit testing

PCUT is a very simple library (framework) for unit testing of C code. PCUT itself is written in plain C and the main target when writing the library was easiness of use.

Briefly, the library provides the test running/evaluating framework and macros to simplify writing the tests: basically just functions with asserts. Following functionality is available:

  • grouping tests into test-suites
  • set-up and tear-down functions for the whole suite
  • assert-like functions for comparing common types with detailed messages
  • Test-Anything-Protocol output
  • standard output capturing
  • pretty safe against crashing tests (such as null pointer dereference)

The library is developed on Linux but it shall run in any decent environment with standard C library. Running under Windows was tested through MinGW only.

Short example follows, if you want to learn more, following pages might help you.

Example

Below is complete source code for a test checking (lamely) the function atoi().

#include <pcut/pcut.h>
#include <stdlib.h>

PCUT_INIT

PCUT_TEST(atoi_zero) {
	PCUT_ASSERT_INT_EQUALS(0, atoi("0"));
}

PCUT_TEST(atoi_positive) {
	PCUT_ASSERT_INT_EQUALS(42, atoi("42"));
}

PCUT_TEST(atoi_negative) {
	PCUT_ASSERT_INT_EQUALS(-273, atoi("-273"));
}

PCUT_MAIN()

There is no need to preprocess this source code. Just compile/link it against libpcut and it would produce executable that would run these 3 tests. The output produced would look like this.

1..3
#> Starting suite Default.
ok 1 atoi_zero
ok 2 atoi_positive
ok 3 atoi_negative
#> Finished suite Default (failed 0 of 3).
Clone this wiki locally