CppUnit基礎使用

來源:互聯網
上載者:User

TestCase
,單個測試案例,自訂測試類別的父類;

TestSuit
,一組測試案例,由
TestCase
組成;

 

編寫的基本步驟:

1
,建立工程,設定
RTTI
及多線程
lib
,添加需要的
lib
,如cppunitd.lib TestRunnerd.lib

 

2
,從CppUnit::TestCase派生測試類別CPlusTestCase;<cppunit/TestCase.h>

<CppUnit/extensions/HelperMacros.h>

<cppunit/extensions/TestFactoryRegistry.h>

 

3
,在CPlusTestCase聲明中註冊
TestSuit
,表示將函數testAdd加到CPlusTestCase這個
TestSuit
中:

class
CPlusTestCase : public CppUnit::TestCase

{

CPPUNIT_TEST_SUITE(CPlusTestCase);

CPPUNIT_TEST(testAdd);

//
testAdd是一個自訂的測試函數

CPPUNIT_TEST_SUITE_END();

public:

CPlusTestCase(void);

//
構造在runner.addTest中被調用,而非run
()
或執行測試時

~CPlusTestCase(void);

void testAdd(void);

};

 

4
,在CPlusTestCase實現檔案中註冊
TestSuit
,表示將
TestSuit
CPlusTestCase註冊到
abc
這個組中;

CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CPlusTestCase,
"
abc
");

 

5
,在程式啟動時使用TestRunner添加測試及執行,<cppunit/ui/mfc/TestRunner.h>,如
(
需要<cppunit/extensions/TestFactoryRegistry.h>
)

CppUnit::MfcUi::TestRunner
runner;

runner.addTest(CppUnit::TestFactoryRegistry::getRegistry("
abc
").makeTest());

//添加測試

runner.run();//show
UI

 

注意:

1

cppunit
相應標頭檔的
#include
需放到

#define new DEBUG_NEW的前面;

2
,使用相應
lib

debug

release
版本,碰到過因版本不同而程式執行報錯的問題;

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.