FCF 2.0 development in progress...
> > > > > >
[News] [C++ Libraries API] [C++ Downloads] [Donate to the project] [Contacts]

fixtureFunction from class fcf::NTest::Fixture

Property: fixtureFunction = nullptr

Type: void (*)()

Class: fcf::NTest::Fixture

Package: fcfTest

File: test.hpp

Available from version: 1.2.1

A pointer to the function containing the fixture logic.

The fixtureFunction property holds the actual logic that will be executed during the setup or teardown phase. This is a standard C-style function pointer.

When the test runner reaches a point where the fixture is required (based on its level and before properties), it invokes this function. If the function throws an exception, the error is caught by the framework and reported as a Fixture error, which may cause the associated tests to be skipped or marked as failed.

Example: Defining Fixture Logic

Demonstrating how to define a standalone function and assign it to a fixture.

#define FCF_TEST_IMPLEMENTATION #include <fcfTest/test.hpp> // Define the logic in a standalone function void mySetupLogic() { fcf::NTest::log() << "[Logic] Initializing database connection..." << std::endl; // Create user data for the test fcf::NTest::state().data("sessionId", fcf::NTest::SharedPtrAny::make<int>(1)); } void myTeardownLogic() { fcf::NTest::log() << "[Logic] Closing database connection..." << std::endl; // Delete fixture data fcf::NTest::state().eraseData("sessionId"); } FCF_TEST_DEFINE("App", "DB", "QueryTest") { // Get and check user data created by the fixture int* sessionIdPtr = fcf::NTest::state().data("sessionId").cast<int>(); FCF_TEST(*sessionIdPtr != 0, *sessionIdPtr); } int main(int a_argc, char* a_argv[]) { // Manually adding a setup fixture { fcf::NTest::Fixture fixture; fixture.parts.push_back("App"); fixture.groups.push_back("DB"); fixture.tests.push_back("*"); fixture.before = true; fixture.level = fcf::NTest::FL_GROUP; fixture.fixtureFunction = mySetupLogic; fixture.file = __FILE__; fixture.line = 12; fcf::NTest::storage().appendFixture(fixture); } // Manually adding a teardown fixture { fcf::NTest::Fixture fixture; fixture.parts.push_back("App"); fixture.groups.push_back("DB"); fixture.tests.push_back("*"); fixture.before = false; fixture.level = fcf::NTest::FL_GROUP; fixture.fixtureFunction = myTeardownLogic; fixture.file = __FILE__; fixture.line = 12; fcf::NTest::storage().appendFixture(fixture); } bool error = false; fcf::NTest::cmdRun(a_argc, a_argv, fcf::NTest::CRM_RUN, &error); return error ? 1 : 0; }

Output:

> [Logic] Initializing database connection... Performing the test: "App" -> "DB" -> "QueryTest" ... [SUCCESS] Test completed successfully (0.000`001`292 sec) > [Logic] Closing database connection... [SUCCESS] All tests were completed. Tests: 1 passed, 0 failed, 0 skipped, 1 total Duration: 0.000`001`292 sec