Plain Google Quick Test Reference Guide 1
Plain Google Quick Test Reference Guide 1
Plain Google Quick Test Reference Guide 1
Example Tests
• The following code snippet is a simple test that one might write when driving a bowling scorer object.
TEST(BowlingGameTest,
AllGuttersGetsZero)
{
BowlingGame
game;
vector<int>
rolls(20,
0);
ASSERT_EQ(0,
game.Score(rolls));
}
• The following code snippets demonstrate a simple data fixture and a test that uses that fixture.
class
QueueTest
:
public
::testing::Test
{
protected:
virtual
void
SetUp()
{
q1_.Enqueue(1);
}
Queue<int>
q0_;
Queue<int>
q1_;
};
TEST_F(QueueTest,
IsEmptyInitially)
{
ASSERT_EQ(0,
q0_.size());
}
TEST_F(QueueTest,
DequeueWorks)
{
int*
n
=
q1_.Dequeue();
ASSERT_TRUE(n
!=
NULL);
EXPECT_EQ(1,
*n);
EXPECT_EQ(0,
q1_.size();
delete
n;
}
Creating Tests
TEST(test_case_name, test_name) test_case_name is the name of the Test Case. test_name is the name of the
individual scenario contained within this test method. A test case can and
probably should contain multiple tests.
TEST_F(test_case_name, test_name) This is the same as above except the test_case_name is the name of a test
fixture. Test fixtures allow you to use the same data and setup across multiple
tests.
* ASSERT_* yields a fatal failure and returns from the current function, while EXPECT_* yields a nonfatal failure, allowing
the function to continue running.
Basic Assertion
Fatal Assertion Nonfatal Assertion Verifies
SUCCEED() This is purely documentation and currently doesnʼt generate any visible output.
In the future google may choose to have this macro produce messages.
String Comparison
Fatal Assertion Nonfatal Assertion Verifies
ASSERT_STREQ(expected, actual) EXPECT_STREQ(expected, actual) the two C strings have the same
content
ASSERT_STRCASEEQ(exp, act) EXPECT_STRCASEEQ(exp, act) the two C strings have the same
content, ignoring case
Examples:
ASSERT_THROW(Foo(5),
bar_exception);
EXPECT_NO_THROW({
int
n
=
5;
Bar(&n)
});
ASSERT_FLOAT_EQ(exp, act) EXPECT_FLOAT_EQ(exp, act) The two float values are almost equal.
ASSERT_DOUBLE_EQ(exp, act) EXPECT_DOUBLE_EQ(exp, act) The two double values are almost
equal
ASSERT_NEAR(val1, val2, abs_err) EXPECT_NEAR(val1, val2, abs_err) The difference between the val1 and
val2 doesnʼt exceed abs_err .
* By “almost equal” we mean the two values are within 4 ULPʼs from each other.
Windows HRESULT Assertions
Fatal Assertion Nonfatal Assertion Verifies
ASSERT_PRED2(pred, val1, val2) EXPECT_PRED2(pred, val1, val2) pred(val1, val2) returns true