Package: fcfTest
File: test.hpp
Available from version: 1.2.1
Provides access to the global singleton instance of the test state.
The state function is a global accessor that returns a reference to the singleton instance of the fcf::NTest::State class. This object acts as the central repository for the current execution context of the test runner.
Through this instance, you can interact with various aspects of the ongoing test session, including:
- Test Metadata: Access the currently executing fcf::NTest::Test or the full set of registered tests.
- Execution Metrics: Retrieve the total fcf::NTest::Duration of the test suite.
- Error Tracking: Inspect the list of errors encountered during execution.
- Custom Data Storage: Store and retrieve arbitrary user data using fcf::NTest::SharedPtrAny via a key-value mechanism.
Result
fcf::NTest::State&
- A reference to the global singleton fcf::NTest::State instance.
Example: Interacting with Global Test State
Demonstrating how to use the state() function to store custom data and retrieve test information during execution.
#define FCF_TEST_IMPLEMENTATION
#include <fcfTest/test.hpp>
#include <iostream>
FCF_TEST_DEFINE("StateDemo", "Data", "CustomDataTest") {
// 1. Store custom data in the global state using a key
// We use SharedPtrAny::make to wrap an integer
fcf::NTest::state().data("my-counter", fcf::NTest::SharedPtrAny::make<int>(42));
// 2. Retrieve the stored data and cast it back to the original type
int* counterPtr = fcf::NTest::state().data("my-counter").cast<int>();
FCF_TEST(*counterPtr == 42, "Value should be 42");
// 3. Access information about the currently running test
fcf::NTest::Test currentTest = fcf::NTest::state().test();
fcf::NTest::log() << "Running test: " << currentTest.test << 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:
Performing the test: "StateDemo" -> "Data" -> "CustomDataTest" ...
> Running test: CustomDataTest
[SUCCESS] Test completed successfully (0.000`000`123 sec)
[SUCCESS] All tests were completed.
Tests: 1 passed, 0 failed, 0 skipped, 1 total
Duration: 0.000`000`123 sec
The state function is essential for advanced test orchestration and sharing context between different parts of your test suite.