一个google Test文件案例

2024-09-10 15:19:50 浏览数 (4)

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>

0 人点赞