Welcome to the next pikoTutorial!
Parameterized unit tests are priceless. They help to test code thoroughly through multiple possible input values, without having to write multiple and almost the same unit tests – if you have a function accepting an enum and output for some of them is the same, you just parameterize the test case with some enum values and write a single test for it.
However, there are situations in which that’s not enough. Imagine for example that your function takes 2 enums as input and only in case one specific combination of their values it is supposed to return true, otherwise false.
enum class Type1
{
kValue1,
kValue2,
kValue3
};
enum class Type2
{
kOtherValue1,
kOtherValue2,
kOtherValue3
};
bool IsCorrectCombination(const Type1 value_1, const Type2 value_2)
{
return value_1 == Type1::kValue1 && value_2 == Type2::kOtherValue1;
}In such case you have 9 combinations of input values, so in order to test this function fully you need 1 test case for true and 8 test cases for false. The true case is an easy one-liner:
TEST(MyTests, IsCorrectCombinationReturnsTrue)
{
EXPECT_TRUE(IsCorrectCombination(Type1::kValue1, Type2::kOtherValue1));
}However, no one wants to write test cases for all the remaining 8 combinations by hand, but fortunately GTest gives us a mechanism dedicated for testing parameters’ combinations. First, you need to define a parameterized fixture for the types you want to test:
using namespace ::testing;
class AllCombinationsParametrizedFixture : public Test,
public WithParamInterface<std::tuple<Type1, Type2>>
{};Then you must instantiate the test with a list of specific values which will be passed to your test cases:
INSTANTIATE_TEST_SUITE_P(AllCombinations,
AllCombinationsParametrizedFixture,
Combine(Values(Type1::kValue1,
Type1::kValue2,
Type1::kValue3),
Values(Type2::kOtherValue1,
Type2::kOtherValue2,
Type2::kOtherValue3)));The last step is to write a test case where you can access specific values of the combination by using std::get to extract value from the tuple returned by GetParam():
TEST_P(AllCombinationsParametrizedFixture, IsCorrectCombinationReturnsFalse)
{
const Type1 first_value = std::get<0>(GetParam());
const Type2 second_value = std::get<1>(GetParam());
if (first_value == Type1::kValue1 && second_value == Type2::kOtherValue1) {
GTEST_SKIP() << "Skipping valid combination";
}
EXPECT_FALSE(IsCorrectCombination(first_value, second_value));
}This allows us to have only 2 test cases in the code, but when you execute the above tests you’ll see that actually all the required 9 test cases have been executed. If you don’t want to explicitly return in the test body to omit the valid combination, you may write your custom function generating combinations and provide it during test instantiation:
using CombinationType = std::tuple<Type1, Type2>;
class AllCombinationsParametrizedFixture : public Test, public WithParamInterface<CombinationType> {};
std::vector<CombinationType> GenerateCombinations() {
std::vector<CombinationType> combinations;
for (const auto &value_1 : {Type1::kValue1, Type1::kValue2, Type1::kValue3}) {
for (const auto &value_2 : {Type2::kOtherValue1, Type2::kOtherValue2, Type2::kOtherValue3}) {
combinations.emplace_back(std::make_tuple(value_1, value_2));
}
}
std::erase(combinations, std::make_tuple(Type1::kValue1, Type2::kOtherValue1));
return combinations;
}
INSTANTIATE_TEST_SUITE_P(AllCombinations,
AllCombinationsParametrizedFixture,
testing::ValuesIn(GenerateCombinations()));Note for beginners: Removing elements by using
std::erasehas been added to C++ basing on consistent container erasure proposal and is available since C++20. If you use an older version of C++, you must use erase-remove idiom instead.
After that modification, the test logs should still cover all 9 parameters’ combinations:
1: [==========] Running 9 tests from 2 test suites.
1: [----------] Global test environment set-up.
1: [----------] 1 test from MyTests
1: [ RUN ] MyTests.IsCorrectCombinationReturnsTrue
1: [ OK ] MyTests.IsCorrectCombinationReturnsTrue (0 ms)
1: [----------] 1 test from MyTests (0 ms total)
1:
1: [----------] 8 tests from AllCombinations/AllCombinationsParametrizedFixture
1: [ RUN ] AllCombinations/AllCombinationsParametrizedFixture.IsCorrectCombinationReturnsFalse/0
1: [ OK ] AllCombinations/AllCombinationsParametrizedFixture.IsCorrectCombinationReturnsFalse/0 (0 ms)
1: [ RUN ] AllCombinations/AllCombinationsParametrizedFixture.IsCorrectCombinationReturnsFalse/1
1: [ OK ] AllCombinations/AllCombinationsParametrizedFixture.IsCorrectCombinationReturnsFalse/1 (0 ms)
1: [ RUN ] AllCombinations/AllCombinationsParametrizedFixture.IsCorrectCombinationReturnsFalse/2
1: [ OK ] AllCombinations/AllCombinationsParametrizedFixture.IsCorrectCombinationReturnsFalse/2 (0 ms)
1: [ RUN ] AllCombinations/AllCombinationsParametrizedFixture.IsCorrectCombinationReturnsFalse/3
1: [ OK ] AllCombinations/AllCombinationsParametrizedFixture.IsCorrectCombinationReturnsFalse/3 (0 ms)
1: [ RUN ] AllCombinations/AllCombinationsParametrizedFixture.IsCorrectCombinationReturnsFalse/4
1: [ OK ] AllCombinations/AllCombinationsParametrizedFixture.IsCorrectCombinationReturnsFalse/4 (0 ms)
1: [ RUN ] AllCombinations/AllCombinationsParametrizedFixture.IsCorrectCombinationReturnsFalse/5
1: [ OK ] AllCombinations/AllCombinationsParametrizedFixture.IsCorrectCombinationReturnsFalse/5 (0 ms)
1: [ RUN ] AllCombinations/AllCombinationsParametrizedFixture.IsCorrectCombinationReturnsFalse/6
1: [ OK ] AllCombinations/AllCombinationsParametrizedFixture.IsCorrectCombinationReturnsFalse/6 (0 ms)
1: [ RUN ] AllCombinations/AllCombinationsParametrizedFixture.IsCorrectCombinationReturnsFalse/7
1: [ OK ] AllCombinations/AllCombinationsParametrizedFixture.IsCorrectCombinationReturnsFalse/7 (0 ms)
1: [----------] 8 tests from AllCombinations/AllCombinationsParametrizedFixture (0 ms total)
1:
1: [----------] Global test environment tear-down
1: [==========] 9 tests from 2 test suites ran. (0 ms total)
1: [ PASSED ] 9 tests.Read also:
- Storing your ATTiny program in…EEPROM?
- Bug of the week #12
- Sharing variable between bash scripts
- A 40-line LLM-based bash command executor in Python
- GTest and short-circuit evaluation in C++
- AI is powerful. Snippets are instant.
- From AUTOSAR to S-Core: the first C++ pub/sub implementation
- How to write Arduino Uno code with Python?
- Combining Bazel with Docker
- Running commands with timeout on Linux
- Running Python unit tests with CMake
- Thirdparty dependencies with FetchContent
- Bug of the week #11
- Combining CMake with Docker
- How to search the internet from Linux terminal?
- Folding expressions in C++
- How to derive from an enum in Python?
- Bug of the week #10
- Trying ROS2: client/server within a single container
- Make C++ a better place #4: Go as an alternative
- How to convert hex to dec in Linux terminal?
- Setting up a Python project with CMake
- Separating builds for different configs with Bazel
- Trying ROS2: pub/sub within a single container
- Bug of the week #9
- UDP multicasting with Python
- Destruction order vs thread safety in C++
- Let’s review some code: C++ #2
- Make C++ a better place #3: D as an alternative
- Registering callback using std::function in C++
- Bug of the week #8
- TCP client/server with Python
- Simple menus in Bash scripts with select
- Calling member function on a nullptr in C++
- Bug of the week #7
- Python lru_cache explained
- How to dockerize a Python application?
- Make C++ a better place #2: CppFront as an alternative
- Parameters combinations in GoogleTest
- Data transfer with curl
- Python reduce explained
- Bug of the week #6
- Custom literals in C++
- Linux and hash command
- 5 Python good practices which make life easier
- Let’s review some code: Python #1
- Make C++ a better place #1: What does better mean
- Enums vs enum class in C++
- Bug of the week #5
- UDP client/server with Python
- Hard links in Linux
- Functions calling order in unit tests in C++
- Bug of the week #4
- Yield in Python – state machines, coroutines and more
- Copy files from another branch with Git
- Make C++ a better place #0: Introduction
- 5 misconceptions about std::move in C++
- How to use xargs on Linux?
- How to test method call order with unittest in Python?
- Bug of the week #3









