How to build samples of Google Test?
In the source of google test, it provides some samples to show how to use google test. In this article, we describe how to build these samples.
1. an example to build a sample
The source of google test provides a sample to build its exmaple. It is in ../make.
This example is to build sample1_unittest, it is in ../samples.
The source code to be tested is in file sample1.cc & sample1.h.
The unit test code is in file sample1_unittest.cc.
# /usr/src/gtest-1.5.0
# cd make
# make
# ./sample1_unittest
The result of build is in ../make, then, we can run sample1_unittest, as follows.
root@yu29:/usr/src/gtest-1.5.0/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 (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.
2. how to build all samples by using cmake?
Cmake generates native build scripts. 先建立mybuild目录
# cd /usr/src/gtest-1.5.0
# mddir mybuild
# cd mybuild
(1) only generate gtest_unittest
# cmake /usr/src/gtest-1.5.0 //generate Makefile
# make //generate executable file gtest_unittest
(2) generate gtest_unittest and all samples
//generate Makefile with Google Test's samples to be compiled
# cmake -Dbuild_gtest_samples=ON /usr/src/gtest-1.5.0
//generate executable files gtest_unittest, sample1_unittest, sample2_unittest, ...
# make
Reference
http://code.google.com/p/googletest
../readme
Appendix. 使用CMake编译gtest_unittest和samples
//生成Makefile文件
root@yu29:/usr/src/gtest-1.5.0/mybuild# cmake -Dbuild_gtest_samples=ON /usr/src/gtest-1.5.0
-- The CXX compiler identification is GNU
-- The C compiler identification is GNU
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Looking for include files CMAKE_HAVE_PTHREAD_H
-- Looking for include files CMAKE_HAVE_PTHREAD_H - found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Found PythonInterp: /usr/bin/python2.6
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/src/gtest-1.5.0/mybuild
root@yu29:/usr/src/gtest-1.5.0/mybuild# ls
CMakeCache.txt CMakeFiles cmake_install.cmake CTestTestfile.cmake Makefile
//编译
root@yu29:/usr/src/gtest-1.5.0/mybuild# make
Scanning dependencies of target gtest
[ 5%] Building CXX object CMakeFiles/gtest.dir/src/gtest-all.cc.o
Linking CXX static library libgtest.a
[ 5%] Built target gtest
Scanning dependencies of target gtest_main
[ 11%] Building CXX object CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
...
//生成了所有samples的可执行程序
root@yu29:/usr/src/gtest-1.5.0/mybuild# ls
CMakeCache.txt libgtest.a sample2_unittest sample7_unittest
CMakeFiles libgtest_main.a sample3_unittest sample8_unittest
cmake_install.cmake Makefile sample4_unittest sample9_unittest
CTestTestfile.cmake sample10_unittest sample5_unittest
gtest_unittest sample1_unittest sample6_unittest
//看看sample2的执行结果
root@yu29:/usr/src/gtest-1.5.0/mybuild# ./sample2_unittest
Running main() from gtest_main.cc
[==========] Running 4 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 4 tests from MyString
[ RUN ] MyString.DefaultConstructor
[ OK ] MyString.DefaultConstructor (0 ms)
[ RUN ] MyString.ConstructorFromCString
[ OK ] MyString.ConstructorFromCString (0 ms)
[ RUN ] MyString.CopyConstructor
[ OK ] MyString.CopyConstructor (0 ms)
[ RUN ] MyString.Set
[ OK ] MyString.Set (0 ms)
[----------] 4 tests from MyString (0 ms total)
[----------] Global test environment tear-down
[==========] 4 tests from 1 test case ran. (1 ms total)
[ PASSED ] 4 tests.
//gtest_unittest的执行结果较长,此处不再显示。