如何跳过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)。这看起来很丑陋且容易出错,而且无声跳过也可能导致用户在不知情的情况下跳过测试。
如何根据任意逻辑干净地跳过升压单元测试?
您可以阻止注册它们,而不是跳过它们。
为此,您可以使用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
例如,您的构建环境可以设置此定义。
您可以阻止注册它们,而不是跳过它们。
为此,您可以使用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
例如,您的构建环境可以设置此定义。
使用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
手动注册测试用例非常繁琐,乏味且容易出错。如果只是通过平台来区分测试用例,那么我就不会通过配置构建系统来编译无关紧要的平台上的无关测试用例。或者,您可以使用 Boost.Predef 它为您可能想要了解的有关操作系统,编译器等的所有内容定义了必要的预处理程序符号 #ifdef
某些测试。
最后,如果此标准只能在运行时知道并且独立于您运行的平台,那么我会将依赖于特定条件的测试分组到套件中,并调整构建使用的命令行以仅运行那些基于运行时条件的套件。
BOOST_AUTO_TEST_CASE(a_test_name,
*boost::unit_test::disabled()
)
{
...
}