200の可能なデータの組み合わせに対して実行する必要がある単体テストがあります。(本番実装には、構成ファイルでテストするデータがあります。これらの値をモックする方法を知っています)。私は、組み合わせごとに個別のテストケースを作成し、データをループする何らかの方法を使用することを好みます。C ++用のGoogleテストを使用するそのような直接的な方法はありますか?
ありがとう、カーシック
200の可能なデータの組み合わせに対して実行する必要がある単体テストがあります。(本番実装には、構成ファイルでテストするデータがあります。これらの値をモックする方法を知っています)。私は、組み合わせごとに個別のテストケースを作成し、データをループする何らかの方法を使用することを好みます。C ++用のGoogleテストを使用するそのような直接的な方法はありますか?
ありがとう、カーシック
これには、gtest の値パラメータ化テストを利用できます。
これをジェネレーターと組み合わせて使用Combine(g1, g2, ..., gN)
すると、最善の策のように聞こえます。
次の例では、 の 1 つと のもう 1 つの 2 つの にデータを入力vector
し、1 つのテスト フィクスチャだけを使用して、2 つint
のsstring
で使用可能な値のすべての組み合わせに対してテストを作成します。vector
#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();
}
構造体の配列 (たとえば、 と呼ばれるCombination
) を使用してテスト データを保持し、1 つのテストで各エントリをループします。テストが中止されず、他の組み合わせのチェックを続行できるように、EXPECT_EQ
代わりに を使用して各組み合わせをチェックします。ASSERT_EQ
に出力できるようoperator<<
に a をオーバーロードします。Combination
ostream
ostream& operator<<(ostream& os, const Combination& combo)
{
os << "(" << combo.field1 << ", " << combo.field2 << ")";
return os;
}
operator==
a をオーバーロードして、 Combination
2 つの組み合わせが等しいかどうかを簡単に比較できるようにします。
bool operator==(const Combination& c1, const Combination& c2)
{
return (c1.field1 == c2.field1) && (c1.field2 == c2.field2);
}
単体テストは次のようになります。
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";
}