gtest 編譯入門


運行GoogleTest Sample在VMware/Redhat enterprise V5

 

* make

[root@localhost make]# make
make: Warning: File `Makefile' has modification time 2.3e+07 s in the future
g++ -I../include -g -Wall -Wextra -c ../samples/sample1.cc
g++ -I../include -g -Wall -Wextra -c ../samples/sample1_unittest.cc
g++ -I../include -I.. -g -Wall -Wextra -c /
            ../src/gtest-all.cc
g++ -I../include -I.. -g -Wall -Wextra -c /
            ../src/gtest_main.cc
ar rv gtest_main.a gtest-all.o gtest_main.o
r - gtest-all.o
r - gtest_main.o
g++ -I../include -g -Wall -Wextra -lpthread sample1.o sample1_unittest.o gtest_main.a -o sample1_unittest
make: 警告:檢測到時鍾錯誤。您的創建可能是不完整的。

 

* run test

[root@localhost make]# ./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 (10 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 (8 ms total)

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

3. 寫一個自己的test

 

* 源文件Foo.cpp and FooTest.cpp

[root@localhost frank-study]# cat Foo.cpp
int Foo(int a, int b) {
        if (a==0 || b==0) {
                throw "don't do that";
        }
        int c=a%b;
        if(c==0) {
                return b;
        }
        return Foo(b,c);
}

 

[root@localhost frank-study]# cat FooTest.cpp
#include <gtest/gtest.h>
extern int Foo(int, int);

TEST(FooTest, HandleNoneZeroInput) {
        EXPECT_EQ(2, Foo(4,10));
        //EXPECT_EQ(6, Foo(30,18));
}

TEST(FooTest2, HandleNoneZeroInput) {
        EXPECT_EQ(2, Foo(4,10));
        EXPECT_EQ(6, Foo(30,18));
}

 

* Makefile

 

[root@localhost frank-study]# cat Makefile
# A sample Makefile for building Google Test and using it in user
# tests.  Please tweak it to suit your environment and project.  You
# may want to move it to your project's root directory.
#
# SYNOPSIS:
#
#   make [all]  - makes everything.
#   make TARGET - makes the given target.
#   make clean  - removes all files generated by make.

# Please tweak the following variable definitions as needed by your
# project, except GTEST_HEADERS, which you can use in your own targets
# but shouldn't modify.

# Points to the root of Google Test, relative to where this file is.
# Remember to tweak this if you move this file.
GTEST_DIR = ..

# Where to find user code.
USER_DIR = .

# Flags passed to the preprocessor.
CPPFLAGS += -I$(GTEST_DIR)/include

# Flags passed to the C++ compiler.
CXXFLAGS += -g -Wall -Wextra

# All tests produced by this Makefile.  Remember to add new tests you
# created to the list.
TESTS = FooTest

# All Google Test headers.  Usually you shouldn't change this
# definition.
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h /
                $(GTEST_DIR)/include/gtest/internal/*.h

# House-keeping build targets.

all : $(TESTS)

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

# Builds gtest.a and gtest_main.a.

# Usually you shouldn't tweak such internal variables, indicated by a
# trailing _.
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)

# For simplicity and to avoid depending on Google Test's
# implementation details, the dependencies specified below are
# conservative and not optimized.  This is fine as Google Test
# compiles fast and for ordinary users its source rarely changes.
gtest-all.o : $(GTEST_SRCS_)
        $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c /
            $(GTEST_DIR)/src/gtest-all.cc

gtest_main.o : $(GTEST_SRCS_)
        $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c /
            $(GTEST_DIR)/src/gtest_main.cc

gtest.a : gtest-all.o
        $(AR) $(ARFLAGS) $@ $^

gtest_main.a : gtest-all.o gtest_main.o
        $(AR) $(ARFLAGS) $@ $^

# Builds a sample test.  A test should link with either gtest.a or
# gtest_main.a, depending on whether it defines its own main()
# function.

Foo.o : $(USER_DIR)/Foo.cpp $(GTEST_HEADERS)
        $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/Foo.cpp

FootTest.o : $(USER_DIR)/FooTest.cpp $(GTEST_HEADERS)
        $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/FooTest.cpp

FooTest : Foo.o  FooTest.o gtest_main.a
        $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

 

注:Makefile的預定義變量

 

GNU make 的主要預定義變量:
預定義變量                      含義
$*              不包含擴展名的目標文件名稱。
$+              所有的依賴文件,以空格分開,並以出現的先后為序,可能包含重復的依賴文件。
$<              第一個依賴文件的名稱。
$?              所有的依賴文件,以空格分開,這些依賴文件的修改日期比目標的創建日期晚。
$@              目標的完整名稱。
$^              所有的依賴文件,以空格分開,不包含重復的依賴文件。
$%              如果目標是歸檔成員,則該變量表示目標的歸檔成員名稱。例如,如果目標名稱
                為 mytarget.so(image.o),則 $@ 為 mytarget.so,而 $% 為 image.o。
AR              歸檔維護程序的名稱,默認值為 ar。
ARFLAGS         歸檔維護程序的選項。
AS              匯編程序的名稱,默認值為 as。
ASFLAGS         匯編程序的選項。
CC              C 編譯器的名稱,默認值為 cc。
CCFLAGS         C 編譯器的選項。
CPP             C 預編譯器的名稱,默認值為 $(CC) -E。
CPPFLAGS        C 預編譯的選項。
CXX             C++ 編譯器的名稱,默認值為 g++。
CXXFLAGS        C++ 編譯器的選項。
FC              FORTRAN 編譯器的名稱,默認值為 f77。
FFLAGS          FORTRAN 編譯器的選項。

 

* make及運行

[root@localhost frank-study]# make
make: Warning: File `../include/gtest/gtest-death-test.h' has modification time 2.3e+07 s in the future
g++ -I../include -g -Wall -Wextra -c ./Foo.cpp
g++ -I../include -I.. -g -Wall -Wextra -c /
            ../src/gtest-all.cc
g++ -I../include -I.. -g -Wall -Wextra -c /
            ../src/gtest_main.cc
ar rv gtest_main.a gtest-all.o gtest_main.o
r - gtest-all.o
r - gtest_main.o
g++ -I../include -g -Wall -Wextra -lpthread Foo.o FooTest.o gtest_main.a -o FooTest
make: 警告:檢測到時鍾錯誤。您的創建可能是不完整的。
[root@localhost frank-study]# ./FooTest
Running main() from gtest_main.cc
[==========] Running 2 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 1 test from FooTest
[ RUN      ] FooTest.HandleNoneZeroInput
[       OK ] FooTest.HandleNoneZeroInput (0 ms)
[----------] 1 test from FooTest (3 ms total)

[----------] 1 test from FooTest2
[ RUN      ] FooTest2.HandleNoneZeroInput
[       OK ] FooTest2.HandleNoneZeroInput (0 ms)
[----------] 1 test from FooTest2 (2 ms total)

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

 

Q:如何每次不用再build gtest的源代碼???

 


免責聲明!

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



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