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

Test Fixture Hooks

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

typedef void PicoTestFixtureBeforeSetupProc(const char *fixtureName, const char *testName)
 Function signature of test fixture before setup hooks.
typedef void PicoTestFixtureAfterSetupProc(const char *fixtureName, const char *testName)
 Function signature of test fixture after setup hooks.
typedef void PicoTestFixtureBeforeTeardownProc(const char *fixtureName, const char *testName, int fail)
 Function signature of test fixture before teardown hooks.
typedef void PicoTestFixtureAfterTeardownProc(const char *fixtureName, const char *testName, int fail)
 Function signature of test fixture after teardown hooks.
#define PICOTEST_FIXTURE_BEFORE_SETUP_DEFAULT(fixtureName, testName)
 Default test fixture before setup hook.
#define PICOTEST_FIXTURE_BEFORE_SETUP
 Define the test fixture before setup hook.
#define PICOTEST_FIXTURE_AFTER_SETUP_DEFAULT(fixtureName, testName)
 Default test fixture after setup hook.
#define PICOTEST_FIXTURE_AFTER_SETUP
 Define the test fixture after setup hook.
#define PICOTEST_FIXTURE_BEFORE_TEARDOWN_DEFAULT(fixtureName, testName, fail)
 Default test fixture before teardown hook.
#define PICOTEST_FIXTURE_BEFORE_TEARDOWN
 Define the test fixture before teardown hook.
#define PICOTEST_FIXTURE_AFTER_TEARDOWN_DEFAULT(fixtureName, testName, fail)
 Default test fixture after teardown hook.
#define PICOTEST_FIXTURE_AFTER_TEARDOWN
 Define the test fixture after teardown hook.

Test Fixture Definitions

#define PICOTEST_FIXTURE_CONTEXT(_fixtureName)
 Test fixture context declaration.
#define PICOTEST_FIXTURE_SETUP(...)
 Test fixture initialization.
#define PICOTEST_FIXTURE_TEARDOWN(...)
 Test fixture cleanup.

Detailed Description

Test fixtures are used to establish the context needed to run test cases.

A test fixture can be used by several, possibly unrelated test cases.

Macro Definition Documentation

◆ PICOTEST_FIXTURE_CONTEXT

#define PICOTEST_FIXTURE_CONTEXT ( _fixtureName)

Test fixture context declaration.

Fixtures can optionally define a context structure that is passed to its setup and teardown functions.

Parameters
_fixtureNameName of the fixture.
Usage
/* Fixture context */
PICOTEST_FIXTURE_CONTEXT(fixtureWithContext) {
int var1;
const char *var2;
void *var3;
};
Examples
mainSuite.inc Example of a simple PicoTest suite.
fixtures.c Demonstrates test fixture call sequences.
See also
PICOTEST_FIXTURE_SETUP
PICOTEST_FIXTURE_TEARDOWN
PICOTEST_CASE

◆ PICOTEST_FIXTURE_SETUP

#define PICOTEST_FIXTURE_SETUP ( ...)

Test fixture initialization.

Parameters
_fixtureNameName of the fixture.
_context(optional) Fixture context structure defined using PICOTEST_FIXTURE_CONTEXT(_fixtureName).
Usage
A simple fixture with no context:
/* Simple fixture */
PICOTEST_FIXTURE_SETUP(simpleFixture) {
/* Initialize static stuff */
staticVar = 1;
srand(1234);
}
PICOTEST_FIXTURE_TEARDOWN(simpleFixture) {
/* Cleanup static stuff */
staticVar = 0;
srand(clock());
}

A more complex example with a context structure:

/* Fixture context */
PICOTEST_FIXTURE_CONTEXT(fixtureWithContext) {
int var1;
const char *var2;
void *var3;
};
/* Fixture setup */
PICOTEST_FIXTURE_SETUP(fixtureWithContext, context) {
/* Initialize static stuff */
staticVar = 2;
/* Initialize fixture context */
context->var1 = 1;
context->var2 = "test";
context->var3 = (void *) malloc(1);
}
/* Fixture teardown */
PICOTEST_FIXTURE_TEARDOWN(fixtureWithContext, context) {
/* Cleanup static stuff */
staticVar = 0;
/* Cleanup fixture context */
free(context->var3);
}

Fixtures may define an optional context that test cases don't need, in this case the context passed to the setup and teardown functions is NULL:

/* Fixture context */
PICOTEST_FIXTURE_CONTEXT(fixtureWithOptionalContext) {
void *buffer;
};
/* Fixture setup */
PICOTEST_FIXTURE_SETUP(fixtureWithOptionalContext, context) {
/* Initialize static stuff */
staticVar = 3;
/* Initialize fixture context */
if (context) {
staticVar++;
context->buffer = (void *) malloc(1);
}
}
/* Fixture teardown */
PICOTEST_FIXTURE_TEARDOWN(fixtureWithOptionalContext, context) {
/* Cleanup static stuff */
staticVar = 0;
/* Cleanup fixture context */
if (context) {
free(context->buffer);
}
}

Here is an example of such a test case:

PICOTEST_CASE(testCase5, fixtureWithOptionalContext) {
PICOTEST_VERIFY(staticVar == 3); /* Passes */
PICOTEST_ASSERT(PASSES); /* Passes */
}
Examples
mainSuite.inc Example of a simple PicoTest suite.
fixtures.c Demonstrates test fixture call sequences.
See also
PICOTEST_FIXTURE_CONTEXT
PICOTEST_FIXTURE_TEARDOWN
PICOTEST_CASE

◆ PICOTEST_FIXTURE_TEARDOWN

#define PICOTEST_FIXTURE_TEARDOWN ( ...)

Test fixture cleanup.

Parameters
_fixtureNameName of the fixture.
_context(optional) Fixture context structure defined using PICOTEST_FIXTURE_CONTEXT(_fixtureName).
Usage
A simple fixture with no context:
/* Simple fixture */
PICOTEST_FIXTURE_SETUP(simpleFixture) {
/* Initialize static stuff */
staticVar = 1;
srand(1234);
}
PICOTEST_FIXTURE_TEARDOWN(simpleFixture) {
/* Cleanup static stuff */
staticVar = 0;
srand(clock());
}

A more complex example with a context structure:

/* Fixture context */
PICOTEST_FIXTURE_CONTEXT(fixtureWithContext) {
int var1;
const char *var2;
void *var3;
};
/* Fixture setup */
PICOTEST_FIXTURE_SETUP(fixtureWithContext, context) {
/* Initialize static stuff */
staticVar = 2;
/* Initialize fixture context */
context->var1 = 1;
context->var2 = "test";
context->var3 = (void *) malloc(1);
}
/* Fixture teardown */
PICOTEST_FIXTURE_TEARDOWN(fixtureWithContext, context) {
/* Cleanup static stuff */
staticVar = 0;
/* Cleanup fixture context */
free(context->var3);
}

Fixtures may define an optional context that test cases don't need, in this case the context passed to the setup and teardown functions is NULL:

/* Fixture context */
PICOTEST_FIXTURE_CONTEXT(fixtureWithOptionalContext) {
void *buffer;
};
/* Fixture setup */
PICOTEST_FIXTURE_SETUP(fixtureWithOptionalContext, context) {
/* Initialize static stuff */
staticVar = 3;
/* Initialize fixture context */
if (context) {
staticVar++;
context->buffer = (void *) malloc(1);
}
}
/* Fixture teardown */
PICOTEST_FIXTURE_TEARDOWN(fixtureWithOptionalContext, context) {
/* Cleanup static stuff */
staticVar = 0;
/* Cleanup fixture context */
if (context) {
free(context->buffer);
}
}

Here is an example of such a test case:

PICOTEST_CASE(testCase5, fixtureWithOptionalContext) {
PICOTEST_VERIFY(staticVar == 3); /* Passes */
PICOTEST_ASSERT(PASSES); /* Passes */
}
Examples
mainSuite.inc Example of a simple PicoTest suite.
fixtures.c Demonstrates test fixture call sequences.
See also
PICOTEST_FIXTURE_CONTEXT
PICOTEST_FIXTURE_SETUP
PICOTEST_CASE

◆ PICOTEST_FIXTURE_BEFORE_SETUP_DEFAULT

#define PICOTEST_FIXTURE_BEFORE_SETUP_DEFAULT ( fixtureName,
testName )

Default test fixture before setup hook.

Does nothing.

See also
PicoTestFixtureBeforeSetupProc
PICOTEST_FIXTURE_BEFORE_SETUP

◆ PICOTEST_FIXTURE_BEFORE_SETUP

#define PICOTEST_FIXTURE_BEFORE_SETUP

Define the test fixture before setup hook.

The default hook does nothing. Redefine this macro to use a custom hook, which must follow the PicoTestFixtureBeforeSetupProc 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_FIXTURE_BEFORE_SETUP
#define PICOTEST_FIXTURE_BEFORE_SETUP beforeSetup
/* Hook function. */
void beforeSetup(const char *fixtureName, const char *testName) {
indent(level++);
printf("fixture %s setup...", fixtureName);
}
Examples
hooks.c Example of PicoTest hooks, prints all events to stdout.
See also
PicoTestFixtureBeforeSetupProc
PICOTEST_FIXTURE_BEFORE_SETUP_DEFAULT
PICOTEST_FIXTURE_AFTER_SETUP

◆ PICOTEST_FIXTURE_AFTER_SETUP_DEFAULT

#define PICOTEST_FIXTURE_AFTER_SETUP_DEFAULT ( fixtureName,
testName )

Default test fixture after setup hook.

Does nothing.

See also
PicoTestFixtureAfterSetupProc
PICOTEST_FIXTURE_AFTER_SETUP

◆ PICOTEST_FIXTURE_AFTER_SETUP

#define PICOTEST_FIXTURE_AFTER_SETUP

Define the test fixture after setup hook.

The default hook does nothing. Redefine this macro to use a custom hook, which must follow the PicoTestFixtureAfterSetupProc 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_FIXTURE_AFTER_SETUP
#define PICOTEST_FIXTURE_AFTER_SETUP afterSetup
/* Hook function. */
void afterSetup(const char *fixtureName, const char *testName) {
printf(" done\n");
--level;
}
Examples
hooks.c Example of PicoTest hooks, prints all events to stdout.
See also
PicoTestFixtureAfterSetupProc
PICOTEST_FIXTURE_AFTER_SETUP_DEFAULT
PICOTEST_FIXTURE_BEFORE_SETUP

◆ PICOTEST_FIXTURE_BEFORE_TEARDOWN_DEFAULT

#define PICOTEST_FIXTURE_BEFORE_TEARDOWN_DEFAULT ( fixtureName,
testName,
fail )

Default test fixture before teardown hook.

Does nothing.

See also
PicoTestFixtureBeforeTeardownProc
PICOTEST_FIXTURE_BEFORE_TEARDOWN

◆ PICOTEST_FIXTURE_BEFORE_TEARDOWN

#define PICOTEST_FIXTURE_BEFORE_TEARDOWN

Define the test fixture before teardown hook.

The default hook does nothing. Redefine this macro to use a custom hook, which must follow the PicoTestFixtureBeforeTeardownProc 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_FIXTURE_BEFORE_TEARDOWN
#define PICOTEST_FIXTURE_BEFORE_TEARDOWN beforeTeardown
/* Hook function. */
void beforeTeardown(const char *fixtureName, const char *testName, int fail) {
indent(level++);
printf("fixture %s teardown...", fixtureName);
}
Examples
hooks.c Example of PicoTest hooks, prints all events to stdout.
See also
PicoTestFixtureBeforeTeardownProc
PICOTEST_FIXTURE_BEFORE_TEARDOWN_DEFAULT
PICOTEST_FIXTURE_AFTER_TEARDOWN

◆ PICOTEST_FIXTURE_AFTER_TEARDOWN_DEFAULT

#define PICOTEST_FIXTURE_AFTER_TEARDOWN_DEFAULT ( fixtureName,
testName,
fail )

Default test fixture after teardown hook.

Does nothing.

See also
PicoTestFixtureAfterTeardownProc
PICOTEST_FIXTURE_AFTER_TEARDOWN

◆ PICOTEST_FIXTURE_AFTER_TEARDOWN

#define PICOTEST_FIXTURE_AFTER_TEARDOWN

Define the test fixture after teardown hook.

The default hook does nothing. Redefine this macro to use a custom hook, which must follow the PicoTestFixtureAfterTeardownProc 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_FIXTURE_AFTER_TEARDOWN
#define PICOTEST_FIXTURE_AFTER_TEARDOWN afterTeardown
/* Hook function. */
void afterTeardown(const char *fixtureName, const char *testName, int fail) {
printf(" done\n");
--level;
}
Examples
hooks.c Example of PicoTest hooks, prints all events to stdout.
See also
PicoTestFixtureAfterTeardownProc
PICOTEST_FIXTURE_AFTER_TEARDOWN_DEFAULT
PICOTEST_FIXTURE_BEFORE_TEARDOWN

Typedef Documentation

◆ PicoTestFixtureBeforeSetupProc

typedef void PicoTestFixtureBeforeSetupProc(const char *fixtureName, const char *testName)

Function signature of test fixture before setup hooks.

Called before running the test fixture setup.

Parameters
fixtureNameTest fixture name.
testNameTest case name.
Usage
/* Hook declarations. */
#undef PICOTEST_FIXTURE_BEFORE_SETUP
#define PICOTEST_FIXTURE_BEFORE_SETUP beforeSetup
/* Hook function. */
void beforeSetup(const char *fixtureName, const char *testName) {
indent(level++);
printf("fixture %s setup...", fixtureName);
}
Examples
hooks.c Example of PicoTest hooks, prints all events to stdout.
See also
PICOTEST_FIXTURE_BEFORE_SETUP

◆ PicoTestFixtureAfterSetupProc

typedef void PicoTestFixtureAfterSetupProc(const char *fixtureName, const char *testName)

Function signature of test fixture after setup hooks.

Called after running the test fixture setup.

Parameters
fixtureNameTest fixture name.
testNameTest case name.
Usage
/* Hook declarations. */
#undef PICOTEST_FIXTURE_AFTER_SETUP
#define PICOTEST_FIXTURE_AFTER_SETUP afterSetup
/* Hook function. */
void afterSetup(const char *fixtureName, const char *testName) {
printf(" done\n");
--level;
}
Examples
hooks.c Example of PicoTest hooks, prints all events to stdout.
See also
PICOTEST_FIXTURE_AFTER_SETUP

◆ PicoTestFixtureBeforeTeardownProc

typedef void PicoTestFixtureBeforeTeardownProc(const char *fixtureName, const char *testName, int fail)

Function signature of test fixture before teardown hooks.

Called before running the test fixture teardown.

Parameters
fixtureNameTest fixture name.
testNameTest case name.
failFailed tests (including its subtests if any).
Usage
/* Hook declarations. */
#undef PICOTEST_FIXTURE_BEFORE_TEARDOWN
#define PICOTEST_FIXTURE_BEFORE_TEARDOWN beforeTeardown
/* Hook function. */
void beforeTeardown(const char *fixtureName, const char *testName, int fail) {
indent(level++);
printf("fixture %s teardown...", fixtureName);
}
Examples
hooks.c Example of PicoTest hooks, prints all events to stdout.
See also
PICOTEST_FIXTURE_BEFORE_TEARDOWN

◆ PicoTestFixtureAfterTeardownProc

typedef void PicoTestFixtureAfterTeardownProc(const char *fixtureName, const char *testName, int fail)

Function signature of test fixture after teardown hooks.

Called after running the test fixture teardown.

Parameters
fixtureNameTest fixture name.
testNameTest case name.
failFailed tests (including its subtests if any).
Usage
/* Hook declarations. */
#undef PICOTEST_FIXTURE_AFTER_TEARDOWN
#define PICOTEST_FIXTURE_AFTER_TEARDOWN afterTeardown
/* Hook function. */
void afterTeardown(const char *fixtureName, const char *testName, int fail) {
printf(" done\n");
--level;
}
Examples
hooks.c Example of PicoTest hooks, prints all events to stdout.
See also
PICOTEST_FIXTURE_AFTER_TEARDOWN