Linux 下使用Google Test


最近一直在搭建 CI 框架,必不可少的就是需要有 Unit Test 的工具。

我們的代碼90%都是C和C++,同時需要支持 Red Hat, SUSE, Windows和 Solaris 平台(好在Solaris 客戶少了,對該版本的支持即將結束)。我們最終選定 Google Test 來做 UT。

首選從 github (https://github.com/google/googletest.git)上下載了 gtest 的源碼.

按照網上大多數人的建議,cmake..... 本以為會很順利的。結果卻沒有想象的那么簡單。

查看 make 錯誤輸出,發現提示 nullptr 未定義。OK, 了解了,這是因為 gtest 支持C++11。嗯,改吧...

這里走了一些彎路,之前我們用到的 gcc, git 這些都是官網上拿到最新版代碼,按照提示直接 make, install。。我們想着 gtest 也這么做吧.

Gtest 的官網說了google 自己是沒有用cmake 的,cmake 都是由社區貢獻 。改cmake模板確實很麻煩,研究了一下 gtest 源碼,發現沒有必要這么做。

查看gtest 下 src 目錄,發現有一個 gtest-all.cc文件。文件基本 include 目錄下所有 .cc 的文件。gtest_main.cc 這里有 main 函數。那我們應該只需要編譯這兩個 cc 文件就行。

這是我的步驟:

1.  g++ 編譯 gtest-all.cc 文件,生成 gtest-all.o 文件

2.  g++ 編譯 gtest_main.cc 文件,生成 gtest_main.o 文件

3. 鏈接 gtest-all.o 為共享庫 libgtest.so, 鏈接 gtest_main.o 為靜態庫gtest_main.a

4. libgtest.so 所在目錄加到 LD_LIBRARY_PATH 變量中。

g++ -Wall -c -fPIC -std=c++11 -I./include -I./ -c ./src/gtest-all.cc

g++ -I ../include -g -Wall -Wextra -pthread -c gtest_main.cc -std=c++11

g++ -shared -fPIC -o libgtest.so gtest-all.o

ar rv gtest_main.a gtest_main.o  

接下來就是使用 gtest了。

寫一個簡單函數先測試一下功能吧。這是我的目標函數,返回數的絕對值。

int myAbs(int x)
{
    return x > 0 ? x : -x;
}

測試目標函數,我至少需要三個case,以下是我的Utest 代碼

#include <gtest/gtest.h>
TEST(MyABSTest, PosNumber)
{
    EXPECT_EQ(2, myAbs(2));
    ASSERT_FALSE(myAbs(2) == -2);
}
TEST(MyABSTest, NegNumber)
{
    EXPECT_EQ(2, myAbs(-2));
   ASSERT_NE(
myAbs(-2),-2);
}
TEST(MyABSTest, Zero)
{
EXPECT_EQ(
0, myAbs(0));
}

 

編譯代碼的sample

-L/$(GTEST_LIB)/lib -lgtest -lpthread $(STATIC_LIBPATH)/gtest_main.a

需要需要指定 gtest 的共享庫,以及 gtest_main.a 的靜態庫,不需要手動寫 main 函數。

輸出如下

 

 

 這種純文本輸出肯定不是我想要的,因為我最后需要把輸出結果放到Jenkins上的,所以需要輸出為 xml. 執行參數為 ”--gtest_output=xml”

如果只簡單指定輸出為 xml, 會生成默認輸出結果 test_detail.xml。也可以使用 "test_detail.xml:./testresult.xml" 在當前路徑下生產 testresult.xml 的輸出文件。

<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="3" failures="0" disabled="0" errors="0" time="0.001" timestamp="2020-03-28T12:03:49" name="AllTests">
  <testsuite name="MyABSTest" tests="3" failures="0" disabled="0" errors="0" time="0" timestamp="2020-03-28T12:03:49">
    <testcase name="PosNumber" status="run" result="completed" time="0" timestamp="2020-03-28T12:03:49" classname="MyABSTest" />
    <testcase name="NegNumber" status="run" result="completed" time="0" timestamp="2020-03-28T12:03:49" classname="MyABSTest" />
    <testcase name="Zero" status="run" result="completed" time="0" timestamp="2020-03-28T12:03:49" classname="MyABSTest" />
  </testsuite>
</testsuites>

這樣三個case 的 xml 格式輸出正常了。

實際中,我需要為case添加一些描述信息,這樣可以讓后續維護的人了解每一個case大致都做些什么。

這時我需要修改我的測試代碼,為其添加一些屬性,代碼如下

#include <gtest/gtest.h>
#include "MyABS.h"

TEST(MyABSTest, PosNumber)
{
        this->RecordProperty("Description", "Pos number n return n");
        EXPECT_EQ(2, myAbs(2));
        ASSERT_FALSE(myAbs(2) == -2);
}
TEST(MyABSTest, NegNumber)
{
        this->RecordProperty("Description", "Neg number n return -n");
        EXPECT_EQ(2, myAbs(-2));
        ASSERT_NE(myAbs(-2),-2);
}
TEST(MyABSTest, Zero)
{
        this->RecordProperty("Description", "Zero return Zero");
        EXPECT_EQ(0, myAbs(0));
}

輸出xml 結果如下

<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="3" failures="0" disabled="0" errors="0" time="0" timestamp="2020-03-28T12:12:29" name="AllTests">
  <testsuite name="MyABSTest" tests="3" failures="0" disabled="0" errors="0" time="0" timestamp="2020-03-28T12:12:29">
    <testcase name="PosNumber" status="run" result="completed" time="0" timestamp="2020-03-28T12:12:29" classname="MyABSTest">
        <properties>
                <property name="Description" value="Pos number n return n"/>
        </properties>
    </testcase>
    <testcase name="NegNumber" status="run" result="completed" time="0" timestamp="2020-03-28T12:12:29" classname="MyABSTest">
        <properties>
                <property name="Description" value="Neg number n return -n"/>
        </properties>
    </testcase>
    <testcase name="Zero" status="run" result="completed" time="0" timestamp="2020-03-28T12:12:29" classname="MyABSTest">
        <properties>
                <property name="Description" value="Zero return Zero"/>
        </properties>
    </testcase>
  </testsuite>
</testsuites>

好了, gtest 簡單用法先介紹到這里了。會用 gtest 后,我們還需要學會 gmock。我會在下一篇文章中介紹 gmock 的使用。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM