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

  為了測試Math類的測試類:

  /// 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

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

  // … 可以添加更多testcase

  */

  CPPUNIT_TEST_SUITE_END();// TestSuite聲明完成

  public:

  MathTest(){}

  ~MathTest(){}

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

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

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

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

  // …

  */

  };

  測試類MathTest的實(shí)現(xiàn):

  /// MathTest.cpp

  // implement of MathTest.h

  #include "MathTest.h"

  #include "Math.h"

  #include "cppunit/TestAssert.h"

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

  CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MathTest, "alltest" );

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

  void MathTest::testAdd()

  {

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

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

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

  }

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