軟體測試——boost單元測試 C++

來源:互聯網
上載者:User

標籤:

分類:

1. 下載安裝Boost
2. 在vs2010 中設定
   工具->選項->vc++目錄
  設定包含檔案目錄:找到解壓的boost檔案夾eg:C:\boost_1_43_0
3. 編寫測試程式
最簡單的,建立一個win32控制台程式,選擇“空項目”;然後添加建立項->代碼->C++檔案(.cpp),然後編寫代碼:
首先,在檔案頭添加以下兩行:
#define BOOST_TEST_MODULE test_xxxx  //定義boost測試模組,宏後面的 test_xxxx不重複就行
#include <boost/test/unit_test.hpp>  //引用boost單元測試標頭檔

然後添加測試案例:
BOOST_AUTO_TEST_CASE( test_someFunction )  //括弧中的可以看做該測試案例的名字
{
}


=================================假設要測試如下代碼

// ! 感應器接收報文
class CLastRecv
{
public:
    CLastRecv(IN const string &strRecv);
    ~CLastRecv();

public:
    bool GetRecvTime(OUT time_t &recvTine);
    bool GetRecvMessage(OUT string &strRecv);

private:
    time_t      m_recvTime;    //接收時間
    string      m_stRecv;      //接收的報文內容
};
================================================




/*! @file
********************************************************************************
<PRE>
模 塊名      : 單元測試
檔案名稱       : Sensor.h
相關檔案     : Sensor.cpp
檔案實現功能 : 測試Sensor 檔案中的所有
作者         : 毛勇
版本         : 1.0
--------------------------------------------------------------------------------
多執行緒安全性 : 是
異常時安全性 : 是
--------------------------------------------------------------------------------
備 注        :
--------------------------------------------------------------------------------
修 改記錄 :
日 期        版本     修改人              修改內容
2010/07/07   1.0.0    毛勇                建立
</PRE>
*******************************************************************************/


#include <iostream>
#include <string.h>
#include <string>

#include "../TransmissionGateway/Sensor.h"

using namespace  std;


#define BOOST_TEST_MODULE sensor
#include <boost/test/included/unit_test.hpp>

//====================================================================
//============================ 單元測試 - Sensor.h ===================
//====================================================================
BOOST_AUTO_TEST_SUITE (sensor) // name of the test suite is sensor

// ! CLastRecv 類測試
BOOST_AUTO_TEST_CASE (clastrecv)
{
    //cout <<"------------------- begin CLastRecv test---------"<< endl;
    string  str("hello");
    CLastRecv  lastRecv(str);
    time_t recvTime;
    string strRecv;

    BOOST_CHECK(lastRecv.GetRecvTime(recvTime) == TRUE);//TRUE

    lastRecv.GetRecvMessage(strRecv);

    BOOST_CHECK(0 == strRecv.compare("hello"));

    BOOST_CHECK(-1 == strRecv.compare("world"));

    //cout << ctime(&recvTime) << endl;

    //cout <<"------------------- end CLastRecv test---------"<< endl;
}

軟體測試——boost單元測試 C++

聯繫我們

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