Open In App

GTest Framework

What is Googletest?

Why Googletest?

Nomenclature:

Basic Concepts:

In below lines * represent multiple character such as EQ / NE / LT / LE / GT / GE.



Fatal assertion Nonfatal assertion Verifies
ASSERT_TRUE (condition); EXPECT_TRUE (condition); condition is true
ASSERT_FALSE (condition); EXPECT_FALSE (condition); condition is false
ASSERT_STREQ(str1, str2); EXPECT_STREQ(str1, str2); the two string str1 and str2 have the same content
ASSERT_STRNE(str1, str2); EXPECT_STRNE(str1, str2); the two strings str1 and str2 have different content
ASSERT_STRCASEEQ(str1, str2); EXPECT_STRCASEEQ(str1, str2); the two string str1 and str2 have the same content, ignoring the case
ASSERT_STRCASENE(str1, str2); EXPECT_STRSTRCASENE(str1, str2); the two strings str1 and str2 have different content, ignoring the case

Simple tests:

TEST():

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

Test Fixtures:

TEST_F(TestFixtureName, TestName) {
... test body ...
}

Invoking the Tests:

Examples:

A factorial function:

int factorial(int n)
{
// If n < 1;
return -1;

// Else factorial = n!;
return factorial;
}

TEST(FactorialTest, FactorialOfZeroShouldBeOne)
{
ASSERT_EQ(1, factorial(0));
}

TEST(FactorialTest, FactorialOfPositiveNos)
{
ASSERT_EQ(1, factorial(1));
ASSERT_EQ(24, factorial(4));
ASSERT_EQ(120, factorial(5));
}

int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

Command Line Arguments:

–gtest_output=”xml:report.xml”



–gtest_repeat=2

–gtest_break_on_failure

–gtest_filter=<test string>

Article Tags :