Type:
class fcf::NTest::State
Package: fcfTest
File: test.hpp
Available from version: 1.2.1
A thread-safe singleton class that manages the global execution state of the test runner, including test results, execution duration, and custom user data.
The fcf::NTest::State class serves as the central repository for all runtime information during a test execution session. It acts as a thread-safe singleton that tracks the currently executing fcf::NTest::Test, the set of all selected tests, the total accumulated fcf::NTest::Duration, and a list of encountered errors. Additionally, it provides a mechanism for users to store and retrieve arbitrary data using fcf::NTest::SharedPtrAny, allowing for complex state sharing between tests, fixtures, and custom reporters.
Example: Managing Global Test State and User Data
Demonstrates how to access the global state to store custom metadata during a test and retrieve it later, as well as how to inspect the current test and error status.
#define FCF_TEST_IMPLEMENTATION
#include <fcfTest/test.hpp>
#include <string>
FCF_TEST_DECLARE("StateDemo", "Data", "StorageTest") {
// 1. Store custom data in the global state using a key
// We use SharedPtrAny to store an arbitrary type (in this case, a string)
fcf::NTest::state().data("session_id", fcf::NTest::SharedPtrAny::make<std::string>("SESSION_123"));
// 2. Perform an assertion
FCF_TEST(true);
// 3. Retrieve the data in a different part of the test (or even a different test)
// We must cast it back to the original type
std::string* session = fcf::NTest::state().data("session_id").cast<std::string>();
FCF_TEST(session != nullptr, "Session pointer should not be null");
FCF_TEST(*session == "SESSION_123", "Session ID mismatch");
// 4. Check the current test information
fcf::NTest::Test current = fcf::NTest::state().test();
FCF_TEST(current.name == "StorageTest", "Current test name mismatch");
}
FCF_TEST_DECLARE("StateDemo", "Data", "RetrievalTest") {
// Retrieve the same data stored in the previous test
std::string* session = fcf::NTest::state().data("session_id").cast<std::string>();
FCF_TEST(session != nullptr, "Data should persist across tests in the same run");
FCF_TEST(*session == "SESSION_123");
}
FCF_TEST_TEST_ORDER("StorageTest", 1);
FCF_TEST_TEST_ORDER("RetrievalTest", 2);
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: "StateDemo" -> "Data" -> "StorageTest" ...
[SUCCESS] Test completed successfully (0.000`001`758 sec)
Performing the test: "StateDemo" -> "Data" -> "RetrievalTest" ...
[SUCCESS] Test completed successfully (0.000`000`430 sec)
[SUCCESS] All tests were completed.
Tests: 2 passed, 0 failed, 0 skipped, 2 total
Duration: 0.000`002`188 sec