PicoTest
A minimalist unit testing framework for C programs
Loading...
Searching...
No Matches
Test Cases

Test Case Hooks

PicoTest provides a way for client code to intercept test case events. This can be used for e.g. logging purpose or reporting.

typedef void PicoTestCaseEnterProc(const char *testName)
 Function signature of test case enter hooks.
typedef void PicoTestCaseLeaveProc(const char *testName, int fail)
 Function signature of test case leave hooks.
#define PICOTEST_CASE_ENTER_DEFAULT(testName)
 Default test case enter hook.
#define PICOTEST_CASE_ENTER
 Define the test case enter hook.
#define PICOTEST_CASE_LEAVE_DEFAULT(testName, fail)
 Default test case enter hook.
#define PICOTEST_CASE_LEAVE
 Define the test case leave hook.

Test Case Definitions

#define PICOTEST_CASE(...)
 Test case declaration.

Detailed Description

Test cases are the most elementary test functions. They are defined as simple functions blocks with assertions that checks the validity of the outcome.

Macro Definition Documentation

◆ PICOTEST_CASE

#define PICOTEST_CASE ( ...)

Test case declaration.

This macro defines a PicoTestProc of the given name that can be called directly.

Parameters
_testNameName of the test case.
_fixtureName(optional) Name of the test fixture used by the test.
_context(optional) Fixture context structure defined using PICOTEST_FIXTURE_CONTEXT(_fixtureName).
Returns
Number of failed tests.
Usage
#define FAILS 0
#define PASSES 1
/* Simple test with no fixture */
PICOTEST_CASE(testCase1) {
PICOTEST_VERIFY(FAILS); /* Logs error and continue */
PICOTEST_ASSERT(FAILS); /* Logs error and abort */
/* Unreached */
PICOTEST_ASSERT(PASSES);
}
/* Test with fixture but no context */
PICOTEST_CASE(testCase2, simpleFixture) {
PICOTEST_ASSERT(staticVar == 1); /* Passes */
PICOTEST_VERIFY(FAILS); /* Logs error and continue */
}
/* Test with fixture and context. Tests belonging to the same suite don't have
* to share the same suite. */
PICOTEST_CASE(testCase3, fixtureWithContext, context) {
PICOTEST_ASSERT(staticVar == 2); /* Passes */
PICOTEST_ASSERT(context->var1 == 1); /* Passes */
PICOTEST_VERIFY(context->var2 == NULL); /* Logs error and continue */
PICOTEST_ASSERT(context->var3 == NULL); /* Logs error and abort */
}
/* Another test using **simpleFixture**. Tests need not belong to the same suite
* to share a fixture. */
PICOTEST_CASE(testCase4, simpleFixture) {
PICOTEST_VERIFY(staticVar == 1); /* Passes */
PICOTEST_ASSERT(PASSES); /* Passes */
}
/* This test uses a fixture that requires a context. In this case the context
* passed to the fixture setup and teardown functions is NULL. */
PICOTEST_CASE(testCase5, fixtureWithOptionalContext) {
PICOTEST_VERIFY(staticVar == 3); /* Passes */
PICOTEST_ASSERT(PASSES); /* Passes */
}
Examples
mainSuite.inc Example of a simple PicoTest suite.
See also
PicoTestProc
PICOTEST_FIXTURE_CONTEXT

◆ PICOTEST_CASE_ENTER_DEFAULT

#define PICOTEST_CASE_ENTER_DEFAULT ( testName)

Default test case enter hook.

Does nothing.

See also
PicoTestCaseEnterProc
PICOTEST_CASE_ENTER

◆ PICOTEST_CASE_ENTER

#define PICOTEST_CASE_ENTER

Define the test case enter hook.

The default hook does nothing. Redefine this macro to use a custom hook, which must follow the PicoTestCaseEnterProc signature.

Note
Custom functions only apply to the tests defined after the macro redefinition. As macros can be redefined several times, this means that different functions may apply for the same source.
Usage
/* Hook declarations. */
PicoTestCaseEnterProc enterTestCase;
#undef PICOTEST_CASE_ENTER
#define PICOTEST_CASE_ENTER enterTestCase
/* Hook function. */
void enterTestCase(const char *testName) {
indent(level++);
printf("running test case %s...\n", testName);
}
Examples
hooks.c Example of PicoTest hooks, prints all events to stdout.
See also
PicoTestCaseEnterProc
PICOTEST_CASE_ENTER_DEFAULT
PICOTEST_CASE_LEAVE

◆ PICOTEST_CASE_LEAVE_DEFAULT

#define PICOTEST_CASE_LEAVE_DEFAULT ( testName,
fail )

Default test case enter hook.

Does nothing.

See also
PicoTestCaseLeaveProc
PICOTEST_CASE_LEAVE

◆ PICOTEST_CASE_LEAVE

#define PICOTEST_CASE_LEAVE

Define the test case leave hook.

The default hook does nothing. Redefine this macro to use a custom hook, which must follow the PicoTestCaseLeaveProc signature.

Note
Custom functions only apply to the tests defined after the macro redefinition. As macros can be redefined several times, this means that different functions may apply for the same source.
Usage
/* Hook declarations. */
PicoTestCaseLeaveProc leaveTestCase;
#undef PICOTEST_CASE_LEAVE
#define PICOTEST_CASE_LEAVE leaveTestCase
/* Hook function. */
void leaveTestCase(const char *testName, int fail) {
indent(--level);
printf("test case %s done (%d failures)\n", testName, fail);
}
Examples
hooks.c Example of PicoTest hooks, prints all events to stdout.
See also
PicoTestCaseLeaveProc
PICOTEST_CASE_LEAVE_DEFAULT
PICOTEST_CASE_ENTER

Typedef Documentation

◆ PicoTestCaseEnterProc

typedef void PicoTestCaseEnterProc(const char *testName)

Function signature of test case enter hooks.

Called before running the test case.

Parameters
testNameTest case name.
Usage
/* Hook declarations. */
PicoTestCaseEnterProc enterTestCase;
#undef PICOTEST_CASE_ENTER
#define PICOTEST_CASE_ENTER enterTestCase
/* Hook function. */
void enterTestCase(const char *testName) {
indent(level++);
printf("running test case %s...\n", testName);
}
Examples
hooks.c Example of PicoTest hooks, prints all events to stdout.
See also
PICOTEST_CASE_ENTER

◆ PicoTestCaseLeaveProc

typedef void PicoTestCaseLeaveProc(const char *testName, int fail)

Function signature of test case leave hooks.

Called after running the test case.

Parameters
testNameTest case name.
failFailed tests (including its subtests if any).
Usage
/* Hook declarations. */
PicoTestCaseLeaveProc leaveTestCase;
#undef PICOTEST_CASE_LEAVE
#define PICOTEST_CASE_LEAVE leaveTestCase
/* Hook function. */
void leaveTestCase(const char *testName, int fail) {
indent(--level);
printf("test case %s done (%d failures)\n", testName, fail);
}
Examples
hooks.c Example of PicoTest hooks, prints all events to stdout.
See also
PICOTEST_CASE_LEAVE