Gtest 单元测试初试

发表于:2014-3-18 11:58

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:彼得    来源:51Testing软件测试网采编

  下载,安装,省略,直接上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.
21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号