gtest 单元测试初试

下载,安装,省略,直接上demo源码
===========linux运行gtest=================
----------------code.cpp-------------------
#include "code.h"
int f( int a ){    return a+1;}
int b( int b ){    return b+2;}
----------------code_test.cpp-------------------
#include "code.h"
#include <gtest/gtest.h>
TEST( TEST_A,NAME ){//TEST_A 是测试用例名称, NAME 是测试名称
    EXPECT_EQ( 2,f( 1 ) );//这个测试执行函数f,参数为1,查看返回值是不是2
    EXPECT_EQ( 3,f( 2 ) );
}
TEST( TEST_B,NAME ){
    EXPECT_EQ( 4,b( 2 ) );
}
----------------main_test.cpp-------------------
#include <gtest/gtest.h>
#include <iostream>
int main(int argc, char **argv) {
        testing::InitGoogleTest(&argc, argv);
        return RUN_ALL_TESTS();
 }
----------------运行--------------------------
gcc $(gtest-config --ldflags --libs) -o main_test code.cpp code_test.cpp main_test.cpp -I /home/sosotest/include -lgtest -L /home/sosotest/lib
----------------运行--------------------------
./main_test 
[==========] Running 4 tests from 4 test cases.
[----------] Global test environment set-up.
[----------] 1 test from TEST_A
[ RUN      ] TEST_A.NAME
[       OK ] TEST_A.NAME (0 ms)
[----------] 1 test from TEST_A (0 ms total)
[----------] 1 test from TEST_B
[ RUN      ] TEST_B.NAME
[       OK ] TEST_B.NAME (0 ms)
[----------] 1 test from TEST_B (0 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 2 test cases ran. (0 ms total)
[  PASSED  ] 2 tests.
===========windows运行gtest=================STEP1: 下载cygwin安装,默认在c:\cygwin
STEP2: 下载gtest1.6,解压并拷贝到cygwin的目录下,即c:\cygwin的某个子目录下
STEP3: hack src/gtest.cc:808行的gettimeofday(window没有这个函数,参考http://blog.chinaunix.net/uid-140978-id-3029195.html)
 替换 :gettimeofday(&now, NULL);
 替换成:
  FILETIME ft;
  long long *time64 = (long long *) &ft;
  GetSystemTimeAsFileTime (&ft);
  *time64 -= 116444736000000000LL;
  *time64 /= 10;
  now.tv_sec = *time64 / 1000000;
  now.tv_usec = *time64 % 1000000;
STEP4: 进入make子目录,去掉Makefile中的最后一行的 -lpthread(window无pthread库)
STEP5: 执行make,生成并运行sample1_unittest.exe,截图略


转载于:https://my.oschina.net/sanpeterguo/blog/207285

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值