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 parametrized 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 instantation:
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::erase
has 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.