Search code examples
c++unit-testingboostboost-test

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).


Solution

  • 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.