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

回眸只為那壹抹淺笑 提交于 2019-12-10 02:12:02

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/10251375/how-to-tell-boost-test-to-stop-on-first-failing-test-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!