Skip to content

Commit

Permalink
Add test fixture hook tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericbonnet committed Mar 6, 2020
1 parent 6172009 commit a986ffa
Show file tree
Hide file tree
Showing 3 changed files with 244 additions and 1 deletion.
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ target_sources(test_picotest
testSuites.c
testSuiteHooks.c
testFixtures.c
testFixtureHooks.c
)
target_link_libraries(test_picotest
PRIVATE
Expand Down
2 changes: 1 addition & 1 deletion tests/mainSuite.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

PICOTEST_SUITE(mainSuite, testAssertions, testAssertionHooks, testFilters,
testLoggers, testCases, testCaseHooks, testSuites,
testSuiteHooks, testFixtures);
testSuiteHooks, testFixtures, testFixtureHooks);
242 changes: 242 additions & 0 deletions tests/testFixtureHooks.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
#include <picotest.h>

/*
* Test utilities
*/

static struct {
int testCaseStep;
struct {
int step;
const char *fixtureName;
const char *testName;
} testFixtureBeforeSetup;
struct {
int step;
const char *fixtureName;
const char *testName;
} testFixtureAfterSetup;
struct {
int step;
const char *fixtureName;
const char *testName;
int fail;
} testFixtureBeforeTeardown;
struct {
int step;
const char *fixtureName;
const char *testName;
int fail;
} testFixtureAfterTeardown;
} state;
static void init() {
memset(&state, 0, sizeof(state));
}

/* Hooks used for testing */
static PicoTestFixtureBeforeSetupProc testFixtureBeforeSetup;
static PicoTestFixtureAfterSetupProc testFixtureAfterSetup;
static PicoTestFixtureBeforeTeardownProc testFixtureBeforeTeardown;
static PicoTestFixtureAfterTeardownProc testFixtureAfterTeardown;
#undef PICOTEST_FIXTURE_BEFORE_SETUP
#undef PICOTEST_FIXTURE_AFTER_SETUP
#undef PICOTEST_FIXTURE_BEFORE_TEARDOWN
#undef PICOTEST_FIXTURE_AFTER_TEARDOWN
#define PICOTEST_FIXTURE_BEFORE_SETUP testFixtureBeforeSetup
#define PICOTEST_FIXTURE_AFTER_SETUP testFixtureAfterSetup
#define PICOTEST_FIXTURE_BEFORE_TEARDOWN testFixtureBeforeTeardown
#define PICOTEST_FIXTURE_AFTER_TEARDOWN testFixtureAfterTeardown
void testFixtureBeforeSetup(const char *fixtureName, const char *testName) {
state.testFixtureBeforeSetup.step = ++state.testCaseStep;
state.testFixtureBeforeSetup.fixtureName = fixtureName;
state.testFixtureBeforeSetup.testName = testName;
}
void testFixtureAfterSetup(const char *fixtureName, const char *testName) {
state.testFixtureAfterSetup.step = ++state.testCaseStep;
state.testFixtureAfterSetup.fixtureName = fixtureName;
state.testFixtureAfterSetup.testName = testName;
}
void testFixtureBeforeTeardown(const char *fixtureName, const char *testName,
int fail) {
state.testFixtureBeforeTeardown.step = ++state.testCaseStep;
state.testFixtureBeforeTeardown.fixtureName = fixtureName;
state.testFixtureBeforeTeardown.testName = testName;
state.testFixtureBeforeTeardown.fail = fail;
}
void testFixtureAfterTeardown(const char *fixtureName, const char *testName,
int fail) {
state.testFixtureAfterTeardown.step = ++state.testCaseStep;
state.testFixtureAfterTeardown.fixtureName = fixtureName;
state.testFixtureAfterTeardown.testName = testName;
state.testFixtureAfterTeardown.fail = fail;
}

PICOTEST_CASE(doPassNoFixture) {
state.testCaseStep++;
PICOTEST_ASSERT(1);
state.testCaseStep++;
}
PICOTEST_CASE(doFailNoFixture) {
state.testCaseStep++;
PICOTEST_ASSERT(0);
state.testCaseStep++;
}
PICOTEST_FIXTURE_SETUP(fixture) {}
PICOTEST_FIXTURE_TEARDOWN(fixture) {}
PICOTEST_CASE(doPassFixture, fixture) {
state.testCaseStep++;
PICOTEST_ASSERT(1);
state.testCaseStep++;
}
PICOTEST_CASE(doFailFixture, fixture) {
state.testCaseStep++;
PICOTEST_ASSERT(0);
state.testCaseStep++;
}

/* Reset default settings */
#include "reset.h"

/*
* Test suite for test fixture hooks
*/
#include "hooks.h"

PICOTEST_SUITE(testFixtureHooks, testFixtureHooksNoFixture,
testFixtureHooksCallOrder, testFixtureBeforeSetupHook,
testFixtureAfterSetupHook, testFixtureBeforeTeardownHook,
testFixtureAfterTeardownHook);

PICOTEST_SUITE(testFixtureHooksNoFixture, testFixtureHooksNoFixturePass,
testFixtureHooksNoFixtureFail)
PICOTEST_CASE(testFixtureHooksNoFixturePass) {
init();
doPassNoFixture(NULL);
PICOTEST_VERIFY(state.testFixtureBeforeSetup.step == 0);
PICOTEST_VERIFY(state.testFixtureAfterSetup.step == 0);
PICOTEST_VERIFY(state.testFixtureBeforeTeardown.step == 0);
PICOTEST_VERIFY(state.testFixtureAfterTeardown.step == 0);
}
PICOTEST_CASE(testFixtureHooksNoFixtureFail) {
init();
doFailNoFixture(NULL);
PICOTEST_VERIFY(state.testFixtureBeforeSetup.step == 0);
PICOTEST_VERIFY(state.testFixtureAfterSetup.step == 0);
PICOTEST_VERIFY(state.testFixtureBeforeTeardown.step == 0);
PICOTEST_VERIFY(state.testFixtureAfterTeardown.step == 0);
}

PICOTEST_SUITE(testFixtureHooksCallOrder, testFixtureHooksCallOrderPass,
testFixtureHooksCallOrderFail)
PICOTEST_CASE(testFixtureHooksCallOrderPass) {
init();
doPassFixture(NULL);
PICOTEST_VERIFY(state.testFixtureBeforeSetup.step == 1);
PICOTEST_VERIFY(state.testFixtureAfterSetup.step == 2);
PICOTEST_VERIFY(state.testFixtureBeforeTeardown.step == 5);
PICOTEST_VERIFY(state.testFixtureAfterTeardown.step == 6);
}
PICOTEST_CASE(testFixtureHooksCallOrderFail) {
init();
doFailFixture(NULL);
PICOTEST_VERIFY(state.testFixtureBeforeSetup.step == 1);
PICOTEST_VERIFY(state.testFixtureAfterSetup.step == 2);
PICOTEST_VERIFY(state.testFixtureBeforeTeardown.step == 4);
PICOTEST_VERIFY(state.testFixtureAfterTeardown.step == 5);
}

/*
* PICOTEST_FIXTURE_BEFORE_SETUP
*/

PICOTEST_SUITE(testFixtureBeforeSetupHook, testFixtureBeforeSetupHookPass,
testFixtureBeforeSetupHookFail);
PICOTEST_CASE(testFixtureBeforeSetupHookPass) {
init();
doPassFixture(NULL);
PICOTEST_VERIFY(
strcmp(state.testFixtureBeforeSetup.fixtureName, "fixture") == 0);
PICOTEST_VERIFY(
strcmp(state.testFixtureBeforeSetup.testName, "doPassFixture") == 0);
}
PICOTEST_CASE(testFixtureBeforeSetupHookFail) {
init();
doFailFixture(NULL);
PICOTEST_VERIFY(
strcmp(state.testFixtureBeforeSetup.fixtureName, "fixture") == 0);
PICOTEST_VERIFY(
strcmp(state.testFixtureBeforeSetup.testName, "doFailFixture") == 0);
}

/*
* PICOTEST_FIXTURE_AFTER_SETUP
*/

PICOTEST_SUITE(testFixtureAfterSetupHook, testFixtureAfterSetupHookPass,
testFixtureAfterSetupHookFail);
PICOTEST_CASE(testFixtureAfterSetupHookPass) {
init();
doPassFixture(NULL);
PICOTEST_VERIFY(
strcmp(state.testFixtureAfterSetup.fixtureName, "fixture") == 0);
PICOTEST_VERIFY(
strcmp(state.testFixtureAfterSetup.testName, "doPassFixture") == 0);
}
PICOTEST_CASE(testFixtureAfterSetupHookFail) {
init();
doFailFixture(NULL);
PICOTEST_VERIFY(
strcmp(state.testFixtureAfterSetup.fixtureName, "fixture") == 0);
PICOTEST_VERIFY(
strcmp(state.testFixtureAfterSetup.testName, "doFailFixture") == 0);
}

/*
* PICOTEST_FIXTURE_BEFORE_TEARDOWN
*/

PICOTEST_SUITE(testFixtureBeforeTeardownHook, testFixtureBeforeTeardownHookPass,
testFixtureBeforeTeardownHookFail);
PICOTEST_CASE(testFixtureBeforeTeardownHookPass) {
init();
doPassFixture(NULL);
PICOTEST_VERIFY(
strcmp(state.testFixtureBeforeTeardown.fixtureName, "fixture") == 0);
PICOTEST_VERIFY(
strcmp(state.testFixtureBeforeTeardown.testName, "doPassFixture") == 0);
PICOTEST_VERIFY(state.testFixtureBeforeTeardown.fail == 0);
}
PICOTEST_CASE(testFixtureBeforeTeardownHookFail) {
init();
doFailFixture(NULL);
PICOTEST_VERIFY(
strcmp(state.testFixtureBeforeTeardown.fixtureName, "fixture") == 0);
PICOTEST_VERIFY(
strcmp(state.testFixtureBeforeTeardown.testName, "doFailFixture") == 0);
PICOTEST_VERIFY(state.testFixtureBeforeTeardown.fail == 1);
}

/*
* PICOTEST_FIXTURE_AFTER_TEARDOWN
*/

PICOTEST_SUITE(testFixtureAfterTeardownHook, testFixtureAfterTeardownHookPass,
testFixtureAfterTeardownHookFail);
PICOTEST_CASE(testFixtureAfterTeardownHookPass) {
init();
doPassFixture(NULL);
PICOTEST_VERIFY(
strcmp(state.testFixtureAfterTeardown.fixtureName, "fixture") == 0);
PICOTEST_VERIFY(
strcmp(state.testFixtureAfterTeardown.testName, "doPassFixture") == 0);
PICOTEST_VERIFY(state.testFixtureAfterTeardown.fail == 0);
}
PICOTEST_CASE(testFixtureAfterTeardownHookFail) {
init();
doFailFixture(NULL);
PICOTEST_VERIFY(
strcmp(state.testFixtureAfterTeardown.fixtureName, "fixture") == 0);
PICOTEST_VERIFY(
strcmp(state.testFixtureAfterTeardown.testName, "doFailFixture") == 0);
PICOTEST_VERIFY(state.testFixtureAfterTeardown.fail == 1);
}

0 comments on commit a986ffa

Please sign in to comment.