Google Test

gtest

Posted by Hao on February 19, 2021

General Unit Test

The general unit test includes:

  • Code review
  • Static code scanning
  • Unit test case writing
  1. Unit testing is to test and verify the smallest unit in the software. Generally speaking, it is a function or a class in the code. Unit testing must be a white box test.
  2. Unit testing is usually completed by development engineers, and is usually submitted to the code base along with the development code.
  3. Unit testing is the most rigorous software testing method. It is the verification method closest to the underlying implementation of the code. It can ensure the quality of partial code in the early stages of software development with minimal cost.
  4. Unit testing is executed in an automated manner, so it can bring high benefits in a large number of regression testing scenarios.
  5. The implementation process of unit testing can also help development engineers improve the design and implementation of the code, and can provide examples of function usage in the unit test code, because the specific form of unit testing is: a combination of various input parameters for a function Make calls, these calling methods constitute the instructions for use of the function.

Unit Test need to observe the following principles

FIRST = Fast, Independent, Repeatable, Self validating and Timely.

  1. The test case can verify the correctness of the function
  2. The test case covers boundary conditions as much as possible
  3. Some exceptions and error handling

What is Google Test?

Google Test (also known as gtest for e.g. the ROS environment) is a unit testing library for the C++ programming language, based on the xUnit architecture.

Who is using Google Test?

Besides being developed and used at Google, many other projects implement Google Test as well:

  • Chromium projects (behind the Chrome browser and Chrome OS)
  • LLVM compiler
  • Protocol Buffers (Google’s data interchange format)
  • OpenCV computer vision library
  • Gromacs molecular dynamics simulation package

gtest blogs

Example Or Alternative Link

  1. Compile gtest
    • Download gtest from here
    • Come into gtest folder and unzip gtest.zip by command:
      1
      2
      
         g++ -I./include -I./ -c ./src/gtest-all.cc  
         ar -rv libgtest.a gtest-all.o
      
    • The file libgtest.a and folder include under gtest need to be added in our own project.
    • Verify if it works:
      1
      2
      3
      
         cd ${GTEST_DIR}/make  
         make  
         ./sample1_unittest  
      

      it works if we get follows:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      
          Running main() from gtest_main.cc  
         [==========] Running 6 tests from 2 test cases.  
         [----------] Global test environment set-up.  
         [----------] 3 tests from FactorialTest  
         [ RUN      ] FactorialTest.Negative  
         [       OK ] FactorialTest.Negative (0 ms)  
         [ RUN      ] FactorialTest.Zero  
         [       OK ] FactorialTest.Zero (0 ms)  
         [ RUN      ] FactorialTest.Positive  
         [       OK ] FactorialTest.Positive (0 ms)  
         [----------] 3 tests from FactorialTest (0 ms total)  
                
         [----------] 3 tests from IsPrimeTest  
         [ RUN      ] IsPrimeTest.Negative  
         [       OK ] IsPrimeTest.Negative (0 ms)  
         [ RUN      ] IsPrimeTest.Trivial  
         [       OK ] IsPrimeTest.Trivial (0 ms)  
         [ RUN      ] IsPrimeTest.Positive  
         [       OK ] IsPrimeTest.Positive (0 ms)  
         [----------] 3 tests from IsPrimeTest (0 ms total)  
                
         [----------] Global test environment tear-down  
         [==========] 6 tests from 2 test cases ran. (0 ms total)  
         [  PASSED  ] 6 tests.  
      

      The latest gtest version (after v1.6) has been updated by cmake installation.

  2. gtest and gmock case
    • Download and install Google Test and Google Mock
  3. How to call gtest There is a gtest_main module, and main function inside. (/googletest/googletest/src/gtest_main.cc)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
     GTEST_API_ int main(int argc, char **argv) {
         printf("Running main() from %s\n", __FILE__);
         testing::InitGoogleTest(&argc, argv);
         return RUN_ALL_TESTS();
     }
    
     // It will run all the TestCase which are instantiated by Test
     #define RUN_ALL_TESTS()\
       (::testing::UnitTest::GetInstance()->Run())
     }  // namespace testing
    

    TEST(test_suite_name, test_case_name)

Test Fixtures

A test fixture is a class that inherits from ::testing::Test and whose internal state is accessible to tests that use it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
```
struct BankAccountTest : testing::Test
{
  BankAccount* account;
  BankAccountTest()
  {
    account = new BankAccount;
  }
  virtual ~BankAccountTest()
  {
    delete account;
  }
};
```    But the test fixture is not an actual test: it’s just rules for setting up and destroying objects that you need for testing. Anyways, the actual test now uses a `TEST_F` instead of the usual `TEST`, and its first argument is the fixture:
```
TEST_F(BankAccountTest, CanDepositMoney)
{
  account->deposit(100);
  EXPECT_EQ(100,account->balance);
}
```