码迷,mamicode.com
首页 > 其他好文 > 详细

如何用googletest写单元测试

时间:2014-05-26 21:07:27      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:des   style   c   class   blog   code   

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

本文包括以下几部分:1、获取并编译googletest(以下简称为gtest);2、如何编写单元测试用例;3、如何执行单元测试。4、google test内部是如何执行我们的单元测试用例的。

1. 获取并编译gtest

gtest试图跨平台,理论上,它就应该提供多个版本的binary包。但事实上,gtest只提供源码和相应平台的编译方式,这是为什么呢?google的解释是,我们在编译出gtest时,有些独特的工程很可能希望在编译时加许多flag,把编译的过程下放给用户,可以让用户更灵活的处理。这个仁者见仁吧,反正也是免费的BSD权限。

 

源码的获取地址:http://code.google.com/p/googletest/downloads/list

 

目前gtest提供的是1.7.0版本,我们看看与以往版本1.6.0的区别:

bubuko.com,布布扣
Changes for 1.7.0:

* New feature: death tests are supported on OpenBSD and in iOS
  simulator now.
* New feature: Google Test now implements a protocol to allow
  a test runner to detect that a test program has exited
  prematurely and report it as a failure (before it would be
  falsely reported as a success if the exit code is 0).
* New feature: Test::RecordProperty() can now be used outside of the
  lifespan of a test method, in which case it will be attributed to
  the current test case or the test program in the XML report.
* New feature (potentially breaking): --gtest_list_tests now prints
  the type parameters and value parameters for each test.
* Improvement: char pointers and char arrays are now escaped properly
  in failure messages.
* Improvement: failure summary in XML reports now includes file and
  line information.
* Improvement: the <testsuites> XML element now has a timestamp attribute.
* Improvement: When --gtest_filter is specified, XML report now doesnt
  contain information about tests that are filtered out.
* Fixed the bug where long --gtest_filter flag values are truncated in
  death tests.
* Potentially breaking change: RUN_ALL_TESTS() is now implemented as a
  function instead of a macro in order to work better with Clang.
* Compatibility fixes with C++ 11 and various platforms.
* Bug/warning fixes.
bubuko.com,布布扣

那么怎么编译呢?

先进入gtest目录(解压gtest.zip包过程就不说了),执行以下两行命令:

  • g++ -I./include -I./ -c ./src/gtest-all.cc  
  • ar -rv libgtest.a gtest-all.o 

之后,生成了libgtest.a,这个就是我们要的东东了。以后写自己的单元测试,就需要libgtest.a和gtest目录下的include目录,所以,这1文件1目录我们需要拷贝到自己的工程中。

 

编译完成后怎么验证是否成功了呢?

  • cd ${GTEST_DIR}/make  
  •   make  
  •   ./sample1_unittest

如果看到下面的输出,就说明成功了。

bubuko.com,布布扣
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 (1 ms total)

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

bubuko.com,布布扣

2、如何编写单元测试用例

测试实例等具体做了再添加。

 

文章参考链接:http://blog.csdn.net/russell_tao/article/details/7333226

 

 

如何用googletest写单元测试,布布扣,bubuko.com

如何用googletest写单元测试

标签:des   style   c   class   blog   code   

原文地址:http://www.cnblogs.com/lhzhou/p/3746005.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!