dbw dbw - 3 years ago 233
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;

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


init_unit_test_suite( int argc, char* argv[] ) 
    if(/* is linux */)
            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.



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