0Pricing
C++ Academy · Lesson

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

  1. Setting Up GTest
  2. Assertions and Matchers
  3. Fixtures and Setup
  4. Mocking with GMock
← Back to C++ Academy