Using a test fixture
For tests that require a common setup and teardown for multiple test cases, Google Test offers the concept of a test fixture. This is achieved by defining a class derived from ::testing::Test
and then using the TEST_F
macro to write tests that use this fixture:
class CalculatorTest : public ::testing::Test { protected: void SetUp() override { // Code here will be called immediately before each test calculator.reset(new Calculator()); } void TearDown() override { // Code here will be called immediately after each test calculator.reset(); } std::unique_ptr<Calculator> calculator; }; TEST_F(CalculatorTest, CanAddPositiveNumbers) { ...