GTest tutorial

 

Content

  • What is a good test case?
    • Easy to read the code
    • Easy to read the fail message. No need to go back to the code to understand the fail reason.
  • Quick Definitions
    • Test Suite
      • A group of logically related tests cases.
    • Fixture
      • A Class factoring out common code for multiple cases
    • Mock Classes/Objects
      • Simplified and lightweight classes to untangle complex dependencies while testing.
      • A way to provide a fake return of a function for testing in order to untangle dependencies.
  • Test Framework comparison
    • GTest
    • Catch2
    • Doctest
    • Boost.Test
  • Assert vs Expect
    • Assert macro will stop the test program once it is failed.
    • Expect macro will keep running the following test cases.
  • Macros for
    • Binary Comparison Assertions
    • C String 
    • Floating points
    • Predicates
      • This is a Marco by default providing more test fail message.
    • Exceptions
    • Death Test
      • This is used to test if your program dead with a correct dead value. For example, throwing -1.
    • Other Macros
      • SUCCEED()
      • FAIL()
      • ...
  • Test Fixtures
    • What is it?
      • They referred to the common codes shared by the same test suite.
      • SetUp(), TearDown() --- run for every test case
      • SetUpTestSuite(), TearDownTestSuite() --- run for the test suite once

      • Need to use TEST_F() macro
    • Set Test Timeout
      • You can do time counting in SetUp() and TearDown().
    • Sharing Resources
      • You can use SetUpTestSuite(), TearDownTestSuite() to manage your shared resources
  • Disabling Tests
    • Use keyword to name your test case in order to skip it --- DISABLED_
    • Use macro in test case --- GTEST_SKIP(), to skip a test
    • Use macro in GTest class --- GTEST_SKIP(), to skip a test suite
    • Use command to filter out the test cases during compilation
  • Customizing output
    • This is helpful when you need a better fail message.
    • You need to override and friend some functions.
      • friend std::ostream& operator<< ()
      • friend void PrintTo()
  • Sharing the Test Logic
    • For Various Inputs
      • Parameterized Test
        • You can define the GTest class with data as input parameters.
        • You can also use the name generator to create name for the test case. 
    • For Various Implementations and Various Inputs
      • Parameterized Test with Combine()
        • With this way, you can parameterized both the implementations and input data.
        • You can also use the name generator in this case
  • Typed Tests / Type-Parameterized Tests
    • What is it? When do you use this?
      • You use it when you want to do interface testing.
      • You defined the test flow for the base class, then you want to parameterize the child class. Those child class carry the actual implementation.
    • How to do this?
      • The macro TYPED_TEST_SUITE() will be needed
      • using testing::Types; also needed
    • Compare Typed Tests vs Type-Parameterized Tests
      • Different macros are needed but it seems getting the same result.
  • How to test private code?
    • We can use friend macro. We recommend test public methods only. 
  • SCOPED_TRACE()
    • When do we use it?
      • We use it when we are testing the same function call in different subroutines.
      • When the test failed, it is hard to know which function call is failed.
      • The SCOPED_TRACE() may be helpful.

All powerpoint slides

Learnt from here

Comments