1 資源下載
使用的是cppunit-1.12.1 ,在網上下載後 需要自己編譯 ,編譯過程中可能出現各種問題
在此,將自己編譯好的軟體放在了資源中,可以直接使用:
http://download.csdn.net/detail/shuilan0066/4395617,免去了重複編譯的工作。
資源中同時包含CPPUnitProjectWizard
2 環境配置
cppunit不能直接使用,必須先配置好運行環境。
CPPUNIT環境配置:
比如,CPPUNIT安裝在E盤 E:\cppunit-1.12.1
1) 定義環境變數 CPPUNITDIR
變數值為: E:\cppunit-1.12.1
2) 修改環境變數PATH,在PATH原有路徑的最後,添加 %CPPUNITDIR%/lib, 以便程式載入時能找到 cppunit_dll.dll
3) 在開發環境中,設定好Include/Lib路徑
項目-->屬性-- 》C/C++ 》常規 下 的 附加元件封裝含目錄中添加Include : $(CPPUNITDIR)\Include
項目-->屬性-- 》連接器 》常規 下 的 附加庫目錄中添加lib: $(CPPUNITDIR)\lib
CPPUnitProjectWizard環境配置:
1) 解壓縮後,複製如下兩個檔案到Visual Studio 8安裝目錄下的 VCProjects 檔案夾中
… /VC/vcprojects下
CPPUnitProjectWizard.vsdir -
為嚮導命名
CPPUnitProjectWizard.vsz -
讓VS知道從哪裡找到嚮導
2) 把整個CPPUnitProjectWizard解決方案檔案夾複製到您的Visual Studio 8安裝目錄下的VCWizards檔案夾中,比如,我放在…/VC/VCWizards下
然後用記事本編輯CPPUnitProjectWizard.vsz,定義參數 ABSOLUTE_PATH
Param="ABSOLUTE_PATH = …VC/VCWizards/ CPPUnitProjectWizard"
3 CPPUNIT 宏說明
CPPUNIT 通過以下宏來判斷是否正確
CPPUNIT_ASSERT是一個宏,判斷後面的參數是否正確,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時失敗
4 簡單樣本
1) 開啟vs2005,建立一個MFC 對話方塊項目名為
CppUnit
2) 設定項目屬性
項目-->屬性-- 》C/C++ 》常規 下 的 附加元件封裝含目錄中添加Include : $(CPPUNITDIR)\Include
項目-->屬性-- 》連接器 》常規 下 的 附加庫目錄中添加lib: $(CPPUNITDIR)\lib
3) 開啟CppUnit.cpp檔案,增加包含檔案和聲明使用庫代碼
#include <cppunit/ui/mfc/TestRunner.h>#include <cppunit/extensions/TestFactoryRegistry.h>#pragma comment(lib, "cppunitd.lib")#pragma comment(lib, "testrunnerud.lib")
註:如果為Release版本聲明使用庫檔案為
#pragma comment(lib, "cppunit.lib")#pragma comment(lib, "testrunner.lib")
4)修改InitInstance()函數
注釋掉了對話方塊
增加了CPPUNIT_NS 測試
BOOL CCppUnitApp::InitInstance(){// 如果一個運行在 Windows XP 上的應用程式資訊清單指定要// 使用 ComCtl32.dll 版本 6 或更高版本來啟用可視化方式,//則需要 InitCommonControlsEx()。否則,將無法建立視窗。INITCOMMONCONTROLSEX InitCtrls;InitCtrls.dwSize = sizeof(InitCtrls);// 將它設定為包括所有要在應用程式中使用的// 公用控制項類。InitCtrls.dwICC = ICC_WIN95_CLASSES;InitCommonControlsEx(&InitCtrls);CWinApp::InitInstance();AfxEnableControlContainer();// 標準初始化// 如果未使用這些功能並希望減小// 最終可執行檔的大小,則應移除下列// 不需要的特定初始化常式// 更改用於儲存設定的登錄機碼// TODO: 應適當修改該字串,// 例如修改為公司或組織名SetRegistryKey(_T("應用程式嚮導產生的本地應用程式"));// 注釋掉了這一段// CCppUnitDlg dlg;// m_pMainWnd = &dlg;// INT_PTR nResponse = dlg.DoModal();// if (nResponse == IDOK)// {// // TODO: 在此處放置處理何時用“確定”來關閉// // 對話方塊的代碼// }// else if (nResponse == IDCANCEL)// {// // TODO: 在此放置處理何時用“取消”來關閉// // 對話方塊的代碼// }// 由於對話方塊已關閉,所以將返回 FALSE 以便退出應用程式,// 而不是啟動應用程式的訊息泵。//增加如下代碼CPPUNIT_NS::MfcUi::TestRunner runner;runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );runner.run();return FALSE;}
編譯運行出現如介面,此時未加任何用例的單元測試例子介面就出來了:
5)添加測試代碼
a) 添加一個c++類example,其基類是public
CPPUNIT_NS::TestFixture
b)
修改example.h
#pragma once#include <cppunit/extensions/HelperMacros.h>#include <cppunit/TestFixture.h>class example :public CPPUNIT_NS::TestFixture{CPPUNIT_TEST_SUITE(example);CPPUNIT_TEST_SUITE_END();public:example(void);~example(void);};
c) 修改example.c
#include "StdAfx.h"#include "example.h"CPPUNIT_TEST_SUITE_REGISTRATION(example);example::example(void){}example::~example(void){}
d) 此時編譯,可發現出現了測試包:
e) 添加測試包初始化、結束函數setUp,tearDown(注意大小寫)。以及添加測試案例testcase1,testcase2
example.h
#pragma once#include <cppunit/extensions/HelperMacros.h>#include <cppunit/TestFixture.h>class example :public CPPUNIT_NS::TestFixture{CPPUNIT_TEST_SUITE(example);CPPUNIT_TEST(testcase1);CPPUNIT_TEST(testcase2);CPPUNIT_TEST_SUITE_END();public:example(void);~example(void);public:void setUp();void tearDown();void testcase1();void testcase2();};
example.cpp
#include "StdAfx.h"#include "example.h"CPPUNIT_TEST_SUITE_REGISTRATION(example);example::example(void){}example::~example(void){}void example::testcase1(){AfxMessageBox((LPCTSTR)_T("I am testcase1"),MB_OK,0);}void example::testcase2(){AfxMessageBox((LPCTSTR)_T("I am testcase2"),MB_OK,0);}void example::setUp(){AfxMessageBox((LPCTSTR)_T("I am setup"),MB_OK,0);}void example::tearDown(){AfxMessageBox((LPCTSTR)_T("I am teardown"),MB_OK,0);}
說明,每一個用例,都會調用setUp和tearDown 這是CPPUNIT內部預設的
f) 添加其他用例
#pragma once#include <cppunit/extensions/HelperMacros.h>#include <cppunit/TestFixture.h>class example :public CPPUNIT_NS::TestFixture{CPPUNIT_TEST_SUITE(example);CPPUNIT_TEST(testcase1);CPPUNIT_TEST(testcase2);CPPUNIT_TEST(testcase3);CPPUNIT_TEST(testcase4);CPPUNIT_TEST_SUITE_END();public:example(void);~example(void);public:void setUp();void tearDown();void testcase1();void testcase2();void testcase3();void testcase4();};
#include "StdAfx.h"#include "example.h"CPPUNIT_TEST_SUITE_REGISTRATION(example);example::example(void){}example::~example(void){}void example::testcase1(){AfxMessageBox((LPCTSTR)_T("I am testcase1"),MB_OK,0);}void example::testcase2(){AfxMessageBox((LPCTSTR)_T("I am testcase2"),MB_OK,0);}void example::testcase3(){CPPUNIT_ASSERT(1==1);}void example::testcase4(){CPPUNIT_ASSERT(1==2);}void example::setUp(){AfxMessageBox((LPCTSTR)_T("I am setup"),MB_OK,0);}void example::tearDown(){AfxMessageBox((LPCTSTR)_T("I am teardown"),MB_OK,0);}
g) 單元測試結果
假如測試中所有斷言都通過,則結果為綠色。
假如由一個宣告失敗,則顯示紅色,下面顯示出錯位置,且雙擊可以找到原始碼位置。
5 測試另一個項目中的代碼
建立一對話方塊項目DlgTest,檢測對話方塊項目的函數CDlgTestDlg::Add。
1) 將CPPUNIT 測試專案 和被測試專案 放在同一目錄下, 並在同一個解決方案中開啟
2) 設定測試環境,將被測項目的路徑添加到附加元件封裝含路徑中
比如,被測項目DlgTest的路徑為: E:\TEST\DlgTest\DlgTest
則在CPPUNIT項目屬性--》C/C++-》常規 附加元件封裝含路徑中添加 E:\TEST\DlgTest\DlgTest
因為之前添加了CPPUNIT的Include路徑
所以,添加被測路徑之後的值為: "$(CPPUNITDIR)\Include";E:\TEST\DlgTest\DlgTest
3) 使用 添加--》現有項功能 將被測的DlgTestDlg.h 和DlgTestDlg.cpp添加到CPPUnit中
4) 修改CPPUNIT單元測試項目中的測試單元example
修改example.h
添加被測函數的標頭檔
#include "DlgTestDlg.h"
修改example.cpp
在此,測試對話方塊CDlgTestDlg中的Add函數
void example::testcase1(){//AfxMessageBox((LPCTSTR)_T("I am testcase1"),MB_OK,0);CDlgTestDlg dlg;int i=dlg.Add(3,5); CPPUNIT_ASSERT(i==9);}
5) 編譯運行 查看測試結果
CPPUNIT測試專案模板 儲存在資源
http://download.csdn.net/detail/shuilan0066/4396670 , 可以下載後,將其放置到被測項目的等同目錄下,修改參數,進行測試
參考資料:
http://blog.csdn.net/ainiyidiandian/article/details/5788240
http://morningspace.51.net/resource/cppunit/cppunit_anno.html
http://www.cppblog.com/ace/archive/2006/08/04/10842.html
http://blog.csdn.net/wincol/article/details/5789644
http://blog.csdn.net/alicehyxx/article/details/4483681
http://blog.csdn.net/jinjunmax_78/article/details/2033755