OS :ubuntu9.04
CppUnit :cppunit-1.12.1
1、 下载、解压
到http://sourceforge.net/projects/cppunit下载,然后复制cppunit-1.11.6.tar.gz到/usr/src;
运行:tar -xf cppunit-1.10.2.tar.gz 解压缩;或者在WIN下直接解压
2 、安装
进入cppunit-1.11.6目录下。依次运行下列命令
A :./configure;
B :make;
C :make check;
D:make install
3、 copy *.h 文件
.o, .a文件已经安装到/usr/local/lib中去了,但头文件没安装到/usr/include中去
把cppunit-1.10.2的cppunit目录复制到/usr/include下
4 、导入lib
运行时要先设置环境变量LD_LIBRARY_PATH到cppunit的安装目录,也就是/usr/local/lib,命令如下:
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
5、 编写测试程序
testApp.cpp
#include <iostream>
#include <cppunit/TestRunner.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
class Test : public CPPUNIT_NS::TestCase
{
CPPUNIT_TEST_SUITE(Test);
CPPUNIT_TEST(testHelloWorld);
CPPUNIT_TEST_SUITE_END();
public:
void setUp(void) {}
void tearDown(void) {}
protected:
void testHelloWorld(void) { std::cout << "Hello, world!" << std::endl; }
};
CPPUNIT_TEST_SUITE_REGISTRATION(Test);
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.wasSuclearcase/" target="_blank" >ccessful() ? 0 : 1;
}
6 、编译,运行
有两种方法
(a) 链接静态库。编译命令:
g++ -L/usr/local/lib/libcppunit.a testApp .cpp -lcppunit -ldl -o testApp
运行:
./ testApp
结果:
: OK
(b) 链接动态库。编译命令:
g++ testApp .cpp -lcppunit -ldl -o testApp
然后运行:
./ testApp
结果:
Test::
: OK
7、 其他例子
在cppunit-docs-1.11.6中有文档money_example.html,该文档详细的介绍了测试、开发的过程;
转载:http://blog.csdn.net/lengxingfei/archive/2006/04/19/669659.aspx
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Leeiee/archive/2009/07/07/4327741.aspx