: Http://code.google.com/p/googletest/downloads/list
The latest version is 1.6.
Document: http://code.google.com/p/googletest/w/list
Download and decompress the package. The process is omitted.
(1) Compile gtest:
Read the README file first, and you will basically know how to use it. Common three steps in linux (in fact, there are only two steps ):
Step 1: #./congure
Step 2: # make
If there is no problem, it will be OK.
Step 3: # make install
"'Make install' is dangerous and not supported. Instead, see README for how to integrate Google Test into your build system ."
That is to say, in gtest of version 1.6, make install is not applicable (according to the online message: previous versions, including version 1.5, can use make install without verification ). So this step is not required.
Why? Please see Official documentation: http://code.google.com/p/googletest/wiki/V1_6_FAQ#Why_is_it_not_recommended_to_install_a_pre-compiled_copy_of_Goog
In the early days, we said that you cocould install compiled Google Test libraries on * nix systems using make install. Then every user of your machine can write tests without recompiling Google Test.
This seemed like a good idea, but it has a got-cha: Every user needs to compile his tests using the same compiler flags used to compile the installed Google test libraries; otherwise he may run into undefined behaviors (I. e. the tests can behave strangely and may even crash for no obvious reasons ).
Why? Because c ++ has this thing called the one-definition rule: if two C ++ source files contain different definitions of the same class/function/variable, and you link them together, you violate the rule. the linker may or may not catch the error (in our cases it's not required by the C ++ standard to catch the violation ). if it doesn' t, you get strange run-time behaviors that are unexpected and hard to debug.
If you compile Google test and your test code using different compiler flags, they may see different definitions of the same class/function/variable (e.g. due to the use of # If in Google test ). therefore, for your sanity, we recommend to Avoid installing pre-compiled Google test libraries. instead, each project shoshould compile Google test itself such that it can be sure that the same flags are used for both Google test and the tests.
The previous make install will install a compiled gtest library on the machine, you only need to link the library to test later (most libraries in linux use this method ). This is convenient, but it may bring about a problem: when everyone uses gtest, different compilation options may be required. If you use a pre-compiled gtest library, it may cause confusion. Therefore, when compiling your own test project, you need to compile your own gtest library. Therefore, make install.
So how should we use gtest?
(2) Compile sample:
Under the gtest root directory, there is a make directory:
1 rao@server233:~/work/gtest-1.6.0/make$ make clean 2 rm -f sample1_unittest gtest.a gtest_main.a *.o 3 raozhengfeng@server233:~/work/gtest-1.6.0/make$ make 4 g++ -I../include -g -Wall -Wextra -c ../samples/sample1.cc 5 g++ -I../include -g -Wall -Wextra -c ../samples/sample1_unittest.cc 6 g++ -I../include -I.. -g -Wall -Wextra -c \ 7 ../src/gtest-all.cc 8 g++ -I../include -I.. -g -Wall -Wextra -c \ 9 ../src/gtest_main.cc10 ar rv gtest_main.a gtest-all.o gtest_main.o11 ar: creating gtest_main.a12 a - gtest-all.o13 a - gtest_main.o14 g++ -I../include -g -Wall -Wextra -lpthread sample1.o sample1_unittest.o gtest_main.a -o sample1_unittest
If there is no problem, it will compile gtest to provide the test example: samples/samplew.unittest.cc, and generate the test program: samplew.unittest
From the compilation output: samplew.unittest does not use the static library method for connection, but directly compiled the source file in the gtest framework: src/gtest_main.cc, src/gtest-all.cc.
In this case, we do not need to pre-compile the gtest framework. We only need to reference the gtest file in the test code and modify the Makefile (this is actually a source code reference ).
Run the test program:
1 rao@server233:~/work/gtest-1.6.0/make$ ./sample1_unittest 2 Running main() from gtest_main.cc 3 [==========] Running 6 tests from 2 test cases. 4 [----------] Global test environment set-up. 5 [----------] 3 tests from FactorialTest 6 [ RUN ] FactorialTest.Negative 7 [ OK ] FactorialTest.Negative (0 ms) 8 [ RUN ] FactorialTest.Zero 9 [ OK ] FactorialTest.Zero (0 ms)10 [ RUN ] FactorialTest.Positive11 [ OK ] FactorialTest.Positive (0 ms)12 [----------] 3 tests from FactorialTest (0 ms total)13 14 [----------] 3 tests from IsPrimeTest15 [ RUN ] IsPrimeTest.Negative16 [ OK ] IsPrimeTest.Negative (0 ms)17 [ RUN ] IsPrimeTest.Trivial18 [ OK ] IsPrimeTest.Trivial (0 ms)19 [ RUN ] IsPrimeTest.Positive20 [ OK ] IsPrimeTest.Positive (0 ms)21 [----------] 3 tests from IsPrimeTest (0 ms total)22 23 [----------] Global test environment tear-down24 [==========] 6 tests from 2 test cases ran. (1 ms total)25 [ PASSED ] 6 tests.
The test sample runs successfully. Next we will study how to use gtest for unit testing.