skip to content

In GoogleTest, how do typed tests differ from type-parameterized tests, and why must a typed test body use this-> for fixture members?

level: middleimportance: should knowfreq 35%

answer

  1. the type list known up front or later
  2. TypeParam and TestFixture
  3. register, then instantiate
  4. members of a dependent base class

basics

~20 s

Typed tests fix the type list where the suite is written with TYPED_TEST_SUITE; type-parameterized tests define patterns first and are instantiated later, possibly elsewhere. The body is a member of a class template derived from the fixture, so its members need this->.

solid answer

~40 s

Both run the same body once per type. **Typed tests** take a fixture class template, a type list such as `using ValueTypes = testing::Types<int, double, std::string>;`, `TYPED_TEST_SUITE(Fixture, ValueTypes)` and `TYPED_TEST` bodies, so the list must be known when the suite is written. **Type-parameterized tests** declare `TYPED_TEST_SUITE_P`, define `TYPED_TEST_P` patterns, list them in `REGISTER_TYPED_TEST_SUITE_P`, and leave `INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, Fixture, Types)` to whoever has the types, which suits a conformance suite in a header. Inside either body `TypeParam` names the current type and `TestFixture` the fixture. The body is a member of a class template deriving from `Fixture<TypeParam>`, a **dependent base**, so unqualified names are not looked up there: write `this->store_` or `TestFixture::kName`, and `typename TestFixture::Store` for a nested type.

code

cpp · 35 lines
cpp
#include <gtest/gtest.h>
#include <map>
#include <string>

template <typename T>
class TypedKvTest : public testing::Test {
 protected:
  using Store = std::map<std::string, T>;
  Store store_;
};

using ValueTypes = testing::Types<int, double, std::string>;
TYPED_TEST_SUITE(TypedKvTest, ValueTypes);

TYPED_TEST(TypedKvTest, OverwriteKeepsOneEntry) {
  typename TestFixture::Store& store = this->store_;  // this-> is required
  store["k"] = TypeParam{};
  store["k"] = TypeParam{};
  EXPECT_EQ(store.size(), 1u);
}

// Type-parameterized: define now, instantiate later (possibly elsewhere).
template <typename T>
class ValueConformanceTest : public testing::Test {};
TYPED_TEST_SUITE_P(ValueConformanceTest);

TYPED_TEST_P(ValueConformanceTest, CopiesCompareEqual) {
  TypeParam a{};
  TypeParam b = a;
  EXPECT_EQ(a, b);
}
REGISTER_TYPED_TEST_SUITE_P(ValueConformanceTest, CopiesCompareEqual);

using Scalars = testing::Types<int, double>;
INSTANTIATE_TYPED_TEST_SUITE_P(Scalar, ValueConformanceTest, Scalars);

go deeper

for a junior

Recall that typed tests run one body per type from a testing::Types list, and that TypeParam names the current type.

for a middle

Contrast TYPED_TEST_SUITE with the TYPED_TEST_SUITE_P, REGISTER and INSTANTIATE sequence, and fix the this-> and comma errors.

for a senior

Choose between typed and value-parameterized suites by what varies, and keep conformance suites reusable through type-parameterized tests in headers.

for a principal

Decide when a shared type-parameterized conformance suite should gate every implementation of an interface or concept across teams.

## The problem typed tests solve A key-value store templated on its value type should behave the same for `int`, `double` and `std::string`. Writing one test per type multiplies tests by types. GoogleTest offers two ways to write the logic once and run it per type. ## Typed tests: the list is known up front 1. Define a fixture **class template** deriving from `testing::Test`. 2. Name a type list with an alias: `using ValueTypes = testing::Types<int, double, std::string>;`. 3. Attach it: `TYPED_TEST_SUITE(TypedKvTest, ValueTypes);`. 4. Write bodies with `TYPED_TEST(TypedKvTest, OverwriteKeepsOneEntry) { ... }`. GoogleTest generates one suite per type, named `TypedKvTest/0`, `TypedKvTest/1` and so on, and its output prints `where TypeParam = int` for each. The alias in step 2 is not optional style: the preprocessor splits macro arguments at commas outside parentheses, so `testing::Types<int, double>` written inline becomes several arguments and compilation fails. ## Type-parameterized tests: define now, instantiate later 1. Define the fixture class template as before. 2. Declare the suite: `TYPED_TEST_SUITE_P(ValueConformanceTest);`. 3. Write patterns with `TYPED_TEST_P(ValueConformanceTest, CopiesCompareEqual) { ... }`. 4. Register every pattern: `REGISTER_TYPED_TEST_SUITE_P(ValueConformanceTest, CopiesCompareEqual);`. 5. Instantiate wherever the types are known: `INSTANTIATE_TYPED_TEST_SUITE_P(Scalar, ValueConformanceTest, Scalars);`. Steps 1 to 4 can live in a header that a library ships; each implementation's tests include it and perform step 5 with their own types, possibly several times with different prefixes. The generated suites are named `Scalar/ValueConformanceTest/0` and so on. With a single type, the type can be written directly instead of a `Types<...>` list. | | Typed tests | Type-parameterized tests | |---|---|---| | Type list | fixed with the suite | supplied at instantiation | | Macros | `TYPED_TEST_SUITE`, `TYPED_TEST` | `TYPED_TEST_SUITE_P`, `TYPED_TEST_P`, `REGISTER_TYPED_TEST_SUITE_P`, `INSTANTIATE_TYPED_TEST_SUITE_P` | | Typical use | one library testing its own types | a conformance suite others run on their types | | Extra step | none | registration listing every pattern | ## Why this-> is required Each `TYPED_TEST` body becomes a member function of a class template that derives from `TypedKvTest<TypeParam>`. That base class depends on a template parameter, which makes it a **dependent base class**. C++ unqualified name lookup does not search dependent base classes, so `store_` alone fails; clang reports `use of undeclared identifier 'store_'`. Three spellings fix it: - `this->store_` for non-static members and member functions, making the name dependent so lookup is deferred until instantiation; - `TestFixture::kName` for static members, where `TestFixture` is GoogleTest's alias for the fixture specialisation (the qualified form also reaches non-static members, but `this->` is the usual spelling); - `typename TestFixture::Store` for nested types, where `typename` tells the compiler the dependent name is a type. `TypeParam` itself is provided by the macro and is always usable directly. The underlying lookup rules belong to the material on templates; the GoogleTest-specific part is knowing that every typed body lives in such a template. ## Reading the output Each type produces its own test suite. A typed suite over three types yields `TypedKvTest/0`, `TypedKvTest/1` and `TypedKvTest/2`, and the header line of each names the type, for example `1 test from TypedKvTest/1, where TypeParam = double`. A type-parameterized instantiation adds its prefix, as in `Scalar/ValueConformanceTest/0`. These are the names a filter matches, so `TypedKvTest/1.*` runs only the `double` case. Long type names print in full, which is why a `std::string` parameter shows up as its whole `std::basic_string<...>` spelling. ## Typed or value-parameterized? - Choose typed tests when behaviour varies by **type**: element types, allocator types, policy classes. - Choose value-parameterized tests when one type varies by **value**: backends selected at run time, sizes, flags. - Combine them sparingly; a typed suite whose body also loops over many values grows quickly and hides which case failed. ## Common mistakes - Writing the type list inline in the macro, which splits on its commas. - Forgetting `this->` and reading the error as a missing member. - Adding a `TYPED_TEST_P` pattern but not listing it in `REGISTER_TYPED_TEST_SUITE_P`; in GoogleTest 1.18 the test binary aborts at start-up with `You forgot to list test Two.`

  • Why does TYPED_TEST_SUITE(Fixture, testing::Types<int, double>) fail to compile?
    The preprocessor knows nothing about templates and splits macro arguments at every comma not inside parentheses, so the macro receives three arguments instead of two. Naming the list with a `using` alias first, as the GoogleTest guide requires, passes it as one token.
  • What does REGISTER_TYPED_TEST_SUITE_P add that typed tests do not need?
    A type-parameterized suite is defined before any type is known, so GoogleTest needs an explicit list of its patterns to instantiate later. `REGISTER_TYPED_TEST_SUITE_P(Fixture, Test1, Test2)` provides that list, and each later `INSTANTIATE_TYPED_TEST_SUITE_P` creates every registered pattern for every type.

  • TYPED_TEST bodies can read fixture members unqualified, like TEST_F bodies.
  • Typed tests and type-parameterized tests are two names for the same feature.
  • The Types<...> list can be written inline in TYPED_TEST_SUITE.
  • TypeParam has to be declared by the test author in the fixture.
  • Type-parameterized patterns need no registration before instantiation.

Assumes GoogleTest 1.18 for the quoted start-up abort when a TYPED_TEST_P pattern is left out of REGISTER_TYPED_TEST_SUITE_P.