Skip to content

[Tool] gtest little tutorial

Hu, Hsien-Wen edited this page Dec 26, 2017 · 1 revision

這是一個 gtest 基本到不行的教學

如題,這個wiki 頁面帶給大家一個非常簡易gtest教學,分享 tomyhu1995 所知道的東西

事前準備

  • 你需要安裝gtest在你的電腦: 目前所使用的方法都是將gtest直接安裝在電腦上

程式碼撰寫與編譯需注意的事項

  1. Include files: 記得 `#include<gtest/gtest.h>
  2. Compile: 必須要加上 -lgtest_main', -lgtest, -lpthread`.
  3. Main program: Main program 必須採用C++撰寫,其餘的library可以使用C/C++。

一個簡單的範例

這邊會提供一個簡單的範例讓大家迅速的了解gtest怎麼使用:

#include <gtest/gtest.h>
TEST(multiply, error)//會顯示在gtest的結果上,請使用者輸入正確的測試單元名稱
{
    int a = 5;
    int b = 1;

    ASSERT_EQ(5, a*b);//檢查是否正確
}
int main(int argc, char **argv)
{
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

這個測試程式會run所有的TEST,其中我們所設計的test case是檢查a * b是不是等於5。

更多方便的Functions

在前一部份我們僅僅使用了 ASSERT_EQ進行測試,然而gtest提供了非常多方便的functions,希望有更多能人補齊這份文件。

Clone this wiki locally