簡體   English   中英

如何跳過BOOST單元測試?

[英]How to skip a BOOST unit test?

如何跳過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)。 這看起來很丑陋且容易出錯,而且無聲跳過也可能導致用戶在不知情的情況下跳過測試。

如何基於任意邏輯干凈地跳過升壓單元測試?

使用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_AUTO_TEST_CASE(a_test_name, *boost::unit_test::disabled() )

{
   ...
}

手動注冊測試用例非常繁瑣,乏味且容易出錯。 如果只是通過平台來區分測試用例,那么我就不會通過配置構建系統來編譯無關緊要的平台上的無關測試用例。 或者,您可以使用Boost.Predef為您可能想要了解的有關操作系統,編譯器等的所有內容定義必要的預處理程序符號,以便#ifdef輸出某些測試。

最后,如果此標准只能在運行時知道並且與您運行的平台無關,那么我會將依賴於特定條件的測試分組到套件中,並調整構建使用的命令行以僅運行那些基於運行時條件的套件。

您可以阻止注冊它們,而不是跳過它們。 為此,您可以使用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

例如,您的構建環境可以設置此定義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM