Google Test Introduction
Google Test Introduction
• Google Test assertions are macros that resemble function calls. You
test a class or function by making assertions about its behavior.
When an assertion fails, Google Test prints the assertion's source file
and line number location, along with a failure message. You may
also supply a custom failure message which will be appended to
Google Test's message.
• We have pairs of assertion. ASSERT_* versions generate fatal failures
when they fail, and abort the current function. EXPECT_* versions
generate nonfatal failures, which don't abort the current function.
UsuallyEXPECT_* are preferred, as they allow more than one
failures to be reported in a test. However, you should
use ASSERT_* if it doesn't make sense to continue when the
assertion in question fails.
Basic Assertions
ASSERT_TRUE(condition EXPECT_TRUE(condition
condition is true
); );
ASSERT_FALSE(conditio EXPECT_FALSE(conditio
condition is false
n); n);
Binary Comparison
• This section describes assertions that compare
two values.
Fatal assertion Nonfatal assertion Verifies
ASSERT_EQ(expecte
EXPECT_EQ(expected, actual); expected == actual
d, actual);
ASSERT_NE(val1, va EXPECT_NE(val1, val2); val1 != val2
l2);
ASSERT_LT(val1, val
EXPECT_LT(val1, val2); val1 < val2
2);
ASSERT_LE(val1, val EXPECT_LE(val1, val2); val1 <= val2
2);
ASSERT_GT(val1, va EXPECT_GT(val1, val2); val1 > val2
l2);
ASSERT_GE(val1, va
EXPECT_GE(val1, val2); val1 >= val2
l2);
ASSERT_GE(val1, val
2); EXPECT_GE(val1, val2); val1 >= val2
String Comparison
• To create a test:
• Use the TEST() macro to define and name a test
function, These are ordinary C++ functions that
don't return a value.
• In this function, along with any valid C++
statements you want to include, use the various
Google Test assertions to check values.
• The test's result is determined by the assertions; if
any assertion in the test fails (either fatally or non-
fatally), or if the test crashes, the entire test fails.
Otherwise, it succeeds.
An example
• For example, let's take a simple integer function:
• int Factorial(int n); // Returns the factorial of nA test case for this function might
look like:
• // Tests factorial of 0.
TEST(FactorialTest, HandlesZeroInput) {
EXPECT_EQ(1, Factorial(0));
}
• If you find yourself writing two or more tests that operate on similar data, you can
use a test fixture. It allows you to reuse the same configuration of objects for
several different tests.
• To create a fixture, just:
• Derive a class from ::testing::Test . Start its body with protected: or public: as we'll
want to access fixture members from sub-classes.
• Inside the class, declare any objects you plan to use.
• If necessary, write a default constructor or SetUp() function to prepare the objects
for each test. A common mistake is to spell SetUp() as Setup() with a small u - don't
let that happen to you.
• If necessary, write a destructor or TearDown() function to release any resources you
allocated in SetUp() . To learn when you should use the constructor/destructor and
when you should use SetUp()/TearDown(), read this FAQ entry.
• If needed, define subroutines for your tests to share.
Invoking the Tests
• TEST() and TEST_F() implicitly register their tests with Google Test. So, unlike with many
other C++ testing frameworks, you don't have to re-list all your defined tests in order to
run them.
After defining your tests, you can run them with RUN_ALL_TESTS() , which returns 0 if
all the tests are successful, or 1 otherwise. Note thatRUN_ALL_TESTS() runs all tests in
your link unit -- they can be from different test cases, or even different source files.
When invoked, the RUN_ALL_TESTS() macro:
• Saves the state of all Google Test flags.
• Creates a test fixture object for the first test.
• Initializes it via SetUp().
• Runs the test on the fixture object.
• Cleans up the fixture via TearDown().
• Deletes the fixture.
• Restores the state of all Google Test flags.
• Repeats the above steps for the next test, until all tests have run.
Writing the main() Function
• Example:
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
• The ::testing::InitGoogleTest() function parses the command
line for Google Test flags, and removes all recognized flags.
This allows the user to control a test program's behavior via
various flags, which we'll cover in AdvancedGuide. You must
call this function before callingRUN_ALL_TESTS(), or the
flags won't be properly initialized.
Known Limitations