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

FCF_TEST_FIXTURE_DECLARE_START macro

FCF_TEST_FIXTURE_DECLARE_START (const char* am_part, const char* am_group, const char* am_test, EFixtureLevel am_level)
FCF_TEST_FIXTURE_DECLARE_START (const char* am_part, const char* am_group, const char* am_test, EFixtureLevel am_level, TYPE am_fixtureClassName)

Package: fcfTest

File: test.hpp

Available from version: 1.2.1

Starts the declaration of a test fixture. It defines the scope (Part, Group, or Test) and the execution level of the fixture.

The FCF_TEST_FIXTURE_DECLARE_START macro is used to define a setup environment (fixture) for your tests. Fixtures allow you to execute code before and after specific tests, groups, or parts of the test tree.

This macro can be used in conjunction with the FCF_TEST_FIXTURE_DECLARE_END macro to complete the definition of a test data set.

Arguments

const char* am_part
- The name of the part to which this fixture applies.

const char* am_group
- The name of the group to which this fixture applies.

const char* am_test
- The name of the specific test(s) to which this fixture applies.

fcf::NTest::EFixtureLevel am_level
- The scope level of the fixture (fcf::NTest::FL_GLOBAL, fcf::NTest::FL_PART, fcf::NTest::FL_GROUP, or fcf::NTest::FL_TEST).

TYPE am_fixtureClassName
- [OPTIONAL] Specifies the name of the fixture class. Can be used when declaring a friend class.
Detailed description

Behavior:

  1. Registers a fixture in the global storage with a specific hierarchy (Part -> Group -> Test).
  2. The scope is determined by the provided selectors:

    • A test hierarchy element (am_part, am_group, am_test) can contain the full name of the specified level (Part -> Group -> Test)
    • A test hierarchy element (am_part, am_group, am_test) can be equal to "*", in which case all elements from the specified level are selected
    • Multiple elements from the specified level can be listed using the separator character "|". Example: "group1|group2"
  3. The execution level is determined by am_level:

      fcf::NTest::FL_GLOBAL: The fixture's global scope. Initialization runs before all tests, and the completion handler runs after all tests.
    • fcf::NTest::FL_PART: Partition fixture execution zone. Initialization runs before the start of a test in a new partition, and the completion handler runs after the last test in the partition.
    • fcf::NTest::FL_GROUP: Fixture scope is per group. Initialization runs before the start of a test in a new group, and the completion handler runs after the last test in the group.
    • fcf::NTest::FL_TEST: Fixture scope is individual test scope. Initialization runs before the start of each test, and the completion handler runs after the test.

Example: Defining a Group Fixture

A complete example demonstrating how to create a fixture that runs for every test within a specific group.

#define FCF_TEST_IMPLEMENTATION #include <fcfTest/test.hpp> //This fixture will be held before the "Mathematics: Basics" group. FCF_TEST_FIXTURE_DECLARE_START("Math", "Basic", "*", fcf::NTest::FL_GROUP) { fcf::NTest::log() << "[FIXTURE] Setting up group environment..." << std::endl; // Perform setup logic here. Set data for tests. fcf::NTest::state().data("fixture_data", fcf::NTest::SharedPtrAny::make<int>(1)); } //This fixture will be held after the "Mathematics: Basics" group. FCF_TEST_FIXTURE_DECLARE_END("Math", "Basic", "*", fcf::NTest::FL_GROUP) { fcf::NTest::log() << "[FIXTURE] Tearing down group environment..." << std::endl; // Perform teardown logic here. fcf::NTest::state().eraseData("fixture_data"); } FCF_TEST_DECLARE("Math", "Before group", "test") { } FCF_TEST_DECLARE("Math", "Basic", "Addition") { // Get the data set by the fixture int* fixtureData = fcf::NTest::state().data("fixture_data").cast<int>(); FCF_TEST(1 + *fixtureData == 2); } FCF_TEST_DECLARE("Math", "Basic", "Subtraction") { // Get the data set by the fixture int* fixtureData = fcf::NTest::state().data("fixture_data").cast<int>(); FCF_TEST(5 - *fixtureData == 4); } FCF_TEST_DECLARE("Math", "After group", "test") { } FCF_TEST_GROUP_ORDER("Before group", 1) FCF_TEST_GROUP_ORDER("Basic", 2) FCF_TEST_GROUP_ORDER("After group", 3) 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:

Performing the test: "Math" -> "Before group" -> "test" ... [SUCCESS] Test completed successfully (0.000`000`357 sec) Performing the test: "Math" -> "After group" -> "test" ... [SUCCESS] Test completed successfully (0.000`000`148 sec) > [FIXTURE] Setting up group environment... Performing the test: "Math" -> "Basic" -> "Addition" ... [SUCCESS] Test completed successfully (0.000`000`540 sec) Performing the test: "Math" -> "Basic" -> "Subtraction" ... [SUCCESS] Test completed successfully (0.000`000`293 sec) > [FIXTURE] Tearing down group environment... [SUCCESS] All tests were completed. Tests: 4 passed, 0 failed, 0 skipped, 4 total Duration: 0.000`001`338 sec