gtest简单使用

安装gtest & gmock

安装后
/usr/local/lib 目录下会有如下的静态库文件
libgmock.a
libgmock_main.a
libgtest.a
libgtest_main.a
/usr/local/include 目录下会有如下文件夹
gmock/
gtest/

使用例子

  • 被测代码
#include <iostream>

int add(int a, int b)
{
    return a + b;
}

/*int main()
{
    std::cout << add(3, 10);
}*/

  • 测试代码
#include <gtest/gtest.h>

extern int add(int a, int b);

TEST(testCase, testNormal)
{
    EXPECT_EQ(add(2, 3), 5);
}

TEST(testCase, testSmallThan0)
{
    EXPECT_EQ(add(-2, 3), 1);
}

TEST(testCase, testBig)
{
    EXPECT_EQ(add(1000000, 1000000), 2000000);
}

int main(int argc, char **argv)
{
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
  • makefile
gtestDemo.exe:
	g++ -o gtestDemo.exe add.cc testAdd.cc -lgtest -lpthread
clean:
	rm *.exe
  • 编译后的运行结果
[==========] Running 3 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 3 tests from testCase
[ RUN      ] testCase.testNormal
[       OK ] testCase.testNormal (0 ms)
[ RUN      ] testCase.testSmallThan0
[       OK ] testCase.testSmallThan0 (0 ms)
[ RUN      ] testCase.testBig
[       OK ] testCase.testBig (0 ms)
[----------] 3 tests from testCase (3 ms total)

[----------] Global test environment tear-down
[==========] 3 tests from 1 test case ran. (6 ms total)
[  PASSED  ] 3 tests.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值