anbian

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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的区别:

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 doesn't
  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.

那么怎么编译呢?

先进入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

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

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.

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

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

 

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

 

 

posted on 2014-05-22 16:51  anbian  阅读(440)  评论(0编辑  收藏  举报