51Testing软件测试论坛

标题: 获取编译gtest [打印本页]

作者: 测试积点老人    时间: 2018-12-26 16:27
标题: 获取编译gtest
获取并编译gtest

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

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

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

  1. Changes for 1.6.0:
  2. * New feature: ADD_FAILURE_AT() for reporting a test failure at the
  3. given source location -- useful for writing testing utilities.
  4. 。。。 。。。
  5. * Bug fixes and implementation clean-ups.
  6. * Potentially incompatible changes: disables the harmful 'make install'
  7. command in autotools
复制代码
就是最下面一行,make install禁用了,郁闷了吧?UNIX的习惯编译方法:./configure;make;make install失灵了,只能说google比较有种,又开始挑战用户习惯了。

那么怎么编译呢?

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

  1. g++ -I./include -I./ -c ./src/gtest-all.cc
  2. ar -rv libgtest.a gtest-all.o
复制代码
之后,生成了libgtest.a,这个就是我们要的东东了。以后写自己的单元测试,就需要libgtest.a和gtest目录下的include目录,所以,这1文件1目录我们需要拷贝到自己的工程中。

编译完成后怎么验证是否成功了呢?(相当不友好!)

  1. cd ${GTEST_DIR}/make
  2.   make
  3.   ./sample1_unittest
复制代码

如果看到:

  1. Running main() from gtest_main.cc
  2. [==========] Running 6 tests from 2 test cases.
  3. [----------] Global test environment set-up.
  4. [----------] 3 tests from FactorialTest
  5. [ RUN      ] FactorialTest.Negative
  6. [       OK ] FactorialTest.Negative (0 ms)
  7. [ RUN      ] FactorialTest.Zero
  8. [       OK ] FactorialTest.Zero (0 ms)
  9. [ RUN      ] FactorialTest.Positive
  10. [       OK ] FactorialTest.Positive (0 ms)
  11. [----------] 3 tests from FactorialTest (0 ms total)

  12. [----------] 3 tests from IsPrimeTest
  13. [ RUN      ] IsPrimeTest.Negative
  14. [       OK ] IsPrimeTest.Negative (0 ms)
  15. [ RUN      ] IsPrimeTest.Trivial
  16. [       OK ] IsPrimeTest.Trivial (0 ms)
  17. [ RUN      ] IsPrimeTest.Positive
  18. [       OK ] IsPrimeTest.Positive (0 ms)
  19. [----------] 3 tests from IsPrimeTest (0 ms total)

  20. [----------] Global test environment tear-down
  21. [==========] 6 tests from 2 test cases ran. (0 ms total)
  22. [  PASSED  ] 6 tests.
复制代码

那么证明编译成功了。







欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2