ASSERT_EQ(x.size(), y.size()) << "Vectors x and y are of unequal length";
for (int i = 0; i < x.size(); ++i) {
EXPECT_EQ(x[i], y[i]) << "Vectors x and y differ at index " << i;
}
// Tests factorial of 0.
TEST(FactorialTest, HandlesZeroInput) {
EXPECT_EQ(Factorial(0), 1);
}
// Tests factorial of positive numbers.
TEST(FactorialTest, HandlesPositiveInput) {
EXPECT_EQ(Factorial(1), 1);
EXPECT_EQ(Factorial(2), 2);
EXPECT_EQ(Factorial(3), 6);
EXPECT_EQ(Factorial(8), 40320);
}
Test Fixtures: Using the Same Data Configuration for Multiple Tests
- Derive a class from
::testing::Test
. Start its body withprotected:
, 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 spellSetUp()
asSetup()
with a smallu
- Useoverride
in C++11 to make sure you spelled it correctly. - If necessary, write a destructor or
TearDown()
function to release any resources you allocated inSetUp()
. To learn when you should use the constructor/destructor and when you should useSetUp()/TearDown()
, read the FAQ. - If needed, define subroutines for your tests to share.
When using a fixture, use TEST_F()
instead of TEST()
as it allows you to access objects and subroutines in the test fixture:
TEST_F(TestFixtureName, TestName) {
... test body ...
}