Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell Boost.Test to stop on first failing test case?

I've got a number of Boost test cases ordered in several test suites. Some test cases have one, some more than one check.

However, when executing all tests, they all get executed – no matter how many fail or pass. I know, that I can stop the execution of one test case with several checks by using BOOST_REQUIRE instead of BOOST_CHECK. But that's not want I want.

How can I tell Boost to stop the whole execution after the first test case failed? I would prefer a compiled solution (e.g. realized with a global fixture) over a runtime solution (i.e. runtime parameters).

like image 690
Torbjörn Avatar asked Apr 20 '12 18:04

Torbjörn


People also ask

In which check level test cases running do not stop even after failure of previous test case for tool boost test?

CHECK for standard checks: this is the most commonly used assertion level. If the statement evaluates to false , the test case is flagged as failed but its execution continues.

What is test adapter for boost test?

The Test Adapter for Boost. Test is a unit testing extension published by Microsoft and based on the existing Boost Unit Test Adapter (v1. 0.8) Visual Studio extension by Gunter Wirth's team from ETAS GmbH. This extension is developed in collaboration with the original project with the aim of improving Boost.

How do I install a boost test?

Create a Boost. cpp file for your tests, right-click on the project node in Solution Explorer and choose Add > New Item. In the Add New Item dialog, expand Installed > Visual C++ > Test. Select Boost. Test, then choose Add to add Test.


2 Answers

BOOST_REQUIRE will stop the current test case in a test suite but go on on others.

I don't really see what you wanted when you asked for a "compiled solution" but here is a trick that should work. I use a boolean to check the stability of the whole test suite. If it is unstable i.e a BOOST_REQUIRE has been triggered then I stop the whole thing.

Hope it can help you.

//#include <...>

//FIXTURES ZONE
struct fixture
{
    fixture():x(0.0),y(0.0){}
    double x;
    double y;
};

//HELPERS ZONE
static bool test_suite_stable = true;

void in_strategy(bool & stable)
{
    if(stable)
        {
            stable = false;
        }
    else
        {
            exit();
        }
}

void out_strategy(bool & stable)
{
    if(!stable)
        {
            stable = true;
        }
}

BOOST_AUTO_TEST_SUITE(my_test_suite)

//TEST CASES ZONE
BOOST_FIXTURE_TEST_CASE(my_test_case, fixture)
{
    in_strategy(test_suite_stable);
    //...
    //BOOST_REQUIRE() -> triggered
    out_strategy(test_suite_stable);
}

BOOST_FIXTURE_TEST_CASE(another_test_case, fixture)
{
    in_strategy(test_suite_stable); //-> exit() since last triggered so stable = false
    //...
    //BOOST_REQUIRE()
    out_strategy(test_suite_stable);
}

BOOST_TEST_SUITE_END()

Benoit.

like image 184
Quanteek Avatar answered Oct 12 '22 23:10

Quanteek


Why not just use assert? Not only you abort immediately the whole program, you will also be able to see stack if necessary.

like image 30
Gennadiy Rozental Avatar answered Oct 13 '22 00:10

Gennadiy Rozental