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

fcf::NTest::Fixture class

Type:
class fcf::NTest::Fixture

Package: fcfTest

File: test.hpp

Available from version: 1.2.1

A structure representing a setup or teardown operation that can be applied at various levels of the test hierarchy.

The fcf::NTest::Fixture structure defines a lifecycle hook that can be executed before or after tests. Fixtures are highly flexible and can be scoped to the entire suite (fcf::NTest::FL_GLOBAL), a specific part (fcf::NTest::FL_PART), a group (fcf::NTest::FL_GROUP), or an individual test (fcf::NTest::FL_TEST). They are essential for managing resources like database connections, file handles, or memory buffers that need to be initialized before tests run and cleaned up afterwards.

Properties
std::vector<std::string> parts
- A list of part names to which this fixture applies.
std::vector<std::string> groups
- A list of group names to which this fixture applies.
std::vector<std::string> tests
- A list of specific test names to which this fixture applies.
bool before = true
- Indicates whether this fixture is a setup fixture (runs before) or a teardown fixture (runs after).
fcf::NTest::EFixtureLevel level = fcf::NTest::FL_GLOBAL
- Defines the hierarchical scope of the fixture.
void (*)() fixtureFunction = nullptr
- A pointer to the function containing the fixture logic.
std::string file
- The source file where the fixture was defined.
unsigned int line = 0
- The source line number where the fixture was defined.

Example: Hierarchical Fixture Usage

Demonstrates how to define global and group-level fixtures to manage shared resources during test execution.

#define FCF_TEST_IMPLEMENTATION #include <fcfTest/test.hpp> // --- Fixtures --- // 1. Define a Global Fixture (Setup) // This runs once at the very beginning of the entire test suite. FCF_TEST_BEFORE_DEFINE("System", "*", "*", fcf::NTest::FL_GLOBAL) { fcf::NTest::log() << "[GLOBAL SETUP] Initializing system resources..." << std::endl; } // 2. Define a Global Fixture (Teardown) // This runs once at the very end of the entire test suite. FCF_TEST_AFTER_DEFINE("System", "*", "*", fcf::NTest::FL_GLOBAL) { fcf::NTest::log() << "[GLOBAL TEARDOWN] Cleaning up system resources..." << std::endl; } // 3. Define a Group-level Fixture // This runs before and after every test within the "Database" group. FCF_TEST_BEFORE_DEFINE("System", "Fixture|SomeGroup", "*", fcf::NTest::FL_GROUP) { fcf::NTest::log() << "[GROUP SETUP] Connecting to database..." << std::endl; } FCF_TEST_AFTER_DEFINE("System", "Fixture|SomeGroup", "*", fcf::NTest::FL_GROUP) { fcf::NTest::log() << "[GROUP TEARDOWN] Disconnecting from database..." << std::endl; } // --- Tests --- FCF_TEST_DEFINE("System", "Fixture", "storage") { 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; } } int main(int a_argc, char* a_argv[]) { bool error = false; fcf::NTest::cmdRun(a_argc, a_argv, fcf::NTest::CRM_RUN, &error); return error ? 1 : 0; }

Output:

> [GLOBAL SETUP] Initializing system resources... > [GROUP SETUP] Connecting to database... Performing the test: "System" -> "Fixture" -> "storage" ... > List of registered fixtures: > Fixture: System -> * -> * (level: 0; before: 1) > Fixture: System -> * -> * (level: 0; before: 0) > Fixture: System -> Fixture|SomeGroup -> * (level: 2; before: 1) > Fixture: System -> Fixture|SomeGroup -> * (level: 2; before: 0) [SUCCESS] Test completed successfully (0.000`017`858 sec) > [GROUP TEARDOWN] Disconnecting from database... > [GLOBAL TEARDOWN] Cleaning up system resources... [SUCCESS] All tests were completed. Tests: 1 passed, 0 failed, 0 skipped, 1 total Duration: 0.000`017`858 sec