您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 > cppUnit
如何使用cppunit做模塊測(cè)試
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/4/15 15:39:33 ] 推薦標(biāo)簽:

  為了測(cè)試Math類(lèi)的測(cè)試類(lèi):

  /// MathTest.h

  // A TestFixture subclass.

  #include “cppunit/TestFixture.h”

  #include “cppunit/extensions/HelperMacros.h”

  class MathTest : public CppUnit::TestFixture

  {

  CPPUNIT_TEST_SUITE(MathTest); //聲明一個(gè)TestSuite

  CPPUNIT_TEST(testAdd); //添加TestCase到TestSuite

  /*定義新的測(cè)試用例需要在這兒聲明一下

  // … 可以添加更多testcase

  */

  CPPUNIT_TEST_SUITE_END();// TestSuite聲明完成

  public:

  MathTest(){}

  ~MathTest(){}

  void eard(){};//初始化函數(shù),此例沒(méi)有用到

  void eardown(){};//清理函數(shù),此例沒(méi)有用到

  void testAdd ();// 測(cè)試加法的測(cè)試函數(shù)

  /*可以添加更多測(cè)試函數(shù)

  // …

  */

  };

  測(cè)試類(lèi)MathTest的實(shí)現(xiàn):

  /// MathTest.cpp

  // implement of MathTest.h

  #include "MathTest.h"

  #include "Math.h"

  #include "cppunit/TestAssert.h"

  //把這個(gè)TestSuite注冊(cè)到名字為"alltest"的TestSuite中

  CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MathTest, "alltest" );

  //實(shí)現(xiàn)測(cè)試,測(cè)試中的核心部分

  void MathTest::testAdd()

  {

  Math M;//實(shí)例化

  int ret = M.add(-1,3); //調(diào)用add方法對(duì)其進(jìn)行測(cè)試;

  CPPUNIT_ASSERT(ret==2); //用cppunit提供的方法對(duì)ret與預(yù)期結(jié)果作比較

  }

上一頁(yè)123下一頁(yè)
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd