如何使用Google Test for C ++通过数据组合运行 [英] How to use google test for C++ to run through combinations of data
问题描述
我有一个单元测试,我需要运行200种可能的数据组合. (生产实现在配置文件中具有要测试的数据.我知道如何模拟这些值).我更喜欢为每种组合编写单独的测试用例,并使用某种方式遍历数据.是否有一些直接的方法使用Google Test for C ++?
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++?
谢谢, 卡尔提克
推荐答案
You can make use of gtest's Value-parameterized tests for this.
将其与Combine(g1, g2, ..., gN)
生成器一起使用听起来像是最好的选择.
Using this in conjunction with the Combine(g1, g2, ..., gN)
generator sounds like your best bet.
以下示例填充2个vector
,一个int
中的一个,另一个string
s,然后仅使用一个测试夹具,为2个vector
中可用值的每种组合创建测试. s:
The following example populates 2 vector
s, one of int
s and the other of string
s, then with just a single test fixture, creates tests for every combination of available values in the 2 vector
s:
#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();
}
这篇关于如何使用Google Test for C ++通过数据组合运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!