Boost测试框架是否支持测试依赖关系? [英] Does the Boost testing framework support test dependencies?

查看:242
本文介绍了Boost测试框架是否支持测试依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我洗过Boost测试框架的文档,并找到各种 提示 Boost支持此功能,但我还没有查找文档页面或Boost中测试依赖关系支持的任何具体示例。先前提供的页面是否为红色的herrings,或者Boost测试框架是否真的支持测试依赖性?

解决方案

自己的功能是有的,根据链接,它的最终用户使用。
它不是意外记录和应该很快添加(根据链接)。



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



从那里采样(不幸的是,看起来没有BOOST_AUTO_TEST_CASE工作在那里)。
另外请注意,代码不正确,因为 Dependency()从未被调用,因此依赖测试也不会运行。 p>

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

使用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_suite1 之后放置 test_suite2 ,test_suite1将保持相同的依赖关系因为 test_suite2 不会在它之前运行。

  3. 我做了 test_case4 失败,跳过 test_suite1 ,但如果 test_case4 成功, test_suite1

$
我确实可以使依赖注册更漂亮和更短。 b
$ b

代码:

  #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
}

BOOST_AUTO_TEST_CASE(test_case2)
{
BOOST_CHECK(true);
}

BOOST_AUTO_TEST_SUITE_END()


// ____________________________________________________________________ //

test_suite *
init_unit_test_suite 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取决于test_suite2 / test_case4
test_suite1> depends_on(& framework :: get< test_case>(testCase4Id));
}
}
}

return 0;
}

输出:



<$运行3个测试用例...
进入测试套件Master Test Suite
输入测试套件test_suite2
输入测试用例test_case4
< blahblahblah> /consoleapplication5/consoleapplication5.cpp(10):test_case4中的错误:检查false失败
离开测试用例test_case4;测试时间:14ms
退出测试套件test_suite2
跳过测试套件test_suite1
退出测试套件主测试套件

*** 1失败在测试套件Master Test Suite中检测到


One of my favorite unit testing frameworks is PHPUnit because it supports test dependencies (i.e. the ability to mark tests as dependent upon other tests, running the dependent tests conditionally on the success of their dependencies). I've been using the Boost testing framework more recently to test my C++ code, and while it suits most of my unit testing needs, it doesn't appear to support test dependencies.

I've scoured the documentation for the Boost testing framework and have found various hints that Boost supports this feature, but I've yet to find a documentation page or any concrete examples of test dependency support in Boost. Are the previously given pages red herrings, or does the Boost testing framework actually support test dependencies?

解决方案

Well, you've found yourself that the feature is there and according to the links, it's there for end user to use. It's not documented by accident and "should be added soon" (as per links).

Now, here's a nother post I've found which uses feature: http://boost.2283326.n4.nabble.com/Unit-Test-Framework-strange-behaviour-of-test-unit-depends-on-td2653654.html

Sample from there (unfortunately, looks like no BOOST_AUTO_TEST_CASE works there). Also note that code is incorrect because Dependency() is never called and therefore dependent test doesn't run as well.

#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;
} 

Update

Performed some experimenting and here's an example with automatic test/suits and dependencies. Some notes about code:

  1. Boost here is 1.42, there may be slight differences for newer versions.
  2. If you put test_suite2 after test_suite1 in the cpp file, keeping dependencies the same, test_suite1 will be always skipped because test_suite2 is not run before it.
  3. I made test_case4 to fail so that test_suite1 is skipped, but if test_case4 succeeds, test_suite1 does execute.
  4. I'm pretty sure you'll be able to make dependencies registration much prettier and shorter.

The code:

#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;
}

Output:

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"

这篇关于Boost测试框架是否支持测试依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆