在手动定义的套件树中增强测试用例和套件夹具

在手动定义的套件树中增强测试用例和套件夹具

本文介绍了在手动定义的套件树中增强测试用例和套件夹具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows x86,Android TI 2.2上使用Boost 1.46.1

Using Boost 1.46.1 on Windows x86, Android TI 2.2

我已经定义了自己的测试套件树,因为我需要用户选择测试顺序.尽管我知道测试应该是独立的,但这是必需的.使用我自己的test_suite* init_unit_test_suite(int, char**)实现重新定义了测试套件树.

I have defined my own test suite tree, since I need the user to choose order of the tests. although I'm aware the tests should be independent, this is a requirement. The test suite tree was redefined using my own implementation of test_suite* init_unit_test_suite(int, char**).

对于自动测试用例和自动测试套件,有Boost宏:BOOST_FIXTURE_TEST_CASEBOOST_FIXTURE_TEST_SUITE( suite_name, F ).这些宏将功能注册到framework::master_test_suite(),在这种情况下,这是不希望的行为.

For automated test cases and automated test suites, there are Boost macros: BOOST_FIXTURE_TEST_CASE and BOOST_FIXTURE_TEST_SUITE( suite_name, F ). These macros register the function to the framework::master_test_suite(), which is undesired behavior in this case.

全局夹具(BOOST_GLOBAL_FIXTURE(fixure_name))在手动测试套件定义中不受影响.

Global fixture (BOOST_GLOBAL_FIXTURE(fixure_name)) remains unaffected in manual test suite definition.

我想在Boost Unit Testing Framework中使用夹具来手动定义测试套件和案例.整洁的方式.

I would like to use fixtures in Boost Unit Testing Framework for manually defined test suites and cases. A neat way.

有一些解决方法:

  • Test Suite治具-可以定义为第一个和最后一个测试这是儿童套房/案例.但是,这会影响测试结果,并且充当一个单独的测试,这并不是一个很好的解决方案.
  • 测试用例夹具-通过在测试用例周围包装作用域内的实例功能.
  • Test Suite Fixture - can be defined as a first and last test amongit's children suites/cases. This however affects the test results andacts as a separate test, which is not really a fine solution.
  • Test Case Fixture - by wrapping a scoped instance around the test casefunction.

是否有其他更清洁,更好的解决方案?我确实没有足够的资源来深入研究Boost库.另一方面,我不想大幅降低代码的质量和可读性.

Is there any other, cleaner and nicer solution to my problem? I don't really have resources to dig deep into Boost library. On the other hand, I don't want to significantly decrease the quality and readability of the code on my side.

LK致敬

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

BOOST_GLOBAL_FIXTURE(GFixture);

test_suite* init_unit_test_suite( int, char** )
{
    test_suite* ts1 = BOOST_TEST_SUITE( "Suite1" );

    boost::shared_ptr<TestClass1> test1 ( new TestClass1 );
    ts1->add( BOOST_TEST_CASE( boost::bind(&TestClass1::Run, test1)));
    boost::shared_ptr<TestClass2> test2 ( new TestClass2 );
    ts1->add( BOOST_TEST_CASE( boost::bind(&TestClass2::Run, test2)));
    boost::shared_ptr<TestClass3> test3 ( new TestClass3);
    ts1->add( BOOST_TEST_CASE( boost::bind(&TestClass3::Run, test3)));

    framework::master_test_suite().add( ts1 );
    return 0;
}

单元测试框架:用户指南
http://www.boost .org/doc/libs/1_46_1/libs/test/doc/html/utf/user-guide.html

Unit Test Framework: User's guide
http://www.boost.org/doc/libs/1_46_1/libs/test/doc/html/utf/user-guide.html

推荐答案

请考虑以下代码:

#define BOOST_TEST_MAIN
#include <boost/test/included/unit_test.hpp>
#include <boost/test/unit_test_suite.hpp>

BOOST_GLOBAL_FIXTURE( ... );

BOOST_AUTO_TEST_SUITE( Suite1 )

    BOOST_AUTO_TEST_CASE( Test1 )
    {
        TestClass1 testClass;

        testClass.Run();
    }


    BOOST_AUTO_TEST_CASE( Test2 )
    {
        TestClass2 testClass;

        testClass.Run();
    }

    BOOST_AUTO_TEST_CASE( Test3 )
    {
        TestClassT testClass;

        testClass.Run();
    }

BOOST_AUTO_TEST_SUITE_END()

这足以执行测试.

这篇关于在手动定义的套件树中增强测试用例和套件夹具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:52