C++ Academy · Lesson

Fixtures and Setup

Share test setup.

Lesson 3 of 413 steps

Fixtures and Setup is a free C++ Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

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}; }
};

Using TEST_F

Tests that use a fixture are written with TEST_F, passing the fixture class name as the first argument.

#include <gtest/gtest.h>
#include <vector>

class VectorTest : public ::testing::Test {
protected:
    std::vector<int> v{1, 2, 3};
};

TEST_F(VectorTest, HasThree) {
    EXPECT_EQ(v.size(), 3u);
}

TEST_F(VectorTest, FirstIsOne) {
    EXPECT_EQ(v.front(), 1);
}

Fresh Instance per Test

GTest creates a new fixture object for every test. Tests never share mutable state, so they stay independent and order does not matter.

SetUp vs Constructor

You can initialize members in the constructor or in SetUp(). Use SetUp() when initialization can throw or when you want to call virtual methods safely.

#include <gtest/gtest.h>

class Resource : public ::testing::Test {
protected:
    int* data = nullptr;
    void SetUp() override { data = new int(10); }
    void TearDown() override { delete data; }
};

TEST_F(Resource, HasTen) {
    EXPECT_EQ(*data, 10);
}

TearDown for Cleanup

TearDown() releases resources acquired in SetUp(). With RAII members, the destructor often handles this automatically and you may not need TearDown() at all.

Suite-Level Setup

For expensive shared resources, use static SetUpTestSuite() and TearDownTestSuite(). They run once per suite, not per test.

#include <gtest/gtest.h>

class DbTest : public ::testing::Test {
protected:
    static void SetUpTestSuite() { /* open shared connection */ }
    static void TearDownTestSuite() { /* close it */ }
};

Per-Test vs Per-Suite

Choose carefully.

  • SetUp/TearDown: per test, isolated, safe
  • SetUpTestSuite/TearDownTestSuite: per suite, shared, faster but tests can interfere

Helper Methods

Fixtures can hold helper methods used by multiple tests, keeping each test focused on its assertions.

#include <gtest/gtest.h>
#include <string>

class ParserTest : public ::testing::Test {
protected:
    int parse(const std::string& s) { return std::stoi(s); }
};

TEST_F(ParserTest, ParsesNumber) {
    EXPECT_EQ(parse("42"), 42);
}

Global Environment

For process-wide setup (run once before any test), register a ::testing::Environment via AddGlobalTestEnvironment. Useful for logging or framework init.

Keeping Tests Independent

Because each TEST_F gets a fresh fixture, never rely on side effects from another test. Independence makes failures easy to diagnose and tests safe to parallelize.

Quick Check

Recall fixture lifecycle.

Recap

You learned fixtures.

  • Derive from ::testing::Test, use TEST_F
  • SetUp/TearDown run per test; fresh object each time
  • SetUpTestSuite for shared, once-per-suite resources
  • Helpers and global environments aid larger suites
Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Fixtures and Setup” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Fixtures and Setup”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

What will I learn in “Fixtures and Setup”?

Share test setup. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Fixtures and Setup” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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