Fixtures and Setup
Share test setup.
Sharing Setup Across Tests
When several tests need the same objects, a fixture lets you define that setup once. A fixture is a class deriving from ::testing::Test.
Defining a Fixture
Members of the fixture are accessible in each test that uses it. SetUp() runs before every test; TearDown() runs after.
#include <gtest/gtest.h>
#include <vector>
class VectorTest : public ::testing::Test {
protected:
std::vector<int> v;
void SetUp() override { v = {1, 2, 3}; }
};All lessons in this course
- Setting Up GTest
- Assertions and Matchers
- Fixtures and Setup
- Mocking with GMock