1. 程式人生 > >【轉】gtest使用簡介

【轉】gtest使用簡介

最近測試過程中使用gtest,簡單易用,是一個非常不錯的單元測試框架,簡單介紹下使用方法,歡迎大家拍磚

一、安裝

1、下載最新版本:gtest-1.4.0.tar.gz

2、解壓後進入gtest-1.4.0

3、執行./configure  make  make install(需要sudo許可權,也可以自己指定安裝的目錄)

4、進入根目錄,編輯.bashrc,新增庫檔案路徑export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

(編譯測試程式時可以找到生成的gtest庫檔案)

二、呼叫

1、編寫測試程式時新增#include <gtest/gtest.h>

2、編譯測試程式時新增以下內容-ldl -lgtest -I/usr/local/include(載入gtest標頭檔案)

三、簡單示例

TEST(TEST_stemmer1,test_pencils)

{

        CStemmer *tt = new CStemmer();  //呼叫stemmer介面函式

        bool initflag = false;

        initflag = tt->initialize_stemmer();  //初始化函式

        char *word;

        word = new char[MAXLEN+1];

        strcpy(word, "pencils");         //

傳入測試資料

        word = tt->stemming(word);     //函式對測試資料進行處理

        EXPECT_STREQ("pencil",word);   //使用斷言,預期值與實際返回結果進行比較

}

四、stemmer程式部分測試結果

Gtest對每個失敗的case給出預期值與實際值的差別以及失敗case的名稱

五、gtest斷言

布林值檢查

Fatal assertion

Nonfatal assertion

Verifies

ASSERT_TRUE(condition);

EXPECT_TRUE(condition);

condition

is true

ASSERT_FALSE(condition);

EXPECT_FALSE(condition);

conditionis false

數值型資料檢查

Fatal assertion

Nonfatal assertion

Verifies

ASSERT_EQ(expected, actual);

EXPECT_EQ(expected, actual);

expected == actual

ASSERT_NE(val1, val2);

EXPECT_NE(val1, val2);

val1 != val2

ASSERT_LT(val1, val2);

EXPECT_LT(val1, val2);

val1 < val2

ASSERT_LE(val1, val2);

EXPECT_LE(val1, val2);

val1 <= val2

ASSERT_GT(val1, val2);

EXPECT_GT(val1, val2);

val1 > val2

ASSERT_GE(val1, val2);

EXPECT_GE(val1, val2);

val1 >= val2

字串檢查

Fatal assertion

Nonfatal assertion

Verifies

ASSERT_STREQ(expected_str, actual_str);

EXPECT_STREQ(expected_str, actual_str);

the two C strings have the same content

ASSERT_STRNE(str1, str2);

EXPECT_STRNE(str1, str2);

the two C strings have different content

ASSERT_STRCASEEQ(expected_str, actual_str);

EXPECT_STRCASEEQ(expected_str, actual_str);

the two C strings have the same content, ignoring case

ASSERT_STRCASENE(str1, str2);

EXPECT_STRCASENE(str1, str2);

the two C strings have different content, ignoring case

更多詳細資料請參看