Google test (Gtest) testing environment under Linux setup steps

Source: Internet
Author: User

1. Download Gtest

Download Link: https://code.google.com/p/googletest/downloads/list

Currently the latest version of Gtest is Gtest-1.7.0.zip, so we will use this version as an example in this article.

2. Understanding folders

After downloading and extracting, you can see the Gooletest (gtest-1.7.0) folder, as shown in the contents:

So many files seem to be troublesome. In fact, Gtest provides support for several different platforms, such as the Msvc folder is used in Microsoft Visual Studio, the Xcode folder is for Mac Xcode,codegrear folder is used for Borland C + + Builder, In the Linux environment, the content we use is the Make folder.

3. Clear files that are not needed

As we have said in the previous step, many files are designed to support different platforms, in order to keep the program concise and avoid confusing the audition, we remove all unnecessary files in this step and keep only what we need.

There are only four folders left, and it looks much better. In fact, open the Make folder, you will find that there is only one makefile file. View the contents of the makefile file and know that this is the first command in the system to compile the Samples folder. But open the sample folder, and see inside a large Tuo source files. In this introductory tutorial, let's not consider the complex examples. So, open the Samples folder and start deleting the file and delete the remaining three files.

4. Overwrite the makefile file

To the Make folder, execute the &&./sample1_unittest command from the command line to see the results of the test execution.

[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.



However, if you open makefile to see, you will find that this makefile only apply to the compilation sample1, if I add a test source file? It's too much trouble to write makefile again. So, in this step, we rewrite the makefile.

As we mentioned in the previous step, there are only 4 folders left (INCLUDE,MAKE,SAMPLES,SRC), since the only file in make has to be rewritten, there is no need to leave this folder. So, the first thing you have to do is to delete the Make folder, along with the makefile file inside ... Then, go to the Samples folder and create a file yourself, named Makefile,

Then, open the makefile file and write to the following content. This new makefile is rewritten by the makefile we have just removed, if you are curious to compare the differences between them, it involves some makefile grammar and functions, if not familiar, you may need to spend a few minutes to check the information to understand them.

Note in the rewritten makefile line 32nd, we compiled a file with the suffix named CPP, and the original example is the end of CC. Therefore, you have to do one more thing, is to change the sample1.cc file name to Sample1.cpp, the sample1_unittest.cc file name changed to Sample1_unittest.cpp, you are done.

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) $ (CPPFLAG S)-i$ (gtest_dir) $ (cxxflags)-C $ (Gtest_dir)/SRC/GTEST-ALL.CCGTEST_MAIN.O: $ (Gtest_srcs_) $ (CXX) $ (cppfla GS)-i$ (gtest_dir) $ (cxxflags)-C $ (gtest_dir)/SRC/GTEST_MAIN.CCGTEST_MAIN.A:GTEST-ALL.O GTEST_MAIN.O $ (A R) $ (arflags) [email protected] $^

%.O:%.cpp
$(CXX)$ (cppflags)$(CXXFLAGS )- C$<-o [email protected]

$ ($ (OBJS) Gtest_main.a
$ ($ (cppflags) $ (CXX-lpthread $^-o [email protected]

Now we have three folders (include,src,samples), our own program is placed in the Samples folder. The name of this folder is also very uncomfortable, you can change it to Mycode or Testcode, and then gtest root folder name gtest-1.7.0 can be changed to mygtest and so on. :

Now, go to the command line to compile the operation: $ make &&./run_test, you can see the results:

6. Add your own test function

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

Google test (Gtest) testing environment under Linux setup steps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.