当一个接口需要重复测试时,就需要考虑将这个重复的次数放到什么地方了,RepeatedTest就是用来装饰一个普通测试用例的。其声明如下:
class CPPUNIT_API RepeatedTest : public TestDecorator //TestDecorator直接从Test派生
{
public:
RepeatedTest( Test *test,
int timesRepeat ) :
TestDecorator( test ),
m_timesRepeat(timesRepeat)
{
}
void run( TestResult *result );
int countTestCases() const;
private:
RepeatedTest( const RepeatedTest & );
void operator=( const RepeatedTest & );
const int m_timesRepeat;
};
可以看到它的构造函数接受一个Test指针和运行次数作为参数,也就是用重复的次数来修饰这个普通的Test对象这个Test对象可能是TestSuite对象,也可能是TestCaller对象,如果是TestSuite对象就会将它包含的测试用例全部重复运行,它能这样做,RepeatedTest的run方法是关键:
// Runs a repeated test
void
RepeatedTest::run( TestResult *result )
{
for ( int n = 0; n < m_timesRepeat; n++ )
{
if ( result->shouldStop() )
break;
TestDecorator::run( result );
}
}
所以如果想重复测试的话就这样做:
// Add the top suite to the test runner
CPPUNIT_NS::TestRunner runner;
CPPUNIT_NS::Test* pTest = CPPUNIT_NS::TestFactoryRegistry::getRegistry("My Test").makeTest();
CPPUNIT_NS::RepeatedTest* pRepeatedTest = new CPPUNIT_NS::RepeatedTest(pTest, 4);
runner.addTest( pRepeatedTest );
这样整个测试套件包含的测试用例会被重复执行4次,可以看出装饰类也不象经典定义的那样,仅仅打印下footer和header之类的东西,它需要接受参数来装饰被装饰的类。再整点有意思的:
CPPUNIT_NS::RepeatedTest* pRepeatedTest_2 =
new CPPUNIT_NS::RepeatedTest(new CPPUNIT_NS::RepeatedTest(pTest, 4), 5);
这样就显示出装饰链的样子了,这个测试套件会被测试20次!
class CPPUNIT_API RepeatedTest : public TestDecorator //TestDecorator直接从Test派生
{
public:
RepeatedTest( Test *test,
int timesRepeat ) :
TestDecorator( test ),
m_timesRepeat(timesRepeat)
{
}
void run( TestResult *result );
int countTestCases() const;
private:
RepeatedTest( const RepeatedTest & );
void operator=( const RepeatedTest & );
const int m_timesRepeat;
};
可以看到它的构造函数接受一个Test指针和运行次数作为参数,也就是用重复的次数来修饰这个普通的Test对象这个Test对象可能是TestSuite对象,也可能是TestCaller对象,如果是TestSuite对象就会将它包含的测试用例全部重复运行,它能这样做,RepeatedTest的run方法是关键:
// Runs a repeated test
void
RepeatedTest::run( TestResult *result )
{
for ( int n = 0; n < m_timesRepeat; n++ )
{
if ( result->shouldStop() )
break;
TestDecorator::run( result );
}
}
所以如果想重复测试的话就这样做:
// Add the top suite to the test runner
CPPUNIT_NS::TestRunner runner;
CPPUNIT_NS::Test* pTest = CPPUNIT_NS::TestFactoryRegistry::getRegistry("My Test").makeTest();
CPPUNIT_NS::RepeatedTest* pRepeatedTest = new CPPUNIT_NS::RepeatedTest(pTest, 4);
runner.addTest( pRepeatedTest );
这样整个测试套件包含的测试用例会被重复执行4次,可以看出装饰类也不象经典定义的那样,仅仅打印下footer和header之类的东西,它需要接受参数来装饰被装饰的类。再整点有意思的:
CPPUNIT_NS::RepeatedTest* pRepeatedTest_2 =
new CPPUNIT_NS::RepeatedTest(new CPPUNIT_NS::RepeatedTest(pTest, 4), 5);
这样就显示出装饰链的样子了,这个测试套件会被测试20次!