試用CppUnit–一個簡單的例子

來源:互聯網
上載者:User

用CppUnit其它與用Junit差不多,基本原理是一致的,不過我以為,與Junit相比,配置它稍微麻煩了一點.
(下一篇文章,將介紹cppunit在vc6.0及vc.net下的配置)

我以一個簡單的Money類為例

//name:money.h

#ifndef MONEY_H
#define MONEY_H

#include <string>

class Money
{
public:
  Money( double amount)
    : m_amount( amount )
  {
  }

  double getAmount() const
  {
    return m_amount;
  }

private:
  double m_amount;
};

#endif

我們現在需要用Cppunit來測試它的這兩個函數-----建構函式和getAmount()函數.

第一步:當然是配置cppunit環境了,這裡就不多說了(下面講的環境是在vc下的)
第二步:利用嚮導建立一個console application,名字為MoneyApp,編輯項目內的MoneyApp.cpp如下:
// MoneyApp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

int main(int argc, char* argv[])
{
  // Get the top level suite from the registry

  CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();

  // Adds the test to the list of test to run
  CppUnit::TextUi::TestRunner runner;
  runner.addTest( suite );

  // Change the default outputter to a compiler error format outputter
  runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
                                                       std::cerr ) );
  // Run the tests.
  bool wasSucessful = runner.run();

  // Return error code 1 if the one of test failed.

  return wasSucessful ? 0 : 1;
}

第三步:現在才到了我們為Money類寫測試的時候了,我們首先建立一個標頭檔MoneyTest.h,其內容如下:
#ifndef MONEYTEST_H
#define MONEYTEST_H

#include <cppunit/extensions/HelperMacros.h>

class MoneyTest : public CppUnit::TestFixture
{
  //其實就是在這裡面加入要測試的項目
  CPPUNIT_TEST_SUITE( MoneyTest );
  CPPUNIT_TEST( testGetAmount );
  CPPUNIT_TEST_SUITE_END();

public:
  void setUp();
  void tearDown();

  void testConstructor();
  void testGetAmount();
};

#endif  // MONEYTEST_H

然後,我們再建立一個cpp檔案MoneyTest.cpp,(對了,真正的測試正是在這裡面),其內容如下:
#include "stdafx.h"
#include "MoneyTest.h"
#include "Money.h"

//這句很重要,應與前面的"CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest()"對照起來看
// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( MoneyTest );

void
MoneyTest::setUp()
{
 
}

void
MoneyTest::tearDown()
{
}

void
MoneyTest::testConstructor()
{

}

void
MoneyTest::testGetAmount()
{
 const double longNumber = 12345678.90123;
 Money money ( longNumber);

 CPPUNIT_ASSERT_EQUAL( longNumber,money.getAmount());
}
第四步:好了,可以按ctrl+F5跑一下了,運行結果是這樣:

.

OK (1)
Press any key to continue

如果想看一看宣告失敗是什麼效果的話,可以這樣:
把CPPUNIT_ASSERT_EQUAL( longNumber,money.getAmount());
改為CPPUNIT_ASSERT_EQUAL( 100,money.getAmount());
則運行結果就是這樣.
.F

D:/ZHANGYD/MoneyApp/MoneyTest.cpp(38):Assertion
Test name: MoneyTest::testGetAmount
equality assertion failed
- Expected: 100
- Actual  : 1.23457e+007

Failures !!!
Run: 1   Failure total: 1   Failures: 1   Errors: 0
Press any key to continue

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.