问题 如何跳过BOOST单元测试?


如何跳过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.
}

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

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


1064
2018-06-04 23:13


起源



答案:


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

看到 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

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


2
2018-06-05 09:44



我不能使用ifdef,其中一些标准必须在执行时确定。我可能会使用类似你的注册建议,谢谢。 - dbn


答案:


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

看到 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

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


2
2018-06-05 09:44



我不能使用ifdef,其中一些标准必须在执行时确定。我可能会使用类似你的注册建议,谢谢。 - dbn


使用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))
{...}

在运行时评估前置条件,在编译时启用enable_if。

看到: http://www.boost.org/doc/libs/1_61_0/libs/test/doc/html/boost_test/tests_organization/enabling.html


4
2017-07-08 11:06



大!这正是我一直在寻找的(当天)。我将通过几个测试来检查它。 - dbn
@Horus,有没有办法在前提条件下使用灯具? - mojo
@mojo您可以查看我的代码 github.com/precice/precice/blob/develop/src/testing/Testing.hpp。我使用装饰器从测试树中删除测试,它也适用于灯具。请注意,我的代码可能使用非官方的boost API。 - Horus


手动注册测试用例非常繁琐,乏味且容易出错。如果只是通过平台来区分测试用例,那么我就不会通过配置构建系统来编译无关紧要的平台上的无关测试用例。或者,您可以使用 Boost.Predef 它为您可能想要了解的有关操作系统,编译器等的所有内容定义了必要的预处理程序符号 #ifdef 某些测试。

最后,如果此标准只能在运行时知道并且独立于您运行的平台,那么我会将依赖于特定条件的测试分组到套件中,并调整构建使用的命令行以仅运行那些基于运行时条件的套件。


3
2017-08-08 21:47





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

{
   ...
}

2
2017-11-24 16:12



我在文档中找不到第二个参数,你确定它可用吗? - moodboom
完全是的 boost.org/doc/libs/1_65_1/libs/test/doc/html/boost_test/... - Sergei
很感谢你 - 现在我看到你的第二个参数实际上是文档的链接,doh。 - moodboom