Add DISABLED_
prefix to test name
Use EXPECT_FLOAT_EQ
main.cpp
#include <gtest/gtest.h>
int main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Simplest type of test, declare it on any .CPP file and that's all
TEST(GroupName, TestName)
{
EXPECT_EQ(1, 1);
ASSERT_EQ(1, 1);
}
Test with base class (allocation before running + deallocation after running, etc)
class MyFixture
: public testing::Test
{
public:
MyFixture()
: Test()
{
// Allocation point n.1
// when Fixture is created
}
~MyFixture()
{
// Deallocation point n.1
// when Fixture is destroyed
}
void SetUp() override
{
// Allocation point n.2
// just before running each test
}
void TearDown() override
{
// Deallocation point n.2
// just after running each test
}
};
// Usage
TEST_F(MyFixture, TestName)
{
// this == MyFixture*
}
Declare a set of test cases, generic fixture to test them all
Func to be tested: int Utils::sum(int, int)
// Declare data structure for storing each test case
struct SumTestCase
{
int left;
int right;
int expectedResult;
};
// Declare parametrized fixture
class SumTest
: public testing::Test
, public testing::WithParamInterface<SumTestCase>
{
// No need for extra implementation (in this case)
}
// Declare generic test using the parameters
TEST_P(SumTest, CheckSumResult)
{
const auto& tcase = GetParam(); // retrieve current test case
// non-critical assertion
EXPECT_EQ(Utils::sum(tcase.left, tcase.right), tcase.expectedResult);
}
// Run tests using sets of parameters
INSTANTIATE_TEST_CASE_P(OnlyPositives, SumTest, testing::Values(
SumTestCase{ 2, 100, 102 },
SumTestCase{ 15, 15, 30 },
SumTestCase{ 30, 12, 42 },
SumTestCase{ 2, 0, 2 },
SumTestCase{ 0, 2, 2 }
));
INSTANTIATE_TEST_CASE_P(OnlyNegatives, SumTest, testing::Values(
SumTestCase{ -2, -100, -102 },
SumTestCase{ -15, -15, -30 },
SumTestCase{ -30, -12, -42 },
SumTestCase{ -2, 0, -2 },
SumTestCase{ 0, -2, -2 }
));
INSTANTIATE_TEST_CASE_P(PositiveNegativeMix, SumTest, testing::Values(
SumTestCase{ 0, 0, 0 },
SumTestCase{ -2, 100, 98 },
SumTestCase{ 15, -15, 0 },
SumTestCase{ -30, 12, -18 },
SumTestCase{ -2, 1, -1 },
SumTestCase{ 1, -2, -1 }
));