一个google Test文件案例

简介: 一个google Test文件案例

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>
目录
相关文章
|
编解码 数据可视化
Google Earth Engine——gee文件导出到drive:影像等大文件导出100000000超限解决办法
Google Earth Engine——gee文件导出到drive:影像等大文件导出100000000超限解决办法
2347 0
Google Earth Engine——gee文件导出到drive:影像等大文件导出100000000超限解决办法
|
6天前
|
JavaScript 前端开发 测试技术
一个google Test文件C++语言案例
这篇文章我们来介绍一下真正的C++语言如何用GTest来实现单元测试。
9 0
|
4月前
Google Earth Engine(GEE)——Segmentation.seedGrid和SNIC (Simple Non-Iterative Clustering)案例和错误缺少特征错误分析
Google Earth Engine(GEE)——Segmentation.seedGrid和SNIC (Simple Non-Iterative Clustering)案例和错误缺少特征错误分析
91 0
|
机器学习/深度学习 安全 JavaScript
Google Earth Engine(GEE)——R 语言 Google 地球引擎20个基本案例分析
Google Earth Engine(GEE)——R 语言 Google 地球引擎20个基本案例分析
845 0
Google Earth Engine(GEE)——R 语言 Google 地球引擎20个基本案例分析
|
缓存 前端开发
[Css 修改后 Google浏览器上无效果] 文件上有:Generated source files should not be edited 的警告
[Css 修改后 Google浏览器上无效果] 文件上有:Generated source files should not be edited 的警告
236 0
[Css 修改后 Google浏览器上无效果] 文件上有:Generated source files should not be edited 的警告
|
算法 双11 Python
6.18专属特惠活动:Google Earth Engine专栏、GEE教程训练专栏、GEE-python专栏和GEE案例分析专栏等限时特价
6.18专属特惠活动:Google Earth Engine专栏、GEE教程训练专栏、GEE-python专栏和GEE案例分析专栏等限时特价
116 0
6.18专属特惠活动:Google Earth Engine专栏、GEE教程训练专栏、GEE-python专栏和GEE案例分析专栏等限时特价
|
存储 JSON Java
如何生成Google-service.json 文件,新建assert目录和读取以及gson文件解析
如何生成Google-service.json 文件,新建assert目录和读取以及gson文件解析
680 0
|
iOS开发 MacOS
使用 google_breakpad 分析 Electron 崩溃日志文件
本机(MacBook Pro)上 Electron 的路径:/usr/local/lib/node_modules/electron/dist/Electron.app/Contents/MacOS/Electron
1188 1
使用 google_breakpad 分析 Electron 崩溃日志文件
|
API
Google Earth Engine(GEE)——2.提取降水值案例(R-GEE版)
Google Earth Engine(GEE)——2.提取降水值案例(R-GEE版)
473 0
Google Earth Engine(GEE)——2.提取降水值案例(R-GEE版)
Google Earth Engine(GEE)——Join连接的案例分析
Google Earth Engine(GEE)——Join连接的案例分析
280 0
Google Earth Engine(GEE)——Join连接的案例分析