用googletest寫單元測試

2021-09-26 20:52:13 字數 955 閱讀 4301

reference:

對於稍微複雜些的測試用例,例如需要共享資料:

class chashtabletest : public ::testing::test   

virtual void setup()

// virtual void teardown() {}

chashtable ht;

string key1;

string key2;

};

然後開始寫測試用例,用例裡可以直接使用上面類中的成員:

test_f(chashtabletest, hashfunc)  

test_f(chashtabletest, hashfuncother)

注意,test_f巨集會直接生成乙個類,這個類繼承自上面我們寫的chashtabletest類。

gtest提供assert_和expect_系列的巨集,用於判斷二進位制、字串等物件是否相等、真假等等。這兩種巨集的區別是,assert_失敗了不會往下執行,而expect_會繼續。

3、如何執行單元測試

首先,我們自己要有乙個main函式,函式內容非常簡單:

#include "gtest/gtest.h"  

int main(int argc, char** ar**)

4、google test內部是如何執行我們的單元測試用例的

首先從main函式看起。

我們的main函式執行了run_all_tests巨集,這個巨集幹了些什麼事呢?

#define run_all_tests()\  

(::testing::unittest::getinstance()->run())

} // namespace testing

Google Test單元測試使用

google開源了很多實用的模組,比如google gtest google gmock google glog google gflags google ctemplate google sparsehash protobuf perftools,gtest是c 的測試模組,提供豐富的測試方法 軟...

Qt使用Google Test 單元測試

場景 對qt程式測試 說明 google test環境搭建,看我部落格 google test 的使用,這裡不再重複說明。實現步驟 1 新增qt庫目錄,在qt的安裝目錄,我安裝的是在盤c下 專案屬性 c c 常規 附加包含目錄 新增路徑 c qt include c qt include qt c ...

unittest單元測框架

django預設使用python的標準庫unittest編寫測試用例。學習django單元測試之前,先學習下unittest單元測試框架的基本使用。下面實現乙個簡單的單元測試1.簡單的加法和減法功能實現,module.py 如下 encoding utf 8 class calculator doc...