before from class fcf::NTest::Fixture
Type:
Class:
Package: fcfTest
File: test.hpp
Available from version: 1.2.2
Indicates whether this fixture is a setup fixture (runs before) or a teardown fixture (runs after).
The
- If
- If
false : The fixture is a Teardown fixture. ItsfixtureFunction will be executed after the target tests/groups/parts have finished.
Example: Setup and Teardown Logic
Demonstrating how to use the
#define FCF_TEST_IMPLEMENTATION
#include <fcfTest/test.hpp>
#include <iostream>
// Setup fixture: runs BEFORE the test
FCF_TEST_BEFORE_DEFINE ("Demo", "Group", "Test", fcf::NTest::FL_TEST) {
fcf::NTest::log () << "[SETUP] Initializing resources..." << std::endl;
// Create user data for the test
fcf::NTest::state ().data ("test-data", fcf::NTest::SharedPtrAny::make <int>(1));
}
// Teardown fixture: runs AFTER the test
FCF_TEST_AFTER_DEFINE ("Demo", "Group", "Test", fcf::NTest::FL_TEST) {
fcf::NTest::log () << "[TEARDOWN] Cleaning up resources..." << std::endl;
// Delete fixture data
fcf::NTest::state ().eraseData ("test-data");
}
FCF_TEST_DEFINE ("Demo", "Group", "Test") {
// Get and check user data created by the fixture
int* testDataPtr = fcf::NTest::state ().data ("test-data").cast <int>();
FCF_TEST (*testDataPtr != 0, *testDataPtr);
}
int main(int a_argc, char* a_argv[]) {
std::vector<fcf::NTest::Fixture > fixtures = fcf::NTest::storage().fixtures();
fcf::NTest::log () << "List of registered fixtures:" << std::endl;
for(fcf::NTest::Fixture & fixture : fixtures) {
std::string parts;
std::string groups;
std::string tests;
for(std::string item : fixture.parts ) { parts += std::string(parts.empty() ? "" : "|") + item; };
for(std::string item : fixture.groups ) { groups += std::string(groups.empty() ? "" : "|") + item; };
for(std::string item : fixture.tests ) { tests += std::string(tests.empty() ? "" : "|") + item; };
fcf::NTest::log () << " Fixture: " << parts << " -> " << groups << " -> " << tests
<< " (level: " << fixture.level << "; before: " << fixture.before << ")"
<< std::endl;
}
bool error = false;
fcf::NTest::cmdRun (a_argc, a_argv, fcf::NTest::CRM_RUN , &error);
return error ? 1 : 0;
}
Output:
> List of registered fixtures:
> Fixture: Demo -> Group -> Test (level: 3; before: 1)
> Fixture: Demo -> Group -> Test (level: 3; before: 0)
> [SETUP] Initializing resources...
Performing the test: "Demo" -> "Group" -> "Test" ...
[SUCCESS] Test completed successfully (0.000`001`286 sec)
> [TEARDOWN] Cleaning up resources...
[SUCCESS] All tests were completed.
Tests: 1 passed, 0 failed, 0 skipped, 1 total
Duration: 0.000`001`286 sec
VPSDime is an industry leading VPS hosting company that provides virtualized server services with high performance, availability and friendly support.