如何跳过BOOST单元测试?

dbn*_*dbn 11 c++ boost unit-testing

如何跳过BOOST单元测试?我想以编程方式跳过我的一些单元测试,具体取决于(例如)我执行它们的平台.我目前的解决方案是:

#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.
}
Run Code Online (Sandbox Code Playgroud)

(请注意,我们的构建环境设置变量OS_CPU).这看起来很丑陋且容易出错,而且无声跳过也可能导致用户在不知情的情况下跳过测试.

如何基于任意逻辑干净地跳过升压单元测试?

Hor*_*rus 7

使用 enable_if / enable / precondition 装饰器。

boost::test_tools::assertion_result is_linux(boost::unit_test::test_unit_id)
{
  return isLinux;
}


BOOST_AUTO_TEST_SUITE(MyTestSuite)

BOOST_AUTO_TEST_CASE(MyTestCase,
                     * boost::unit_test::precondition(is_linux))
{...}
Run Code Online (Sandbox Code Playgroud)

precondition 在运行时进行评估,enable 和 enable_if 在编译时进行评估。

请参阅:http : //www.boost.org/doc/libs/1_61_0/libs/test/doc/html/boost_test/tests_organization/enabling.html


Ser*_*gei 7

BOOST_AUTO_TEST_CASE(a_test_name,*boost::unit_test::disabled())

{
   ...
}
Run Code Online (Sandbox Code Playgroud)

  • 完全是的http://www.boost.org/doc/libs/1_65_1/libs/test/doc/html/boost_test/tests_organization/enabling.html (2认同)

Dat*_*yte 3

您可以阻止注册它们,而不是跳过它们。为此,您可以使用 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;
}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅http://www.boost.org/doc/libs/1_53_0/libs/test/doc/html/utf/user-guide/test-organization/manual-nullary-test-case.html

另一种可能性是将 #ifdef ... #endif 与 BOOST_AUTO_TEST_CASE 一起使用。因此,您需要一个在目标平台上编译代码时定义的定义。

#ifdef PLATFORM_IS_LINUX

BOOST_AUTO_TEST_CASE(onlyLinux)
{
    ...
}
#endif
Run Code Online (Sandbox Code Playgroud)

例如,此定义可以由您的构建环境设置。