如何使用Google测试进行C 通过数据组合运行
How to use google test for C++ to run through combinations of data
我有一个单位测试,我需要以200个可能的数据组合进行运行。(生产实现具有要在配置文件中测试的数据。我知道如何模拟这些值)。我更喜欢为每种组合编写单独的测试用例,并使用某种方式通过数据进行循环。使用C ?
您可以使用GTEST的价值参数测试。
与Combine(g1, g2, ..., gN)
发电机一起使用它就像您最好的选择。
以下示例填充了2个 vector
s,一个 int
s之一,另一个string
s,然后仅使用一个测试固定装置,为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();
}
使用一个结构数组(称为Combination
)保存您的测试数据,并在单个测试中循环每个条目。使用EXPECT_EQ
而不是ASSERT_EQ
检查每个组合,以免测试中止,您可以继续检查其他组合。
Combination
的Overload operator<<
,以便您可以将其输出到ostream
:
ostream& operator<<(ostream& os, const Combination& combo)
{
os << "(" << combo.field1 << ", " << combo.field2 << ")";
return os;
}
Combination
的Overload operator==
,以便您可以轻松地比较两个组合的平等:
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";
}
相关文章:
- 如何遍历几个每小时一次的根(.root)文件,并将它们组合成更大的每日数据.root文件?
- 选择和 UDP 协议组合时无法接收数据
- TCP 数据包有时会被组合(并延迟?
- 组合来自多个线程的数据的标准方法?
- 多种搜索组合的数据结构设计
- 数据的所有组合都是产品的两个整数
- 在组合数据时不可能是恒定正确的,并且它是锁定的?
- 如何使用Google测试进行C 通过数据组合运行
- 如何使用某些MPI命令(或组合命令)之间在两个处理器之间交换2D数组中存储的数据
- 在不更改数据类型的情况下可逆地组合两个uint32_t
- C++组合2个数组,数据有序
- 使用在组合类中计算的数据构造成员类
- STL容器,用于在C++中组合多个非常大的光栅数据文件
- 组合框中文本文件中的数据
- 用于在C++中填充数据的组合
- 我应该如何使用 std::map<> 来聚合/组合存储在数组C++中的数据?
- 组合常量和非常量引用数据成员的单个类
- 对于巨大的数据集,从m个数组的元素组合中提取n个最低和
- 将不同的数据类型组合成一个void类型
- 具有固定字符串列表和自定义数据的WinAPI组合框