dbw dbw - 6 months ago 69
C++ Question

How to skip a BOOST unit test?

How can I skip a BOOST unit test? I would like to programatically skip some of my unit tests depending on, (for instance) the platform on which I am executing them. My current solution is:

#define REQUIRE_LINUX char * os_cpu = getenv("OS_CPU"); if ( os_cpu != "Linux-x86_64" ) return;

BOOST_AUTO_TEST_CASE(onlylinux) {
REQUIRE_LINUX
...
the rest of the test code.
}


(note that our build environment sets the variable OS_CPU). This seems ugly and error-prone, and also like the silent skips could cause users to be skipping tests without knowing about it.

How can I cleanly skip boost unit tests based on arbitrary logic?

Answer Source

Instead of skipping them, you can prevent to register them. To achieve that you can use the manual test registration of boost.test:

#include <boost/test/included/unit_test.hpp>
using namespace boost::unit_test;

//____________________________________________________________________________//

void only_linux_test()
{
    ...
}

//____________________________________________________________________________//

test_suite*
init_unit_test_suite( int argc, char* argv[] ) 
{
    if(/* is linux */)
        framework::master_test_suite().
            add( BOOST_TEST_CASE( &only_linux_test ) );

    return 0;
}

See http://www.boost.org/doc/libs/1_53_0/libs/test/doc/html/utf/user-guide/test-organization/manual-nullary-test-case.html for more information

Another possibility would be to use #ifdef ... #endif with BOOST_AUTO_TEST_CASE. Therfor you need a definition that is defined if you are compiling the code on the target platform.

#ifdef PLATFORM_IS_LINUX

BOOST_AUTO_TEST_CASE(onlyLinux)
{
    ...
}
#endif

This definition can for example be set by your build environment.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download