一 準(zhǔn)備
在sourceforge.net上下載CPPUnit的源代碼,地址:http://sourceforge.net/projects/cppunit ,現(xiàn)在最新的版本:cppunit-1.12.0.tar.gz
二 參考
1)看CPPUnit的源代碼,在你下載的目錄下的src目錄下,還有examples目錄下的samples。
2)在源碼的工程下有一個txt的幫助文件:INSTALL-WIN32.txt, 很不錯,但是是英文的。
3)更多的幫助可以查看:
// http://www.vckbase.com/document/viewdoc/?id=1762
// http://www.codeproject.com/library/Using_CPPUnit.asp
// http://cppunit.sourceforge.net/doc/1.8.0/cppunit_cookbook.html#cppunit_cookbook
三 編譯(我使用vs編譯)
CPPUnit: Unit test 的核心框架庫
CPPUnit_dll:作用同上,區(qū)別是上面的是靜態(tài)的lib,此為動態(tài)的DLL,我們使用時可以從中任選一個就可以
TestRunner:MFC擴展的DLL,負責(zé)Report的GUI瀏覽
TestPlugInRunner:PlugIn方式運行時,用于運行實現(xiàn)了CPPUnit指定接口的PlugIn DLL
DllPlugInTester:PlugIn方式運行時,在plugIn dll的post event 中使用,在對Plugin dll的編譯時候檢測結(jié)果。
DSPlugIn:vc6需要的addin,當(dāng)某些case沒有通過是,雙擊可以跳到指定的代碼行,VS200*中不需要
四 實例 (只介紹使用宏定義來實現(xiàn),宏定義使用真是太簡單了,其他的原理和類的實現(xiàn)及關(guān)系可以參考上面的鏈接)
1)假設(shè)我們要測試的類:
//test data
class SampleClass


{
public:
int Add(int i,int j)

{
return i+j;
}
int Square(int i)

{
return i*i;
}
};
2)我們需要實現(xiàn)相應(yīng)的測試模塊test case:(此類繼承CppUnit::TestFixture或CPPUnit::TestCase,可以實現(xiàn)2個虛函數(shù)setup()和teardown(),他們在每個測試函數(shù)即case的前后自動調(diào)用)
class SampleClass_TestCase : public CppUnit::TestFixture


{
CPPUNIT_TEST_SUITE(SampleClass_TestCase);
CPPUNIT_TEST(Add_Test);
CPPUNIT_TEST(Square_Test);
CPPUNIT_TEST_SUITE_END();

public:
void setUp()

{
testClass = new SampleClass();
}

void tearDown()

{
delete testClass;
testClass = NULL;
}

protected:

void Add_Test()

{
CPPUNIT_ASSERT(6 == testClass->Add(1,4));
}
void Square_Test()

{
CPPUNIT_ASSERT(10 == testClass->Square(3));
}

public:
static std::string GetSuiteName();

private:
SampleClass *testClass;
};

std::string SampleClass_TestCase::GetSuiteName()


{
return "TestSample";
}
3)增加上面的testcase到test suite中,當(dāng)然test suite的名字是可以自己定義的。
//CPPUNIT_TEST_SUITE_REGISTRATION(SampleClassTestCase);
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(SampleClass_TestCase,SampleClass_TestCase::GetSuiteName());
4)增加上面的test suite到test runner,testrunner負責(zé)所有test suite的管理。可以使用CPPUNIT_REGISTRY_ADD( which, to ) 建立suite的樹形關(guān)系。比如:CPPUNIT_REGISTRY_ADD( A, B ) ,CPPUNIT_REGISTRY_ADD( B, C )
CPPUNIT_REGISTRY_ADD_TO_DEFAULT(SampleClass_TestCase::GetSuiteName());


5)調(diào)用Testrunner,顯示結(jié)果,一般有三種方式:
第一種:GUI的方式,需要我們建立一個MFC的exe,把下面的代碼加入,在我們的theapp::InitInstance()中調(diào)用下面的函數(shù),且函數(shù)要改為返回true:
// 1) run ui for output in mfc dialog project(.exe)

void TestMain()


{
// declare a test runner, fill it with our registered tests and run them
CppUnit::MfcUi::TestRunner runner;
runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest());

runner.run();//show UI

}
第二種:console的方式,需要建立一個console的exe,把下面的代碼加入,在main中調(diào)用下面的函數(shù):
// 2) run console for output in console project(.exe)

void TestMain()


{
// 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 );

// Print test in a compiler compatible format.
CPPUNIT_NS::CompilerOutputter outputter( &result, CPPUNIT_NS::stdCOut() );
outputter.write();
}
第三種:PlugIn的方式,建立一個DLL,加入下面的代碼,且必須在此DLL中調(diào)用下面的宏之一:(需要注意的是你的dll本身有沒有main函數(shù))
我們可以對此DLL的post -event事件中增加對DllPlugInTesterud.exe的調(diào)用來在編譯的時候就得到結(jié)果, 或?qū)幾g好的DLL使用TestPlugInRunner來打 開,用GUI的方式查看結(jié)果。
// //Implements all the plug-in stuffs, WinMain
CPPUNIT_PLUGIN_IMPLEMENT();
// //or
// //only export function not has main function. if you have main ,use below macro
// CPPUNIT_PLUGIN_EXPORTED_FUNCTION_IMPL( CPPUNIT_NS::TestPlugInDefaultImpl );
四 更多
1)一般需要使用的頭文件:
#include <cppunit/BriefTestProgressListener.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestRunner.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/mfc/TestRunner.h>
2)一般需要使用的宏定義:
//CPPUNIT_ASSERT(condition): checks condition and throws an exception if it's false.
//CPPUNIT_ASSERT_MESSAGE(message, condition): checks condition and throws an exception and showing 
//specified message if it is false.
//CPPUNIT_ASSERT_EQUAL(expected,current): checks if expected is the same as current, and raises exception 
//showing expected and current values.
//CPPUNIT_ASSERT_EQUAL_MESSAGE(message,expected,current): checks if expected is the same as actual, and 
//raises exception showing expected and current values, and specified message.
//CPPUNIT_ASSERT_DOUBLES_EQUAL(expected,current,delta): checks if expected and current difference is
//smaller than delta. If it fails, expected and current values are shown.
3)如果我們對想對我們的整個工程下的函數(shù)進行單元測試,且想使測試代碼和我們的正式代碼分離,這時我們可以為我們的測試代碼建立一個單獨的dll(使用plugin)或exe(輸出到GUI或console),在此測試工程中加入我們正式的代碼,但是不對我們正式的代碼進行拷貝,但是如果我們的project的靜態(tài)的lib,則我們可以直接調(diào)用不需要把代碼再加入測試模塊中。