GTest - cheat sheet
A quick reference on parameterized testing and some useful commands in Google Test.
Parameterized testing
In GTest, parameterized testing allows the same test to be executed with different inputs. The following example demonstrates testing the isBalanced() method of a Parentheses class using various input strings.
struct ParenthesesClassFixture : public testing::TestWithParam<std::pair<std::string, bool>> {
Parentheses p;
void checkIsBalanceMethod(const std::string& result, bool expectedResult) {
EXPECT_EQ(p.isBalanced(result), expectedResult);
}
};
TEST_P(ParenthesesClassFixture, givenEmptyStringWhenIsBalancedIsCalledThenResultIsTrue) {
auto [result, expectedResult] = GetParam();
checkIsBalanceMethod(result, expectedResult);
}
INSTANTIATE_TEST_SUITE_P(MyInstantiationName,
ParenthesesClassFixture,
testing::Values(
std::make_pair("", true),
std::make_pair("", false),
std::make_pair("({)}", false),
std::make_pair("[({})]", true),
std::make_pair("{}([])", true),
std::make_pair("{()}[[{}]]", true),
std::make_pair("{(([]))]((", false)
)
);
Important test flags
Command | Description |
---|---|
--gtest_filter=<TEST_NAME> | Runs only tests with the specified name. |
--gtest_repeat=<COUNT> | Repeats the tests a specified number of times (useful for investigating undefined behavior). |
--gtest_shuffle | Shuffles the order of tests, so they are not executed in the order they appear in the source file. |