Linux下Google Test (GTest)測試環境搭建步驟

來源:互聯網
上載者:User

標籤:lan   第一個   user   htm   blog   入門教程   解壓   cas   list   

1.下載GTEST

下載連結為:https://code.google.com/p/googletest/downloads/list

目前GTEST的最新版本為gtest-1.7.0.zip,因此我們在本文中將以這個版本作為例子講解。

2.認識檔案夾

下載並解壓之後,就可以看到gooletest(gtest-1.7.0)檔案夾了,裡面的內容如所示:

這麼多檔案看起來很麻煩。其實,GTEST提供了對於多個不同平台的支援,例如msvc檔案夾是用在微軟Visual Studio中,xcode檔案夾是用於Mac Xcode,codegrear檔案夾是用於Borland C++ Builder,在Linux環境中,我們用的內容就是make檔案夾了。

3.清除不需要的檔案

前一步中我們已經講到,很多檔案是為了支援不同平台的,為了保持程式的簡潔,避免混淆試聽,我們在這一步把所有不需要的檔案全部刪除,只保留我們需要的。

只剩下了四個檔案夾,看上去好多了。其實開啟make檔案夾,你會發現裡面只有一個Makefile檔案。查看Makefile檔案內容,得知這是系統給出的編譯samples檔案夾中的第一個sample的命令。但是開啟sample檔案夾,又看到裡面一大坨源檔案。在本入門教程中,我們先不考慮那些複雜的例子。因此,開啟samples檔案夾,開始刪檔案,刪到只剩下的三個檔案為止。

4.改寫Makefile檔案

到make檔案夾下,通過命令列執行 $ make && ./sample1_unittest 命令,可以看到測試的執行結果。

[email protected]5437:~/gtest/googletest/make$ make && ./sample1_unittest

g++ -isystem ../include -g -Wall -Wextra -pthread -c ../samples/sample1.cc
g++ -isystem ../include -g -Wall -Wextra -pthread -c ../samples/sample1_unittest.cc
g++ -isystem ../include -I.. -g -Wall -Wextra -pthread -c \
            ../src/gtest-all.cc
g++ -isystem ../include -I.. -g -Wall -Wextra -pthread -c \
            ../src/gtest_main.cc
ar rv gtest_main.a gtest-all.o gtest_main.o
ar: creating gtest_main.a
a - gtest-all.o
a - gtest_main.o
g++ -isystem ../include -g -Wall -Wextra -pthread -lpthread sample1.o sample1_unittest.o gtest_main.a -o sample1_unittest
Running main() from gtest_main.cc
[==========] Running 6 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from FactorialTest
[ RUN      ] FactorialTest.Negative
[       OK ] FactorialTest.Negative (0 ms)
[ RUN      ] FactorialTest.Zero
[       OK ] FactorialTest.Zero (0 ms)
[ RUN      ] FactorialTest.Positive
[       OK ] FactorialTest.Positive (0 ms)
[----------] 3 tests from FactorialTest (0 ms total)

[----------] 3 tests from IsPrimeTest
[ RUN      ] IsPrimeTest.Negative
[       OK ] IsPrimeTest.Negative (0 ms)
[ RUN      ] IsPrimeTest.Trivial
[       OK ] IsPrimeTest.Trivial (0 ms)
[ RUN      ] IsPrimeTest.Positive
[       OK ] IsPrimeTest.Positive (0 ms)
[----------] 3 tests from IsPrimeTest (0 ms total)

[----------] Global test environment tear-down
[==========] 6 tests from 2 test cases ran. (0 ms total)
[  PASSED  ] 6 tests.



不過如果開啟Makefile查看一下,就會發現這個makefile只適用於編譯sample1,如果我再增加一個被測的源檔案呢?又要重新寫makefile,太麻煩了。於是,在這一步,我們改寫一下Makefile。

 上一步我們講到,現在只剩下4個檔案夾(include,make,samples,src),既然make裡面的唯一一個檔案也要被改寫,那也沒必要留這個檔案夾了。於是,你要做的第一件事情就是,把make檔案夾,連同裡面的Makefile檔案全部刪除……然後,進入samples檔案夾,自己建立一個檔案,名為Makefile,

然後,開啟Makefile檔案,寫入以下內容。這個新的Makefile是由剛才被我們刪除的Makefile改寫而來的,如果你好奇的話可以比較一下它們之間的差別,裡面涉及到一些makefile的文法和函數,如果不熟的話,你可能需要花費幾分鐘查一下資料去瞭解他們。

注意中改寫的Makefile第32行,我們編譯的是尾碼名為cpp的檔案,而原來給的例子卻以cc結尾。因此,你還要做一件事情,就是把sample1.cc的檔案名稱改為sample1.cpp,把sample1_unittest.cc的檔案名稱改為sample1_unittest.cpp,就大功告成了。

GTEST_DIR = ..USER_DIR = .CPPFLAGS += -isystem $(GTEST_DIR)/includeCXXFLAGS += -g -Wall -Wextra -pthreadTESTS = run_testGTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h                 $(GTEST_DIR)/include/gtest/internal/*.hFILES =$(foreach d,$(USER_DIR),$(wildcard $(d)/*.cpp))
OBJS =$(patsubst %.cpp,%.o,$(FILES))


all : $(TESTS)clean : rm -f $(TESTS) gtest_main.a *.o

.PHONY :clean
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)gtest-all.o : $(GTEST_SRCS_) $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c $(GTEST_DIR)/src/gtest-all.ccgtest_main.o : $(GTEST_SRCS_) $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c $(GTEST_DIR)/src/gtest_main.ccgtest_main.a : gtest-all.o gtest_main.o $(AR) $(ARFLAGS) [email protected] $^

%.o :%.cpp
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o [email protected]

$(TESTS) : $(OBJS) gtest_main.a
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o [email protected]

現在我們的檔案夾有三個(include,src,samples),我們自己被測的程式放在samples檔案夾中。這個檔案夾的名字看著也比較不爽,你可以把它改為mycode或者testcode,然後GTEST根目錄的檔案夾名稱gtest-1.7.0也可以改為mygtest之類,。:

 

現在,進入命令列進行編譯執行操作: $ make && ./run_test,就可以看到結果了,:

6.添加自己的測試函數

參考http://www.linuxidc.com/Linux/2015-05/116894.htm

 

Linux下Google Test (GTest)測試環境搭建步驟

聯繫我們

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