c++ - Boost 测试框架是否支持测试依赖?

标签 c++ unit-testing boost boost-test

我最喜欢的单元测试框架之一是 PHPUnit因为它支持测试依赖性(即能够将测试标记为依赖于其他测试,有条件地运行依赖性测试以依赖于它们的依赖性)。我一直在使用 Boost testing framework最近测试我的 C++ 代码,虽然它适合我的大部分单元测试需求,但它似乎不支持测试依赖项。

我搜索了 Boost 测试框架的文档并找到了 various hints Boost 支持此功能,但我还没有在 Boost 中找到文档页面或测试依赖支持的任何具体示例。之前给出的页面是转移注意力,还是 Boost 测试框架实际上支持测试依赖项?

最佳答案

好吧,您已经发现该功能已经存在,并且根据链接,它可供最终用户使用。 它不是偶然记录的,“应该很快添加”(根据链接)。

现在,这是我发现的另一篇帖子,它使用了以下功能: http://boost.2283326.n4.nabble.com/Unit-Test-Framework-strange-behaviour-of-test-unit-depends-on-td2653654.html

来自那里的示例(不幸的是,看起来没有 BOOST_AUTO_TEST_CASE 在那里工作)。 另请注意,代码不正确,因为从未调用过 Dependency(),因此依赖测试也未运行。

#include <boost/test/included/unit_test.hpp>

using boost::unit_test::test_suite;

void Dependency()
{
  BOOST_MESSAGE( "Dependency!" );
  BOOST_CHECK( 1 );
}

void TC_TestCase()
{
  BOOST_MESSAGE( "A test case!" );
  BOOST_CHECK( 1 );
}

test_suite*
init_unit_test_suite( int argc, char* argv[] )
{
  test_suite* ts = BOOST_TEST_SUITE( "Test_Test" );

  ts->add( BOOST_TEST_CASE( &TC_TestCase ) );

/*1*/  ts->depends_on( BOOST_TEST_CASE( &Dependency ) );

  return ts;
} 

更新

进行了一些实验,这里是一个带有自动测试/套装和依赖项的示例。 关于代码的一些说明:

  1. 此处的 Boost 为 1.42,较新的版本可能会略有不同。
  2. 如果在 cpp 文件中将 test_suite2 放在 test_suite1 之后,保持依赖关系不变,test_suite1 将始终被跳过,因为 test_suite2 未运行在此之前。
  3. 我让 test_case4 失败以便跳过 test_suite1,但是如果 test_case4 成功,test_suite1 会执行.
  4. 我很确定您能够使依赖项注册变得更漂亮、更短。

代码:

#include <boost/test/included/unit_test.hpp>
using namespace boost::unit_test;

BOOST_AUTO_TEST_SUITE(test_suite2)
BOOST_AUTO_TEST_CASE(test_case4)
{
    BOOST_CHECK(false);
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(test_suite1)
BOOST_AUTO_TEST_CASE(test_case1) 
{ 
    BOOST_CHECK(true);
}

BOOST_AUTO_TEST_CASE(test_case2) 
{
    BOOST_CHECK(true);
}

BOOST_AUTO_TEST_SUITE_END()


//____________________________________________________________________________//

test_suite*
init_unit_test_suite( int argc, char* argv[] )
{
    const auto testSuite1Id = framework::master_test_suite().get("test_suite1");

    if( testSuite1Id != INV_TEST_UNIT_ID ) {
        auto test_suite1 = &framework::get<test_suite>( testSuite1Id );

        const auto testSuite2Id = framework::master_test_suite().get("test_suite2");
        if (testSuite2Id != INV_TEST_UNIT_ID) {
            auto testSuite2 = &framework::get<test_suite>( testSuite2Id );

            const auto testCase4Id = testSuite2->get("test_case4");
            if (testCase4Id != INV_TEST_UNIT_ID) {
                // test_suite1 depends on test_suite2/test_case4 
                test_suite1->depends_on( &framework::get<test_case>( testCase4Id ));
            }
        }
    }

    return 0;
}

输出:

Running 3 test cases...
Entering test suite "Master Test Suite"
Entering test suite "test_suite2"
Entering test case "test_case4"
<blahblahblah>/consoleapplication5/consoleapplication5.cpp(10): error in "test_case4": check false failed
Leaving test case "test_case4"; testing time: 14ms
Leaving test suite "test_suite2"
Test suite "test_suite1"is skipped
Leaving test suite "Master Test Suite"

*** 1 failure detected in test suite "Master Test Suite"

关于c++ - Boost 测试框架是否支持测试依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17604111/

相关文章:

c++ - SSL 握手错误 : session id context uninitialized

c++ - boost是否支持循环文件?

c++ - 如何修复此子类的初始化程序列表错误?

c++ - 在大型项目中以现代 CMake 方式添加 header

c++ - 如何获取非静态成员函数的地址以在 QtConcurrent 中使用?

c++ - 在 OPENMP 中并行添加矩阵 vector

ios - 创建用于单元测试的模拟数据对象

django - 在 Django 中测试 admin.ModelAdmin

c# - 我应该为以下示例使用模拟吗

c++ - Boost ICL : Are some combinations of interval types and functions not implemented?中函数 "contains"的基本使用