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

Assertion Hooks

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

typedef void PicoTestAssertBeforeProc(const char *type, const char *test)
 Function signature of assert before hooks.
typedef void PicoTestAssertAfterProc(const char *type, const char *test, int fail)
 Function signature of assert after hooks.
#define PICOTEST_ASSERT_BEFORE_DEFAULT(type, test)
 Default assert before hook.
#define PICOTEST_ASSERT_BEFORE
 Define the assert before hook.
#define PICOTEST_ASSERT_AFTER_DEFAULT(type, test, fail)
 Default assert after hook.
#define PICOTEST_ASSERT_AFTER
 Define the assert after hook.

Assertion Definitions

#define PICOTEST_ASSERT(x, ...)
 Hard assertion.
#define PICOTEST_VERIFY(x, ...)
 Soft assertion.
#define PICOTEST_FAILURE(type, test, ...)
 Generic failure.
#define PICOTEST_ABORT()
 Abort a test case.

Detailed Description

Assertions are the basic building blocks of test cases.

Macro Definition Documentation

◆ PICOTEST_ASSERT

#define PICOTEST_ASSERT ( x,
... )

Hard assertion.

Logs an error if the given value is false, then stops the test with PICOTEST_ABORT().

PICOTEST_FAILURE_LOGGER() is called with the type argument set to "ASSERT".

Parameters
xValue to test. Evaluated once, so it can be an expression with side effects.
msg(optional) Message format string.
...(optional) Message string arguments.
Note
msg and following arguments arguments are suitable arguments to printf().
Examples
mainSuite.inc Example of a simple PicoTest suite.
See also
PICOTEST_FAILURE_LOGGER
PICOTEST_ABORT
PICOTEST_VERIFY

◆ PICOTEST_VERIFY

#define PICOTEST_VERIFY ( x,
... )

Soft assertion.

Logs an error if the given value is false, but let the test continue.

PICOTEST_FAILURE_LOGGER() is called with the type argument set to "VERIFY".

Parameters
xValue to test. Evaluated once, so it can be an expression with side effects.
msg(optional) Message format string.
...(optional) Message string arguments.
Note
msg and following arguments arguments are suitable arguments to printf().
Examples
mainSuite.inc Example of a simple PicoTest suite.
See also
PICOTEST_FAILURE_LOGGER
PICOTEST_ASSERT

◆ PICOTEST_FAILURE

#define PICOTEST_FAILURE ( type,
test,
... )

Generic failure.

PICOTEST_FAILURE_LOGGER() is called with the provided type, test and msg arguments.

This can be used to implement custom testing logic.

Parameters
typeType of test that failed (e.g. "ASSERT").
testFailed test.
msg(optional) Message format string.
...(optional) Message string arguments.

◆ PICOTEST_ABORT

#define PICOTEST_ABORT ( )

Abort a test case.

This can be used to implement custom testing logic.

See also
PICOTEST_CASE

◆ PICOTEST_ASSERT_BEFORE_DEFAULT

#define PICOTEST_ASSERT_BEFORE_DEFAULT ( type,
test )

Default assert before hook.

Does nothing.

See also
PicoTestAssertBeforeProc
PICOTEST_ASSERT_BEFORE

◆ PICOTEST_ASSERT_BEFORE

#define PICOTEST_ASSERT_BEFORE

Define the assert before hook.

The default hook does nothing. Redefine this macro to use a custom hook, which must follow the PicoTestAssertBeforeProc 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. */
#undef PICOTEST_ASSERT_BEFORE
#define PICOTEST_ASSERT_BEFORE beforeAssert
/* Hook function. */
void beforeAssert(const char *type, const char *test) {
indent(level++);
printf("before assertion %s(%s)...\n", type, test);
}
Examples
hooks.c Example of PicoTest hooks, prints all events to stdout.
See also
PicoTestAssertBeforeProc
PICOTEST_ASSERT_BEFORE_DEFAULT
PICOTEST_ASSERT_AFTER

◆ PICOTEST_ASSERT_AFTER_DEFAULT

#define PICOTEST_ASSERT_AFTER_DEFAULT ( type,
test,
fail )

Default assert after hook.

Does nothing.

See also
PicoTestAssertAfterProc
PICOTEST_ASSERT_AFTER

◆ PICOTEST_ASSERT_AFTER

#define PICOTEST_ASSERT_AFTER

Define the assert after hook.

The default hook does nothing. Redefine this macro to use a custom hook, which must follow the PicoTestAssertAfterProc 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. */
#undef PICOTEST_ASSERT_AFTER
#define PICOTEST_ASSERT_AFTER afterAssert
/* Hook function. */
void afterAssert(const char *type, const char *test, int fail) {
indent(--level);
printf("after assertion %s(%s) => %s\n", type, test, fail ? "fails" : "passes");
}
Examples
hooks.c Example of PicoTest hooks, prints all events to stdout.
See also
PicoTestAssertAfterProc
PICOTEST_ASSERT_AFTER_DEFAULT
PICOTEST_ASSERT_BEFORE

Typedef Documentation

◆ PicoTestAssertBeforeProc

typedef void PicoTestAssertBeforeProc(const char *type, const char *test)

Function signature of assert before hooks.

Called before running an assertion.

Parameters
typeType of test (e.g. "ASSERT").
testTest.
Usage
/* Hook declarations. */
#undef PICOTEST_ASSERT_BEFORE
#define PICOTEST_ASSERT_BEFORE beforeAssert
/* Hook function. */
void beforeAssert(const char *type, const char *test) {
indent(level++);
printf("before assertion %s(%s)...\n", type, test);
}
Examples
hooks.c Example of PicoTest hooks, prints all events to stdout.
See also
PICOTEST_ASSERT_BEFORE

◆ PicoTestAssertAfterProc

typedef void PicoTestAssertAfterProc(const char *type, const char *test, int fail)

Function signature of assert after hooks.

Called after running an assertion.

Parameters
typeType of test (e.g. "ASSERT").
testTest.
failTest result: zero for success, non-zero for failure.
Usage
/* Hook declarations. */
#undef PICOTEST_ASSERT_AFTER
#define PICOTEST_ASSERT_AFTER afterAssert
/* Hook function. */
void afterAssert(const char *type, const char *test, int fail) {
indent(--level);
printf("after assertion %s(%s) => %s\n", type, test, fail ? "fails" : "passes");
}
Examples
hooks.c Example of PicoTest hooks, prints all events to stdout.
See also
PICOTEST_ASSERT_AFTER