简体   繁体   中英

How to get the test outcome programmatically in boost

Is it possible to determine the outcome of a boost test?

eg if boost support if statements,

if( BOOST_CHECK_SMALL(Fred,0.001))
{
    ...
    printValuesInTest("This value failed:",Fred);
}

If statements do seem to work or I may be having a bad day.

I think you are approaching this wrong. Unit testing tools are usually executed by a test runner . The entire point of these type of frameworks is to avoid doing things like :

// you dont need a unit testing framework to do this! 
if(false_cond())
{
    print_error(...)
    // do other error reporting stuff
}

The whole point is that you dont try to run these tests directly, but use the unit testing framework 's macros and naming schemes to register the test cases (which are each made of a one or more test assertions /conditions). These test cases can sometimes be grouped into categories and subcategories using test suites . The test runner will then run all the tests and give you a report.

The main benefit of this is that you can focus on the logic of the tests, rather than creating, maintaining, and worrying about the mechanism of testing/error reporting. Therefore, when using a unit testing framework, you shouldn't be checking if an assumption is true, you should be asserting that it is true or false. Likewise, you should try to handle error reporting immediately then and there; you should be handing off the any describing information to the unit testing framework, so it can report it properly.

Boost provides multiple options for how a test runner is linked to the library you are testing. Generally once you include the headers with the right macros, running your library will actually run the test runner, which will run all of your tests. You also have an option to use an external test runner.

Here is a better intro to boost unit testing than the official documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM