学习笔记:解读CppUnit源码2
2009-11-09 12:40
459 查看
上次我详细的剖析了与Test.h相互关联的代码。Test类是cppUnit的核心。Test这个类相信看了上一章就知道,也就起到测试用例的作用,但是毕竟一个类的功能有限,如何扩充Test类的功能呢?下面装饰者模式就登场了。
TestDecorator.h
这个类中保存了一份Test的指针,这个类的countTestCases方法,run方法和getChildTestCount方法,其实都是调用这个指针对应的方法,当然一个也只是装饰的基类,而真正的类功能扩展是放在它的子类中。
RepeatedTest.h
这个类加了一个m_timesRepeat变量(扩展),就实现了单个测试用例的重复测试。
TestSetUp.h
跟上面一个相比,它做的不是类成员变量的扩充而是方法的扩充。这块代码是对Decorator模式的美妙运用,这样想再扩展Test抽象类功能的时候就变得方便多了。因为不用该其他类了。相同的代码还运用在了TestCaseDecorator这个地方。代码基本相同,在这里就不废话了。
下面该说说另外一个很重要的类,TestFixture,相信从这个名字中大家就可以知道了这个是测试的装置器,因为在面向对象的类的测试中,不光光是执行一个类中的一个共有的方法就可以了,至少应该要做被测类的new以及相关测试所必需的成员变量的赋值等等。而装置器正是做这些事情。前面讲的TestCase就多重继承了它。
TestFixture.h
因为是空的虚函数,那么setUp和tearDown这两个方法是在哪里实现的呢?当然是在具体的测试类中实现(如ExampleTestCase,它继承自TestFixture,然后再扩展这两个方法的功能)。
TestDecorator.h
//装饰者模式:在TestDecorator的派生类中,这些功能将得到扩展 class CPPUNIT_API MyTestDecorator : public MyTest { public: MyTestDecorator( MyTest *test ) { m_test = test; }; ~MyTestDecorator() { delete m_test; }; int countTestCases() const { m_test->countTestCases(); }; std::string getName() const { m_test->getName(); }; void run( MyTestResult *result ) { m_test->run(result); }; int getChildTestCount() const { m_test->getChildTestCount(); }; protected: MyTest *doGetChildTestAt( int index ) const { return m_test->getChildTestAt( index ); }; MyTest *m_test;//一个指向Test实例的指针 private: MyTestDecorator( const MyTestDecorator &); MyTestDecorator& operator =( const MyTestDecorator & ); };
这个类中保存了一份Test的指针,这个类的countTestCases方法,run方法和getChildTestCount方法,其实都是调用这个指针对应的方法,当然一个也只是装饰的基类,而真正的类功能扩展是放在它的子类中。
RepeatedTest.h
//测试运行重复 class CPPUNIT_API MyRepeatedTest : public MyTestDecorator { public: MyRepeatedTest( MyTest *test, int timesRepeat ) : MyTestDecorator( test ), m_timesRepeat(timesRepeat) { }; //重复调用基类的run void run( MyTestResult *result ) { for ( int n = 0; n < m_timesRepeat; n++ ) { if ( result->shouldStop() ) break; MyTestDecorator::run( result ); } }; int countTestCases() const { return MyTestDecorator::countTestCases () * m_timesRepeat; }; private: MyRepeatedTest( const MyRepeatedTest & ); MyRepeatedTest& operator=( const MyRepeatedTest & ); const int m_timesRepeat;//测试重复运行指定的次数 };
这个类加了一个m_timesRepeat变量(扩展),就实现了单个测试用例的重复测试。
TestSetUp.h
//它使测试类具有了SetUp和TearDown的特性 class CPPUNIT_API MyTestSetUp : public MyTestDecorator { public: MyTestSetUp( MyTest *test ) : TestDecorator( test ){}; void run( MyTestResult *result ) { setUp(); MyTestDecorator::run(result); tearDown(); }; protected: //供派生类覆盖 virtual void setUp(); virtual void tearDown(); private: MyTestSetUp( const MyTestSetUp & ); MyTestSetUp& operator =( const MyTestSetUp & ); };
跟上面一个相比,它做的不是类成员变量的扩充而是方法的扩充。这块代码是对Decorator模式的美妙运用,这样想再扩展Test抽象类功能的时候就变得方便多了。因为不用该其他类了。相同的代码还运用在了TestCaseDecorator这个地方。代码基本相同,在这里就不废话了。
下面该说说另外一个很重要的类,TestFixture,相信从这个名字中大家就可以知道了这个是测试的装置器,因为在面向对象的类的测试中,不光光是执行一个类中的一个共有的方法就可以了,至少应该要做被测类的new以及相关测试所必需的成员变量的赋值等等。而装置器正是做这些事情。前面讲的TestCase就多重继承了它。
TestFixture.h
//该类也是抽象类,用于包装测试类使之具有setUp方法和tearDown方法。 //利用它,可以为一组相关的测试提供运行所需的公用环境 class CPPUNIT_API MyTestFixture { public: virtual ~MyTestFixture() {}; //进行一些初始化的工作 virtual void setUp() {}; //清理环境 virtual void tearDown() {}; };
因为是空的虚函数,那么setUp和tearDown这两个方法是在哪里实现的呢?当然是在具体的测试类中实现(如ExampleTestCase,它继承自TestFixture,然后再扩展这两个方法的功能)。
相关文章推荐
- 学习笔记:解读CppUnit源码7
- 学习笔记:解读CppUnit源码8
- 学习笔记:解读CppUnit源码3
- Spark学习笔记(7)源码解读之JobScheduler内幕实现
- Spark学习笔记(15)Spark Streaming源码解读之No Receivers
- Feign学习笔记2-源码解读
- (一)Netty源码学习笔记之概念解读
- OpenCv学习笔记(一)--OpenCv中Mat类源码的详细解读(1)
- spring学习笔记之DispatcherServlet源码解读
- Spark学习笔记(11)源码解读之Driver中的ReceiverTracker架构
- spring学习笔记之handler mapping源码解读
- jfinal学习笔记02 源码解读JFinalFilter
- Spark学习笔记(12)源码解读之Executor容错安全性
- spring学习笔记之AbstractController源码解读
- Spark学习笔记(6)源码解读之Job动态生成和深度思考
- CppUnit源码解读
- CAFFE源码学习笔记之一
- zepto.1.1.6.js源码中的each方法学习笔记
- nginx 源码学习笔记(二)——nginx精粹-模块