[GTest] Simple use of C++ unit testing framework Google Test

1 Introduction

GTest is a cross-platform (Liunx, Mac OS X, Windows, Cygwin, Windows CE and Symbian) C++ unit testing framework released by Google.

GTest has the following features:

  • Provides a powerful set of assertions, including Boolean, integer, floating-point, string, etc.
  • Provide assertion method custom extension
  • Automatically collect test cases, no need for developers to reorganize
  • Provide death test function
  • Common use case initialization and cleanup work can be put into test fixtures, which are automatically called by gtest
  • Automatically generate multiple similar test cases using parameterization

Get the address: https://github.com/google/googletest

2. Initialization

After all the environment and compilation are ready, start learning GTest.

Let's learn the part of the GTEST test for the FCL case I use:

// gtest 头文件
#include <gtest/gtest.h>

// main 函数,用于初始化 gtest 和运行所有的测试样例
int main(int argc, char* argv[])
{
    
    
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

We have created many TESTs in the program (see below for details), and RUN_ALL_TESTS();they can be executed in batches.

The resulting output in the terminal is:

[==========] Running 5 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 5 tests from FCL_COLLISION

That is, in our current test program, there is a test sample, which contains five test segments, and these five tests all belong to FCL_COLLISION. See the following for details.

3. use

Add the function we want to test to GTEST:

GTEST_TEST(FCL_COLLISION, test_SplineMotion_rotated_spline_collide_test)
{
    
    
  test_SplineMotion_rotated_spline_collide_test<double>();
}
GTEST_TEST(FCL_COLLISION, OBB_Box_test)
{
    
    
  test_OBB_Box_test<double>();
}

GTEST_TEST(FCL_COLLISION, OBB_shape_test)
{
    
    
  test_OBB_shape_test<double>();
}

GTEST_TEST(FCL_COLLISION, OBB_AABB_test)
{
    
    
  test_OBB_AABB_test<double>();
}

GTEST_TEST(FCL_COLLISION, mesh_mesh)
{
    
    
  test_mesh_mesh<double>();
}

Here, if the test function is successfully added, the terminal will print when executing:

[ RUN      ] FCL_COLLISION.test_SplineMotion_rotated_spline_collide_test

You can intuitively see GTEST_TESTthe relationship between the parameters in and the terminal printout.

At the end of the above five functions to be tested, add an assertion macro function

EXPECT_TRUE(result.is_collide);

Note, result.is_collideit is a custom bool type state variable.

In this way, we can know the result of the test. Displayed in the terminal as:

[       OK ] FCL_COLLISION.test_SplineMotion_rotated_spline_collide_test (0 ms)

Note:

The assertion (assertion) macro of gtest is used here. include:

  • ASSERT_XXX(condition), with return types of successandfatal failture
  • EXPECT_XXX(condition), with return types of successandnon-fatal failture

The result of the assertion is condition(expected, actual)judged according to the returned result. If false is returned, the result of the assertion is failure. Among them, fatal failure will cause the program to exit directly, and non-fatal failure will return an error message, and then continue to test subsequent cases. So in engineering, EXPECT_XXXmacro functions are commonly used.

The main ones EXPECT_XXX, ASSERT_XXXfunctions and their judgment conditions are shown in the following two tables.

insert image description here

insert image description here
There are more macros, you can find them online.

After all test samples are completed, the terminal prints out:

[----------] Global test environment tear-down
[==========] 5 tests from 1 test case ran. (3834 ms total)
[  PASSED  ] 5 tests.

Guess you like

Origin blog.csdn.net/qq_43557907/article/details/126118125