Fixtures and Setup
Share test setup.
Fixtures and Setup is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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, safeSetUpTestSuite/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, useTEST_F SetUp/TearDownrun per test; fresh object each timeSetUpTestSuitefor shared, once-per-suite resources- Helpers and global environments aid larger suites
Frequently asked questions
Is the “Fixtures and Setup” lesson free?
Yes — the full text of “Fixtures and Setup” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
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
- Setting Up GTest
- Assertions and Matchers
- Fixtures and Setup
- Mocking with GMock