Contents
  1. 1. Install gtest
  2. 2. Integrate a test case in gtest
  3. 3. Reference

Start to enable unit test for C++.

Verified on ubuntu 16.04 and 18.04.

Install gtest

  • install gtest source package

    1
    sudo apt install libgtest-dev
  • Build gtest lib

    1
    2
    3
    4
    5
    cd /usr/src/gtest
    sudo mkdir build
    cd build
    sudo cmake ../
    sudo make -j4
  • move the libs( to /user/local/bin

    1
    2
    3
    4
    /usr/src/gtest/build# ls libgtest*
    libgtest.a libgtest_main.a

    sudo cp -a libgtest*.a /usr/local/lib/

Integrate a test case in gtest

  • Prepare functions to test

    • func.cpp

      1
      2
      3
      4
      5
      6
      #include "func.h"

      int add(int a, int b)
      {
      return a + b;
      }
    • func.h

      1
      2
      3
      4
      5
      6
      #ifndef _FUNC_H
      #define _FUNC_H

      int add(int a, int b);

      #endif
  • Create a test case

    • func_test.cpp
      1
      2
      3
      4
      5
      6
      7
      8
      #include "gtest/gtest.h"
      #include "func.h"

      TEST(AddTest, AddTestCase)
      {
      ASSERT_EQ(2, add(1, 1));
      ASSERT_EQ(3, add(2, 1));
      }
  • Create the main function for all test cases

    • main.cpp
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      #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;
      }
  • Build with CMakeLists.txt, depending on libgtest, libpthread

    1
    2
    3
    4
    5
    6
    7
    8
    cmake_minimum_required(VERSION 2.8)

    add_executable(main
    main.cpp
    func.cpp
    func_test.cpp
    )
    target_link_libraries(main gtest pthread)
  • Run test and result:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    ./main
    [==========] Running 1 test from 1 test case.
    [----------] Global test environment set-up.
    [----------] 1 test from AddTest
    [ RUN ] AddTest.AddTestCase
    [ OK ] AddTest.AddTestCase (0 ms)
    [----------] 1 test from AddTest (0 ms total)

    [----------] Global test environment tear-down
    [==========] 1 test from 1 test case ran. (0 ms total)
    [ PASSED ] 1 test.
  • xml repot

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    $ ./main --gtest_output=xml

    $ ls *.xml
    test_detail.xml

    $ cat test_detail.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <testsuites tests="1" failures="0" disabled="0" errors="0" timestamp="2018-11-12T22:45:07" time="0" name="AllTests">
    <testsuite name="AddTest" tests="1" failures="0" disabled="0" errors="0" time="0">
    <testcase name="AddTestCase" status="run" time="0" classname="AddTest" />
    </testsuite>
    </testsuites>

Reference

Contents
  1. 1. Install gtest
  2. 2. Integrate a test case in gtest
  3. 3. Reference