一个google Test文件案例

admin 2024年7月1日08:44:17评论1 views字数 3374阅读11分14秒阅读模式
1, 建立头文件calculator.h

//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

//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

//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

#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到其中

mkdir libcp /home/jerery/googletest-main/lib/*.a ./lib

编译

g++ calculator.h calculator.cpp calculatorTest.cpp TestAll.cpp -o test -lgtest -lgmock -lpthread -std=c++14

运行测试

./test --gtest_output=xmlroot@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

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 Test文件案例

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2024年7月1日08:44:17
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   一个google Test文件案例http://cn-sec.com/archives/2902953.html

发表评论

匿名网友 填写信息