0

I'm using boost.test as test suite. I want to know if is possible make some prerequisite for test. For example

uniqut_ptr< MyClass > g_class;

BOOST_AUTO_TEST_CASE( test1 )
{
    BOOST_REQUIRE_NO_THROW( g_class = CreateMyClass() );
}

BOOST_AUTO_TEST_CASE( test2 )
{
    // This test need the test1 as passed
    BOOST_REQUIRE( g_class->doSomething() );
}

In this case if test1 fail program will crash in test2. I know that I can add BOOST_REQUIRE( g_class ) at the begining of each test. But is there another way?

Elvis Dukaj
  • 6,441
  • 10
  • 38
  • 77
  • Unit tests should not depend on the order of execution. This is not a limitation of Boost.Test, but a principle of unit testing in general. To factor out common stuff, you may want to use fixtures – Andy Prowl Jul 05 '13 at 14:23
  • But is test1 always executed first of test2 right? – Elvis Dukaj Jul 05 '13 at 14:24
  • I guess so, but you should not rely on it. You should write your tests in a way that they would work no matter what the order of execution is – Andy Prowl Jul 05 '13 at 14:25
  • Unless I invoke it with the random switch - which is a good thing to try from time to time – doctorlove Jul 05 '13 at 14:27

1 Answers1

2

I see the boost REQUIRE for use when you require something to be true, so put the require at the top of each test. Or consider using a test fixture and do the setup in the setup function. There are examples here It smells like you are trying to use a global variable in your test, so they might interact in horrible ways. Global data is more trouble than it's worth.

doctorlove
  • 17,477
  • 2
  • 41
  • 57