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

fcf::NTest::State class

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.

Methods
fcf::NTest::Test test()
- Returns the currently executing test case metadata.
std::set<fcf::NTest::Test> tests()
- Returns a set of all currently selected tests.
size_t testCount()
- Returns the number of tests currently selected for execution.
fcf::NTest::Duration duration()
void duration(fcf::NTest::Duration a_duration)
- Returns or sets the total execution duration of the test suite.
void error(const char* a_error, bool a_ignoreExists)
- Records an error message in the global state.
std::vector<std::string> errors()
void errors(std::vector<std::string> a_errors)
- Returns or sets the list of recorded error messages.
fcf::NTest::SharedPtrAny data(const char* a_key)
void data(const char* a_key, fcf::NTest::SharedPtrAny a_data)
- Provides access to the user-defined data storage within the state.
void eraseData(const char* a_key)
- Removes a data entry from the state storage.
std::set<std::string> dataKeys()
- Returns a set of all keys currently present in the state storage.

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> FCF_TEST_DEFINE("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.test == "StorageTest", "Current test name mismatch"); } FCF_TEST_DEFINE("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