mockpp安装使用说明
一、安装说明:
1、从网上下载源码包
网站URL:http://mockpp.sourceforge.net/
2、进行安装
# ./configure
#make
#make install
如果使用
# ./configure --enable-cppunit
#make
#make install
如果使用cppunit作为测试框架,需要先安装cppunit(参见cppunit安装使用说明)。
二、使用说明
1、在makefile中将-ldl -lmockpp -lcppunit这三个库加进来;
2、实例说明
class MockSummer : public Summer,public VisitableMockObject
其中Summer为接口类(没有实现,只定义了接口),VisitableMockObject为mockpp提供的基类;
class TestSum : public TestFixture
其中TestSum为cppunit测试类,TestFixture为cppunit提供的基类;
class User
这个类是我们要测试的目标类;在class User中我们会调用class Summer的接口,所以要用mockpp来模拟这个接口的实现。
class MockSummer : public Summer,public VisitableMockObject {
public:
MockSummer() : VisitableMockObject(MOCKPP_CHAR("MockSummer"),0)
,MOCKPP_CONSTRUCT_MEMBERS_FOR_VISITABLE_EXT2(sum,ext) {
}
MOCKPP_VISITABLE_EXT2(MockSummer,int,sum,int,int,int,ext,int,int);
};
class TestSum : public TestFixture
{
CPPUNIT_TEST_SUITE(TestSum);
CPPUNIT_TEST( testSum );
CPPUNIT_TEST_SUITE_END();
private:
MockSummer* mockSummer;
public:
void setUp();
void tearDown();
void testSum();
};
void TestSum::setUp()
{
this->mockSummer=new MockSummer();
}
void TestSum::tearDown()
{
delete mockSummer;
}
void TestSum::testSum()
{
MOCKPP_CONTROLLER_FOR_EXT(MockSummer,sum,ext)
sum_controller(mockSummer);
mockSummer->sum(20,10);
sum_controller.addReturnValue(30);
mockSummer->activate();
User user;
CPPUNIT_ASSERT(30==user.use(*mockSummer,20,10));
sum_controller.verify();
}
class Summer {
public:
virtual int sum(int,int) =0;
};
class User {
private:
//Summer& summer;
public:
User() {}
~User() {}
int use(Summer &summer,int x,int y) {
return summer.sum(x,y);
}
};
注:收集网上资料编写而成。