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

fcf::NTest::run() function

void fcf::NTest::run(const fcf::NTest::Options& a_options, bool* a_errorPtr = 0)

Package: fcfTest

File: test.hpp

Available from version: 1.0.1

Executes the selected tests based on the provided fcf::NTest::Options configuration. If an error occurs during test execution, the error state is reported via the provided pointer.

The fcf::NTest::run function is the core execution engine of the framework. It filters the registered tests according to the criteria specified in the fcf::NTest::Options structure, sets the global logger level, and executes each test sequentially. The function handles exceptions thrown by tests, ensuring that the logger state is restored to its original level after execution.

Arguments

fcf::NTest::Options a_options
- A constant reference to an fcf::NTest::Options object containing filters (parts, groups, tests, ignores) and the desired logLevel.

bool* a_error = 0
- An optional pointer to a bool.
  • If provided, it is set to true if any test fails or an exception is caught.
  • If nullptr is passed, the function re-throws the caught std::exception.
Detailed description

  • Filtering: Determines the exact set of tests to execute and resets a_errorPtr if a pointer is passed..
  • Logging: Temporarily updates the global logger level to a_options.logLevel.
  • Execution: Iterates through the selected tests, printing a progress message to std::cout before each test.
  • Error Handling: Catches std::exception. Upon catching, it logs the error message and updates a_errorPtr.

Example:

The most common way to use fcf::NTest::run is within a main function to capture whether the test suite passed or failed without crashing the application

#define FCF_TEST_IMPLEMENTATION #include <fcfTest/test.hpp> int main(int argc, char* argv[]) { fcf::NTest::Options options; // ... populate options manually if not using cmdRun ... bool testFailed = false; fcf::NTest::run(options, &testFailed); if (testFailed) { std::cerr << "Test suite failed!" << std::endl; return 1; } return 0; }

Example: Execution with Exception Propagation

If you want the application to terminate immediately with an exception when a test fails, pass nullptr as the second argument

#define FCF_TEST_IMPLEMENTATION #include <fcfTest/test.hpp> int main(int argc, char* argv[]) { fcf::NTest::Options options; // If a test fails, std::runtime_error will be thrown here fcf::NTest::run(options, nullptr); return 0; }