转自:http://blog.chinaunix.net/u/13995/showart_263738.html
配置方法:http://blog.csdn.net/LuckilyYu/archive/2009/02/10/3874819.aspx
CppUnit 测试方法说明
目录
解压缩cppunit-1.12.0.tar.gz。,在/src目录下,将CppUnitLibraries.dsw工程用VC打开。
1. 编译cppunit目录\src\cppunit\CppUnitLibraries.dsw中的cppunit_dll项目,release版生成cppunit_dll.lib和cppunit_dll.dll,debug版生成cppunitd_dll.lib和cppunitd_dll.dll。这是CPPUnit基本类库。
2. 编译cppunit目录\src\cppunit\CppUnitLibraries.dsw中的TestRunner项目。release版生成TestRunner.lib和TestRunner.dll,debug版生成TestRunnerd.lib和TestRunnerd.dll。这是使用MFC的图形化界面的类库。
3. 在VC中工具->选择->目录->Library Files中将cppunit中的lib目录放进来
4. 把dll文件放到相应的debug和release可执行目录下面。
5. 在VC中工具->选择->目录->Include Files中将cppunit中的include目录放进来
6.vc中工程->设置->link 放入cppunitd_dll.lib
在Projects/Settings.../C++/C++ Language页选中Enable RTTI。
在Projects/Settings.../C++/Code Generation页选择Use run-time library中的内容:
Release版, 选择"Mulithreaded DLL".
Debug版, 选择 "Debug Multihreaded DLL".
使用如下命令解压cppunit-1.12.0.tar.gz包:
tar –zvxf cppunit-1.12.0.tar.gz
进入cppunit-1.12.0,
./configure
make
make install
make编译的文件都在src/cppunit/lib下,makeinstall仅仅是把连接库文件copy到/usr/local/lib中,我们需要把include目录下的cppunit目录复制到/usr/include/下。
cp –rdf cppunit /usr/include
配置链接库,链接库的配置文件是/etc/ld.so.conf
vi /etc/ld.so.conf
在新起一行,即如代码:
/usr/local/lib
之后重新加载链接库的配置文件
ldconfig
建立一个win32 console application工程,加入来两个文件
// TestHello.cpp : Defines the entry point for the console application.
#include <cppunit/ui/text/TestRunner.h>//以text方式测试,其他方式还有mfc、qt,后续介绍
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/extensions/HelperMacros.h>
#include "TestHello.h"
#ifdef _DEBUG
#pragma comment( lib, "testrunnerd.lib" )
#pragma comment( lib, "cppunitd.lib" )
#else
#pragma comment( lib, "testrunner.lib" )
#pragma comment( lib, "cppunit.lib" )
#endif
using namespace CppUnit;
void MyTest::testHelloWorld()//实际的测试方法的实现
{
CMyPlus plus;// CMyPlus就是我们要测试的类
CPPUNIT_ASSERT(plus.Add(1,2) == 2);
//CPPUNIT_ASSERT(1 == 6.0);
}
CPPUNIT_TEST_SUITE_REGISTRATION(MyTest);
int main(int argc, char* argv[])
{
// Create the event manager and test controller
CPPUNIT_NS::TestResult controller;
// Add a listener that colllects test result
CPPUNIT_NS::TestResultCollector result;
controller.addListener( &result );
// Add a listener that print dots as test run.
CPPUNIT_NS::BriefTestProgressListener progress;
controller.addListener( &progress );
// Add the top suite to the test runner
CPPUNIT_NS::TestRunner runner;
runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );
runner.run( controller );
return result.wasSuccessful() ? 0 : 1;
}
TestHello.h文件
// TestHello.h : Defines the entry point for the console application.
class MyTest : public CPPUNIT_NS::TestCase
{
CPPUNIT_TEST_SUITE(MyTest);//测试组
CPPUNIT_TEST(testHelloWorld);//加入测试方法
CPPUNIT_TEST_SUITE_END();
public:
void setUp(void) {}
void tearDown(void) {}
protected:
void testHelloWorld(void);//实际的测试方法
};
之后编译运行,从控制台可以看到运行的结果。
环境设置在上边已经介绍了,首先建立一个MFC工程MyTestCase。
在MyTestCase.cpp之中包含头文件:
#include <cppunit/ui/mfc/TestRunner.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
之后把InitInstance函数修改成如下:
BOOL CMyTestCaseApp::InitInstance()
{
// AfxEnableControlContainer();
CppUnit::MfcUi::TestRunner runner;
//runner.addTest(TestFactoryRegistry::getRegistry("CPlusTestCase").makeTest()); //Ìí¼Ó²âÊÔ
runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );
runner.run(); //show UI
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
return FALSE;
}
目的就是不使用原来的对话框,而采用CppUnit的测试对话框。
在stdafx.h之中添加如下代码:
#include <cppunit/ui/mfc/TestRunner.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/extensions/HelperMacros.h>
#ifdef _DEBUG
#pragma comment( lib, "testrunnerd.lib" )
#pragma comment( lib, "CppUnitd.lib" )
#else
#pragma comment( lib, "testrunner.lib" )
#pragma comment( lib, "CppUnit.lib" )
#endif
using namespace CppUnit;
到目前MFC的环境搭建好,下面开始写我们的测试用例代码:
添加两个各文件:
// CPlusTestCase.h
class CPlusTestCase : public CppUnit::TestCase
{
CPPUNIT_TEST_SUITE(CPlusTestCase);
CPPUNIT_TEST(testAdd);
CPPUNIT_TEST_SUITE_END();
public:
CPlusTestCase();
virtual ~CPlusTestCase();
void testAdd();
};
CPlusTestCase.cpp文件
// CPlusTestCase.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "CPlusTestCase.h"
#include "Plus\MyPlus.h"
#include "Plus\MyPlus.cpp"
//CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CPlusTestCase,"CPlusTestCase");
CPPUNIT_TEST_SUITE_REGISTRATION(CPlusTestCase);
CPlusTestCase::CPlusTestCase()
{
}
CPlusTestCase::~CPlusTestCase()
{
}
void CPlusTestCase::testAdd()
{
CMyPlus plus;// CMyPlus就是我们要测试的类
CPPUNIT_ASSERT(plus.Add(1,2) == 2);
//CPPUNIT_ASSERT(1 == 6.0);
}
至此就可以编译测试了
(图片)
我们的被测试类很简单:
#ifndef MYPLUS_H
#define MYPLUS_H
class CMyPlus
{
public:
CMyPlus();
~CMyPlus();
int Add(const int a, const int b){return a + b;}
};
#endif //MYPLUS_H
配置步骤:
1、解压缩cppunit-1.12.0.tar.gz,之后进入目录执行如下操作:
./configure
./make
./make install
2、可以使用以下命令验证安装是否成功
ls /usr/local/lib/libcppunit*
代码同win的文本方式,编译方式如下:
[root@localhost mnt]# /usr/bin/g++ -g -o helloworld TestHello.cpp -I. -I./h -I/usr/include -I/usr/local/incluse -L/usr/lib -L/usr/local/lib -ldl -lm -lpthread -lcppunit -static
之后执行
[root@localhost mnt]# ./helloworld
MyTest::testHelloWorld : OK
CppUnit提供的断言:
CPPUNIT_ASSERT(condition) // 确信condition为真
CPPUNIT_ASSERT_MESSAGE(message, condition) // 当condition为假时失败, 并打印message
CPPUNIT_FAIL(message) // 当前测试失败, 并打印message
CPPUNIT_ASSERT_EQUAL(expected, actual) // 确信两者相等
CPPUNIT_ASSERT_EQUAL_MESSAGE(message, expected, actual) // 失败的同时打印message
CPPUNIT_ASSERT_DOUBLES_EQUAL(expected, actual, delta) // 当expected和actual之间差大于delta时失败
运行方式:
CpUnit::TextUi::TestRunner // 文本方式的TestRunner
CppUnit::QtUi::TestRunner // QT方式的TestRunner
CppUnit::MfcUi::TestRunner // MFC方式的TestRunner