在Ubuntu12.04上使用gtest
来源:互联网 发布:绿色建筑设计评价软件 编辑:程序博客网 时间:2024/05/16 10:51
Google test是Google开发的编写C/C++代码单元测试的框架。
在本文中描述了如何在Ubuntu上使用gtest.
首先需要下载gtest的安装包:
sudo apt-get install libgtest-dev
在安装完成之后,此包里面只包含一些源代码,库文件需要自己进行编译。
sudo apt-get install cmake # install cmakecd /usr/src/gtestsudo cmake CMakeLists.txtsudo make # copy or symlink libgtest.a and libgtest_main.a to your /usr/lib foldersudo cp *.a /usr/lib
假如我们要测试一个函数如下:
// whattotest.cpp#include <math.h> double squareRoot(const double a) { double b = sqrt(a); if(b != b) { // nan check return -1.0; }else{ return sqrt(a); }}
然后我们编写此函数的单元测试文件:
// tests.cpp#include "whattotest.cpp"#include <gtest/gtest.h> TEST(SquareRootTest, PositiveNos) { ASSERT_EQ(6, squareRoot(36.0)); ASSERT_EQ(18.0, squareRoot(324.0)); ASSERT_EQ(25.4, squareRoot(645.16)); ASSERT_EQ(0, squareRoot(0.0));} TEST(SquareRootTest, NegativeNos) { ASSERT_EQ(-1.0, squareRoot(-15.0)); ASSERT_EQ(-1.0, squareRoot(-0.2));} int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS();}
然后使用g++进行编译:
g++ -o tests test.cpp -lgtest -lpthread
运行./tests可以得到如下结果:
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from SquareRootTest
[ RUN ] SquareRootTest.PositiveNos
[ OK ] SquareRootTest.PositiveNos (0 ms)
[ RUN ] SquareRootTest.NegativeNos
[ OK ] SquareRootTest.NegativeNos (0 ms)
[----------] 2 tests from SquareRootTest (0 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (0 ms total)
[ PASSED ] 2 tests.
0 0
- 在Ubuntu12.04上使用gtest
- xcode上使用gtest
- [转]在Ubuntu12.04上使用hostapd和dhcp3
- tftp 在ubuntu12.04, busyBox上的配置和使用
- 在win10上使用VMware workstation安装ubuntu12.04
- 在linux上搭建gtest
- 在linux上搭建gtest
- 在vs中使用gtest
- 在NDK中使用gtest
- 在Ubuntu12.04上安装mysql
- 在ubuntu12.04上安装Adobe Air
- 如何在Ubuntu12.04上安装CUDA
- 在Ubuntu12.04上安装中文输入法
- 在Ubuntu12.04LTS上安装RubyGem
- 在ubuntu12.04上编译 android源代码
- ecos在Ubuntu12.04上的安装
- 在ubuntu12.04上安装部署foreman
- 在Ubuntu12.04上部署BigBlueButton
- git reset 总结
- 职业化
- sql 语句拼装类-自动智能拼装生成常用sql语句
- English—Spoken English
- android中常用的弹出提示框
- 在Ubuntu12.04上使用gtest
- C++编程学习50个经典网站 强力推荐
- async 和 await 被我这个新手第一次使用的笔记
- Visual AssistX 10.6.1837安装文件和破解补丁打包下载
- 移动短信箱二次开发,以及进行Base64转码的问题
- 两个大数组foreach,找出相同的key数量,所用的时间对比
- HDU 3787 A+B
- find命令用法
- http://571522149.duapp.com