如何编译google test的例子?

原创 2011年01月16日 00:13:00
本博客http://blog.csdn.net/livelylittlefish 贴出作者(三二一@小鱼)相关研究、学习内容所做的笔记,欢迎广大朋友指正!

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_unittestsamples

 

//生成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的执行结果较长,此处不再显示。





版权声明:本文为博主原创文章,未经博主允许不得转载。

Linux下Google Test测试环境安装和使用,实战总结

google test(libgtes)是由谷歌开发的一款基于xunit框架的跨平台单元测试框架,C#上的NUnit,Java的JUnit写单元测试非常容易,也不乏可视化工具与IDE集成插件,深受喜欢...

gtest 编译、安装和使用

本例以 gtest-1.7.0 为例进行讲解。 一、要求: 1. 安装 cmake 二、编译 1. 下载 gtest 源码包,并解压,如:/home/hdc/gtest-1.7.0; ...

Googletest测试框架搭建方法

转载前注明出处 欢迎转载分享 网上有很多类似的步骤,我简要说明下我自己在Linux-centos6.8下的安装步骤: 1.下载包 首先去google官方的github下载1.6.0版本的zip包,...

尝试使用google test

google test 是一个开源代码测试框架,主要用于单元测试。代码可以从 https://github.com/google/googletest 获得。 下面以一个简单的例子来说明如何使用goo...

如何用googletest写单元测试

googletest是一个用来写C++单元测试的框架,它是跨平台的,可应用在windows、linux、Mac等OS平台上。下面,我来说明如何使用最新的1.6版本gtest写自己的单元测试。 本文包...

Google Test(GTest)使用方法和源码解析——概况

本文将以GTest的1.7版本为参照,讲解GTest测试框架的简单使用和特性。

googletest 学习笔记

前言googletest 是由 Google 开发的开源 C++ 单元测试框架,在很多开源项目中(如 chromium)都有使用。这篇博客记录在了自己在学习 googletest 的文档《Primer...

Google Test(GTest)使用方法和源码解析——结果统计机制分析

本文分析了GTest框架中结果统计机制的实现

Google Test(GTest)使用方法和源码解析——死亡测试技术分析和应用

本文介绍了GTest的死亡测试技术的实现。

google test 入门(一)

翻译自 https://code.google.com/p/googletest/wiki/V1_6_Primer 1.
  • cdhql
  • cdhql
  • 2014年07月09日 15:30
  • 1699
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:如何编译google test的例子?
举报原因:
原因补充:

(最多只允许输入30个字)