Google Test / Mock personal cheat sheet

Overview

Last time, I made a Google Mock environment for C that I use for my work, so I decided to study a little more. There is something like "Google C ++ Mocking Framework cheat sheet", but it's hard to understand and it doesn't come to my mind just by looking at it. While reading, I decided to make something like a cheat sheet for myself. We will update it one by one.

Summary of macros

TEST(), TEST_F(), TEST_P() https://github.com/google/googletest/blob/master/googletest/docs/primer.md#simple-tests

TEST(TestSuiteName, TestName) {
  ... test body ...
}

ASSERT_EQ, EXPECT_EQ http://opencv.jp/googletestdocs/primer.html#primer-binary-comparison

MOCK_METHOD*, MOCK_CONST_METHOD*

EXPECT_CALL

::testing::_ https://github.com/google/googletest/blob/master/googlemock/docs/cheat_sheet.md#wildcard

Times http://opencv.jp/googlemockdocs/fordummies.html#cardinalities

WillOnce, WillRepeatedly http://opencv.jp/googlemockdocs/fordummies.html#fordummies-general-syntax

using ::testing::Return;...
EXPECT_CALL(turtle, GetX())
    .Times(5)
    .WillOnce(Return(100))
    .WillOnce(Return(150))
    .WillRepeatedly(Return(200));

::testing::Return https://github.com/google/googletest/blob/master/googlemock/docs/cheat_sheet.md#actions-actionlist

RetiresOnSaturation http://opencv.jp/googlemockdocs/fordummies.html#expectation-sticky

Google Mock Expectation indicates "sticky" by default

::testing::Invoke http://opencv.jp/googlemockdocs/cheatsheet.html#cheatsheet-using-afunction-or-functor

double Distance(Unused, double x, double y) { return sqrt(x*x + y*y); }
...
EXPECT_CALL(mock, Foo("Hi", _, _)).WillOnce(Invoke(Distance));

NiceMock<> http://opencv.jp/googlemockdocs/cookbook.html#nice-strict

Grammatic

Test fixture

https://github.com/google/googletest/blob/master/googletest/docs/primer.md#test-fixtures-using-the-same-data-configuration-for-multiple-tests

RUN_ALL_TESTS and main function

http://opencv.jp/googletestdocs/primer.html#primer-invoking-the-tests http://opencv.jp/googlemockdocs/fordummies.html#fordummies-using-mocks-in-tests

::testing::InitGoogleMock

Multiple Exceptions (ʻEXPECT_CALL`)

http://opencv.jp/googlemockdocs/fordummies.html#fordummies-using-multiple-expectations

EXPECT_CALL(turtle, Forward(_));  // #1
EXPECT_CALL(turtle, Forward(10))  // #2
    .Times(2);

InSequence http://opencv.jp/googlemockdocs/fordummies.html#fordummies-ordered-vs-unordered

ʻON_CALL` and default action

http://opencv.jp/googlemockdocs/cheatsheet.html#action

The one I didn't understand

Recommended Posts

Google Test / Mock personal cheat sheet
Curry cheat sheet
SQLite3 cheat sheet
pyenv cheat sheet
Cheat sheet when scraping with Google Colaboratory (Colab)
conda command cheat sheet
PIL / Pillow cheat sheet
Linux command cheat sheet
How to install Google Test / Google Mock in Visual Studio 2019
ps command cheat sheet
Spark API cheat sheet
Python3 cheat sheet (basic)
PySpark Cheat Sheet [Python]
Python sort cheat sheet
Go language cheat sheet
tox configuration file cheat sheet
numpy memory reuse cheat sheet
Test embedded software with Google Test
Try Google Mock with C
[Python3] Standard input [Cheat sheet]
Data Science Cheat Sheet (Python)
Slack API attachments cheat sheet
Python Django Tutorial Cheat Sheet
scikit learn algorithm cheat sheet
Apache Beam Cheat Sheet [Python]