[1]. 首先下载CppUnit自动测试工具(Linux 和 Windows 都可以用)
http://sourceforge.net/projects/cppunit/
cppunit-1.12.1.tar.gz
然后解压。
gunzip < cppunit-1.12.1.tar.gz | tar -xvf -
[2]. LINUX
进入解压后的目录, 运行命令
(要授执行权限: chown 777 xxx )
./configure --disable-shared
make
make install // error: 没创建文件夹权限mkdir
make clean
成功后在目录/usr/local/include 下有个cppunit文件夹,里面是需要的头文件,
/usr/local/lib 有需要的库文件。
-------------------------------------------------------------------
下面是LINUX测试代码:
建一个文件testCppUnit.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
{
public:
void setUp(void) {}
void tearDown(void) {}
protected:
void testHelloWorld(void) { std::cout << "Hello" << std::endl; }
private:
CPPUNIT_TEST_SUITE(Test);
CPPUNIT_TEST(testHelloWorld); // only Modify function: testHelloWorld()
CPPUNIT_TEST_SUITE_END();
};
CPPUNIT_TEST_SUITE_REGISTRATION(Test);
int main(void)
{
CPPUNIT_NS::TestResult controller;
CPPUNIT_NS::TestResultCollector result;
controller.addListener( &result );
CPPUNIT_NS::BriefTestProgressListener progress;
controller.addListener( &progress );
CPPUNIT_NS::TestRunner runner;
runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );
runner.run( controller );
return result.wasSuccessful() ? 0 : 1;
}
-------------------------------------------------------
Makefile文件(里面用了Valgrind检查内存情况):
######################################
# Makefile
######################################
DIR = .
INCLUDE = -I./inc -I/usr/local/include
SOURCE = ./src
LIBS = -L./lib -L/usr/local/lib
CC = g++
CCFLAG = -Wall -g
MAINCPP = $(SOURCE)/testCppUnit.cpp
OBJECT = main
all : $(OBJECT)
$(OBJECT) : testCppUnit.o
$(CC) $(CCFLAG) -o $(OBJECT) testCppUnit.o /
$(LIBS) -lcppunit
testCppUnit.o : $(MAINCPP)
$(CC) $(INCLUDE) $(CCFLAG) -c $(MAINCPP)
valgrind:
valgrind --tool=memcheck --leak-check=full --show-reachable=yes /
--db-attach=yes -v -q --log-file-exactly=./valgrindLog.txt ./$(OBJECT)
clean:
/rm -rf *.o $(OBJECT) valgrindLog.txt
#########################################################
[3]. WINDOWS环境(使用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. 把所有lib文件放置到CommonFiles/Lib目录下面。
或者 在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".
7.可以引入宏AddingUnitTestMethod.dsm,可以方便生成测试框架
### END ###