前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一个google Test文件案例

一个google Test文件案例

作者头像
顾翔
发布2024-09-10 15:19:50
1040
发布2024-09-10 15:19:50
举报
文章被收录于专栏:啄木鸟软件测试

1, 建立头文件calculator.h

代码语言:javascript
复制
//functions.h
#ifndef _FUNCTIONS_H
#define _FUNCTIONS_H


int add(int a,int b);
int myMinus(int a,int b);
int multiply(int a,int b);
int divide(int a,int b);
#endif

2 建立被测文件calculator.cpp

代码语言:javascript
复制
//calculator.cpp
#include "calculator.h"
int add(int a,int b){
        return a+b;
}
int myMinus(int a,int b){
        return a-b;
}
int multiply(int a,int b){
        return a*b;
}
int divide(int a,int b){
        return a/b;
}

3 建立测试文件calculatorTest.cpp

代码语言:javascript
复制
//calculatorTest.cpp
#include "gtest/gtest.h"
#include "calculator.h"


TEST(AddTest,AddTestCase){
        ASSERT_EQ(2,add(1,1));
}
TEST(MinusTest,MinusTestCase){
        ASSERT_EQ(10,myMinus(25,15));
}
TEST(MultiplyTest,MutilplyTestCase){
        ASSERT_EQ(12,multiply(3,4));
}
TEST(DivideTest,DivideTestCase){
        ASSERT_EQ(2,divide(7,3));
}

4 建立总测试文件TestAll.cpp

代码语言:javascript
复制
#include "gtest/gtest.h"
#include <iostream>
using namespace std;


int main(int argc,char* argv[])
{
        testing::GTEST_FLAG(output) = "xml:"; //若要生成xml结果文件
        testing::InitGoogleTest(&argc,argv); //初始化
        RUN_ALL_TESTS();                     //跑单元测试
        return 0;
}

在GTestApp目录下新建lib目录,并复制libgtest.a到其中

代码语言:javascript
复制
mkdir lib
cp /home/jerery/googletest-main/lib/*.a ./lib

编译

代码语言:javascript
复制
g++ calculator.h calculator.cpp calculatorTest.cpp TestAll.cpp -o test -lgtest -lgmock -lpthread -std=c++14

运行测试

代码语言:javascript
复制
./test --gtest_output=xml
root@jerry-virtual-machine:/home/jerry/googletest-main/googletest/myworkspace/calculator# ./test --gtest_output=xml
[==========] Running 4 tests from 4 test suites.
[----------] Global test environment set-up.
[----------] 1 test from AddTest
[ RUN      ] AddTest.AddTestCase
[       OK ] AddTest.AddTestCase (0 ms)
[----------] 1 test from AddTest (0 ms total)


[----------] 1 test from MinusTest
[ RUN      ] MinusTest.MinusTestCase
[       OK ] MinusTest.MinusTestCase (0 ms)
[----------] 1 test from MinusTest (0 ms total)


[----------] 1 test from MultiplyTest
[ RUN      ] MultiplyTest.MutilplyTestCase
[       OK ] MultiplyTest.MutilplyTestCase (0 ms)
[----------] 1 test from MultiplyTest (0 ms total)


[----------] 1 test from DivideTest
[ RUN      ] DivideTest.DivideTestCase
[       OK ] DivideTest.DivideTestCase (0 ms)
[----------] 1 test from DivideTest (0 ms total)


[----------] Global test environment tear-down
[==========] 4 tests from 4 test suites ran. (0 ms total)
[  PASSED  ] 4 tests.

查看产生xml文件:test_detail.xml

代码语言:javascript
复制
cat test_detail.xml
?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="4" failures="0" disabled="0" errors="0" time="0." timestamp="2024-06-30T18:51:31.201" name="AllTests">
  <testsuite name="AddTest" tests="1" failures="0" disabled="0" skipped="0" errors="0" time="0." timestamp="2024-06-30T18:51:31.201">
    <testcase name="AddTestCase" file="calculatorTest.cpp" line="5" status="run" result="completed" time="0." timestamp="2024-06-30T18:51:31.201" classname="AddTest" />
  </testsuite>
  <testsuite name="MinusTest" tests="1" failures="0" disabled="0" skipped="0" errors="0" time="0." timestamp="2024-06-30T18:51:31.202">
    <testcase name="MinusTestCase" file="calculatorTest.cpp" line="8" status="run" result="completed" time="0." timestamp="2024-06-30T18:51:31.202" classname="MinusTest" />
  </testsuite>
  <testsuite name="MultiplyTest" tests="1" failures="0" disabled="0" skipped="0" errors="0" time="0." timestamp="2024-06-30T18:51:31.202">
    <testcase name="MutilplyTestCase" file="calculatorTest.cpp" line="11" status="run" result="completed" time="0." timestamp="2024-06-30T18:51:31.202" classname="MultiplyTest" />
  </testsuite>
  <testsuite name="DivideTest" tests="1" failures="0" disabled="0" skipped="0" errors="0" time="0." timestamp="2024-06-30T18:51:31.202">
    <testcase name="DivideTestCase" file="calculatorTest.cpp" line="14" status="run" result="completed" time="0." timestamp="2024-06-30T18:51:31.202" classname="DivideTest" />
  </testsuite>
</testsuites>
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-06-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云服务器利旧
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档