Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use google test for C++ to run through combinations of data

Tags:

c++

googletest

I have a unit test that I need to run for 200 possible combinations of data. (The production implementation has the data to be tested in configuration files. I know how to mock these values). I prefer nit writing separate test case for each combination and to use some way of looping through the data. Is there some such direct way using Google test for C++?

Thanks, Karthick

like image 718
Karthick S Avatar asked Nov 03 '12 20:11

Karthick S


People also ask

Does Google test support C?

GoogleTest requires a codebase and compiler compliant with the C++11 standard or newer.

Does Google test run tests in parallel?

gtest-parallel is a script that executes Google Test binaries in parallel, providing good speedup for single-threaded tests (on multi-core machines) and tests that do not run at 100% CPU (on single- or multi-core machines).

What is typed_ test?

TYPED_TEST(TestSuiteName, TestName) { ... statements ... } Defines an individual typed test named TestName in the typed test suite TestSuiteName . The test suite must be defined with TYPED_TEST_SUITE .

How does Google test work?

Independent and Repeatable: Googletest isolates the tests by running each of them on a different object. Portable and Reusable: Googletest works on different Oses (Linux, Windows, or a Mac), with different compilers. When tests fail, it should provide as much information about the problem as possible.


2 Answers

You can make use of gtest's Value-parameterized tests for this.

Using this in conjunction with the Combine(g1, g2, ..., gN) generator sounds like your best bet.

The following example populates 2 vectors, one of ints and the other of strings, then with just a single test fixture, creates tests for every combination of available values in the 2 vectors:

#include <iostream>
#include <string>
#include <tuple>
#include <vector>
#include "gtest/gtest.h"

std::vector<int> ints;
std::vector<std::string> strings;

class CombinationsTest :
    public ::testing::TestWithParam<std::tuple<int, std::string>> {};

TEST_P(CombinationsTest, Basic) {
  std::cout << "int: "        << std::get<0>(GetParam())
            << "  string: \"" << std::get<1>(GetParam())
            << "\"\n";
}

INSTANTIATE_TEST_CASE_P(AllCombinations,
                        CombinationsTest,
                        ::testing::Combine(::testing::ValuesIn(ints),
                                           ::testing::ValuesIn(strings)));

int main(int argc, char **argv) {
  for (int i = 0; i < 10; ++i) {
    ints.push_back(i * 100);
    strings.push_back(std::string("String ") + static_cast<char>(i + 65));
  }
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
like image 164
Fraser Avatar answered Oct 03 '22 20:10

Fraser


Use an array of structs (called, say, Combination) to hold your test data, and loop though each entry in a single test. Check each combination using EXPECT_EQ instead of ASSERT_EQ so that the test isn't aborted and you can continue checking other combinations.

Overload operator<< for a Combination so that you can output it to an ostream:

ostream& operator<<(ostream& os, const Combination& combo)
{
    os << "(" << combo.field1 << ", " << combo.field2 << ")";
    return os;
}

Overload operator== for a Combination so that you can easily compare two combinations for equality:

bool operator==(const Combination& c1, const Combination& c2)
{
    return (c1.field1 == c2.field1) && (c1.field2 == c2.field2);
}

And the unit test could look something like this:

TEST(myTestCase, myTestName)
{
    int failureCount = 0;
    for (each index i in expectedComboTable)
    {
        Combination expected = expectedComboTable[i];
        Combination actual = generateCombination(i);
        EXPECT_EQ(expected, actual);
        failureCount += (expected == actual) ? 0 : 1;
    }
    ASSERT_EQ(0, failureCount) << "some combinations failed";
}
like image 34
Emile Cormier Avatar answered Oct 03 '22 22:10

Emile Cormier