- 浏览: 111096 次
- 来自: ...
文章分类
最新评论
设计原则与模式: 案例介绍--CppUnit
CppUnit 是一个单元测试框架, 我们看一看它的设计是如何遵循基础的设计原则和模式的
单一职责原则
TestRunner 和 TestResult 的分离
class CPPUNIT_API TestRunner {
virtual void addTest( Test *test );
virtual void run( TestResult &result, const std::string &testPath = "" );
...
};
TestRunner 负责收集并运行测试用例, 但并不主动打印测试结果. 测试结果被收集在 TestResult 对象中, 可以以各种形式被处理:
CPPUNIT_NS::TestResult controller;
CPPUNIT_NS::TestResultCollector result;
controller.addListener( &result );
...
runner.run( controller );
// Print test in a compiler compatible format.
CPPUNIT_NS::CompilerOutputter compiler_outputter( &result, CPPUNIT_NS::stdCOut() );
compiler_outputter.write();
// Print test in XML format.
CPPUNIT_NS::XmlOutputter xml_outputter( &result, CPPUNIT_NS::stdCOut() );
xml_outputter.write();
开放封闭原则
遵循开放封闭原则的一个重要特征就是 "针对接口/基类编程", 任何根据 typeid 等类型信息进行的分支处理如 if/else, switch/case 等都可以看做是破坏开放封闭原则的前兆
TestResult 对于如何处理测试过程中发生的事件是开放的, 可以通过 TestListener 来扩展
class CPPUNIT_API TestResult : protected SynchronizedObject {
virtual void addListener( TestListener *listener );
virtual void removeListener( TestListener *listener );
/// Informs TestListener that a test will be started.
virtual void startTest( Test *test );
...
};
void TestResult::startTest( Test *test ) {
...
for ( TestListeners::iterator it = m_listeners.begin(); it != m_listeners.end(); ++it )
(*it)->startTest( test );
}
};
void BriefTestProgressListener::startTest ( Test *test ) {
stdCOut () << test-> getName ();
stdCOut ().flush();
m_lastTestFailed = false ;
}
void TextTestProgressListener::startTest ( Test *test ){
stdCOut() << "." ;
}
接口分离原则
从 TestRunner 的角度来讲, 它只需要把测试用例 "运行" 起来, 并不需要关心测试用例是如何运行的, 如何搭建和清理运行环境, 它所需要的 Test 对象的接口, 只需要一个 run( ) 方法 :
class CPPUNIT_API Test {
virtual void run ( TestResult *result ) =0;
};
void TestRunner::addTest ( Test *test ) {
m_suite -> addTest ( test );
}
void TestRunner::run ( TestResult &controller, const std:: string &testPath ) {
TestPath path = m_suite -> resolveTestPath ( testPath );
Test *testToRun = path.getChildTest();
controller. runTest ( testToRun );
}
但是总有一个类需要负责搭建和清理测试环境:
class CPPUNIT_API TestFixture {
virtual void setUp () {};
virtual void tearDown () {};
};
class CPPUNIT_API TestCase : public Test , public TestFixture {
};
TestCase 这个类分别实现了Test 和TestFixture 两个分离的接口, 这样TestCase 的客户只需要按需依赖 Test 或 TestFixture 就可以了
依赖倒置原则
后来被发展为依赖注入或者控制反转模式,思想就是依赖于抽象,而不是具体的类
TestRunner 并不依赖具体的 TestCase/TestSuite 等, 它只依赖于 Test. 在main函数中由 TestFactoryRegistry::makeTest() 产生 具体的Test对象, 注入到TestRunner中
class CPPUNIT_API TestRunner {
virtual void addTest( Test *test );
virtual void run( TestResult &result, const std::string &testPath = "" );
...
};
int main ( int argc, char * argv[] ) {
// Add the top suite to the test runner
CPPUNIT_NS:: TestRunner runner;
runner. addTest ( CPPUNIT_NS:: TestFactoryRegistry :: getRegistry (). makeTest () );
runner. run ( controller );
...
}
Test
*
TestFactoryRegistry::makeTest
() {
TestSuite *suite = new TestSuite ( m_name );
addTestToSuite ( suite );
return suite;
}
Composite 模式
TestSuite 本身也是 Test, 但可以包含很多其它 Test
class CPPUNIT_API TestComposite : public Test {
...
};
class CPPUNIT_API TestSuite : public TestComposite {
void addTest ( Test *test );
};
客户代码参见前面的 main 函数
Decorator 模式
1. 如何重复运行测试用例?
重复运行也是运行, 只是添加了额外的一种职责: 运行多次
class CPPUNIT_API TestDecorator : public Test {
...
};
class CPPUNIT_API RepeatedTest : public TestDecorator {
RepeatedTest ( Test *test, int timesRepeat ) : TestDecorator ( test ), m_timesRepeat (timesRepeat) {
}
void run ( TestResult *result ) {
for ( int n = 0; n < m_timesRepeat; n++ ){
TestDecorator::run( result );
}
}
...
};
2. 如何确保 TestCase 的隔离性, 即使出现异常也不影响后续 TestCase 的运行 ?
ProtectedFunctor -> Functor -> 函数指针
class TestCaseMethodFunctor : public Functor {
typedef void ( TestCase ::* Method )();
TestCaseMethodFunctor ( TestCase *target, Method method ) : m_target ( target ), m_method ( method ) {
}
bool operator() () const {
( m_target ->* m_method )();
return true ;
}
...
};
bool DefaultProtector::protect ( const Functor &functor, const ProtectorContext &context ) {
try {
return functor();
} catch ( Exception &failure ) {
reportFailure( context, failure );
} catch ( std::exception &e ) {
reportError( context, Message( "uncaught exception of type " , e.what() ) );
} catch ( ... ) {
reportError( context, Message( "uncaught exception of unknown type" ) );
}
return false ;
}
练习 : 这里使用了 Decorator 模式的思想, 但并不是严格的 Decorator 模式, 为什么?
Observer 模式
TestResult 与 TestListener
void TestResult::addListener ( TestListener *listener ) {
m_listeners .push_back( listener );
}
void TestResult::removeListener ( TestListener *listener ) {
removeFromSequence( m_listeners , listener );
}
void TestResult::startTestRun ( Test *test ) {
for ( TestListeners :: iterator it = m_listeners .begin(); it != m_listeners .end(); ++it )
(*it)->startTestRun( test, this );
}
void TestResult::endTestRun ( Test *test ) {
for ( TestListeners :: iterator it = m_listeners .begin(); it != m_listeners .end(); ++it )
(*it)->endTestRun( test, this );
}
Factory Method 模式
调用测试用例注册表来产生测试用例
class CPPUNIT_API TestFactoryRegistry : public TestFactory {
virtual Test *makeTest ();
};
Strategy 模式
不同的格式的输出, 如Compiler风格的, Xml格式的, 等等
class CPPUNIT_API TextTestRunner : public CPPUNIT_NS:: TestRunner {
void
setOutputter
(
Outputter
*outputter )
{
delete
m_outputter
;
m_outputter
= outputter;
}
void
printResult
(
bool
doPrintResult ) {
m_outputter -> write ();
}
};
CPPUNIT_NS::
TextTestRunner
runner;
CPPUNIT_NS:: CompilerOutputter compiler_outputter ( ... );
runner.setOutputter(compiler_outputter);
Template Method 模式
TestFixture 的 setUp 与 tearDown
class
CPPUNIT_API
TestFixture
{
virtual void setUp () {};
virtual void tearDown () {};
};
void TestCase::run ( TestResult *result ) {
result-> startTest ( this );
if ( result->protect( TestCaseMethodFunctor( this , & TestCase ::setUp ), this , "setUp() failed" ) ) {
result->protect( TestCaseMethodFunctor( this , & TestCase :: runTest ), this );
}
result->protect( TestCaseMethodFunctor( this , & TestCase ::tearDown ), this , "tearDown() failed" );
result-> endTest ( this );
}
练习 : CppUnit 支持将测试结果输出到控制台, 或者文件, 但缺省并不支持同时输出到控制台和文件, 如何在遵循各种设计原则的情况下, 为 CppUnit 添加此功能?
发表评论
-
The Object Primer
2004-12-11 11:21 5501,书名 被翻译成“ ... -
错误处理规范
2004-12-11 16:47 725错误处理规范 〇、概念澄清 概念 解释 错误 ... -
Java,误解为何如此之深
2005-08-24 13:50 559前几天被电话面试,问J ... -
Java:画蛇添足的编码规范
2005-09-02 13:13 540前几天公司培训编码规范: 第n条: ... -
synchronized : 规则, 推论与实践
2007-07-23 22:32 42714.3.Synchronization. Rule ... -
交互设计: 股市帮凶
2008-05-04 21:30 609同事 Y 在线操作股票时, 把"买入"点成 ... -
交互设计: 火车上的厕所
2008-05-26 17:17 572有人在动车组的厕所前等了很久, 直到乘务员路过说厕所是被锁住了 ... -
工作流:形参,实参,相关数据
2004-12-11 11:40 623关于形参,实参,相关数据 一、形参(FormalParame ... -
工作流:第一次发版,过程总结
2004-12-11 11:42 667交流 即时讨论:小组成员咫尺之遥,有问题立即提出并解决 ... -
工作流:第一次发版,设计总结
2004-12-11 11:43 578整体 面向接口:消息系统,持久系统等,其实现都是可替换 ... -
Beyond Workflow : An Introduction to Vitria BusinessWare
2005-09-26 10:13 778一、简介 Busines ... -
Vitria BusinessWare: 存储与访问安全
2006-03-26 15:45 709事实上,BusinessWare使用LDAP做为存储机制和 ... -
Vitria BusinessWare: 平台与软件总线
2006-04-01 12:59 779经过一段时间的使用 ... -
Vitria BusinessWare: Web Services
2006-04-01 14:30 706BusinessWare的Web Services ... -
Web Services:自洽,编码,交换模型
2006-04-01 16:02 6161, 自洽 以前曾经写过: 目前WebServi ... -
Web Services:WSDL 1.1 规范中的几个错误
2006-04-01 16:40 680读完了WSDL 1.1的规范,令人惊讶的是发现似乎例子中有几个 ... -
C++/CLI:被忽视的集成技术
2006-05-17 20:02 715十几行代码,就使一个重要的旧系统组件,完全融入了基于.Ne ... -
AJP/JK:异构Web平台的集成技术
2006-05-25 21:44 666Tomcat Connector 可以将Tomcat ... -
Vitria BusinessWare: 事件与端口
2006-05-27 17:24 590Event BusinessWare是一个事件驱动的系统 ... -
WS-Security Interoperability Issues: WebSphere, JBoss, Axis and .Net
2006-08-31 22:46 796检验了一下WS-Security的互操作情况,涉及到 ...
相关推荐
CPPUNIT-1.12.1 已经编译好,可以直接使用 另外还包括ProjectWizard
cppunit-1.12.0 最新版本的c++ 单元测试工具。
含两个文件:Cppunit-1.12.1,以及CPPUNIT的测试框架模板实例Demo一个.相关使用方法详见我的CSDN博文
cppunit-1.12.1 用于单元测试的工具 方便大家哦
官方离线安装包,亲测可用
cppunit-1.10.2.tar.gz,单元测试利器
CppUnit测试是软件开发过程中必不可少的一个部分,是软件质量的保障的重要手段. 单元测试作为代码级最小的单位的测试,在软件开发过程中起着举足轻重的作用。
CppUnit-Guide.doc CppUnit-Guide.doc
2007-03-26 20:08 446,921 cppunit-docs-1.12.0.tar.gz 2007-03-26 20:09 177,009 MSDNIntegrator-1.0.zip 4 个文件 1,610,335 字节 2 个目录 422,510,592 可用字节 G:\培训资料\cppunit\Install 的目录 2008-...
商业编程-源码-CppUnit测试框架入门.zip
CPPUNIT单元测试 -- 测试程序模板 在此基础上, 可以增添其他测试代码
与前一种情况相比,它提供了更好的控制和多功能性。 课程: :... :... CppUnit 库 您可以在以下位置找到 CppUnit 库: 及其文档: linux环境下安装命令: sudo apt-get install libcppunit-dev 使用 QT ...
目前最稳定的cpp单位测试框架。虽然这个可以在sourceforge上下载,但是这个网站速度实在是太慢了。
CPPUnit
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
CPPUNIT使用说明与环境配置 CPPUNIT使用说明与环境配置 CPPUNIT使用说明与环境配置 CPPUNIT使用说明与环境配置 CPPUNIT使用说明与环境配置
单元测试工具 cppunit 的最新版本
CppUnit的安装包,版本是1.12.1