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

Creating test suites

With PCUT it is possible to split the tests into different groups, called test suites. It is then possible to launch the suites separately but the main reason for creating the suites is to keep your tests organized and to allow using set-up and tear-down procedures.

Creating a suite

To create a new suite, use the PCUT_TEST_SUITE macro. It has a single identifier argument describing the suite. All tests after this macro (prior another invocation of this macro) belong to a new suite. Short example follows:

#include <pcut/pcut.h>
PCUT_INIT

PCUT_TEST_SUITE(Suite_1);

PCUT_TEST(test_one) { }
PCUT_TEST(test_two) { }

PCUT_MAIN()

Adding the test suite will slightly change the output but for single suite there is no other difference:

1..2
#> Starting suite Suite_1.
ok 1 test_one
ok 2 test_two
#> Finished suite Suite_1 (failed 0 of 2).

Set-up and tear-down routines

Suites start to be interesting when we need to execute some code before the actual test is run or after each test.

TODO: describe in more detail

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

PCUT_INIT

static char *buffer = NULL;
#define BUFFER_SIZE 512

PCUT_TEST_SUITE(snprintf)

PCUT_TEST_BEFORE {
	buffer = malloc(BUFFER_SIZE);
	PCUT_ASSERT_NOT_NULL(buffer);
}

PCUT_TEST_AFTER {
	free(buffer);
	buffer = NULL;
}

PCUT_TEST(snprintf) {
	snprintf(buffer, BUFFER_SIZE - 1, "%d-%s", 56, "abcd");
	PCUT_ASSERT_STR_EQUALS("56-abcd", buffer);
}

PCUT_TEST_SUITE(no_setup_or_teardown)

PCUT_TEST(whatever) {
	PCUT_ASSERT_NULL(buffer);
}

PCUT_MAIN()

Clone this wiki locally