GTest将std::vector作为参数源



我有一个函数,可以读取选定目录中的所有文件。

std::vector<std::string> getAllFilesInDirectory(const std::string_view strDirectory);

现在我想对向量的每个元素进行测试。我的测试装备很直接。

class myTestfixture: public ::testing::TestWithParam<std::string> 
{
public:
myTestfixture();
~myTestfixture() override;
};

现在我想把向量的每个元素都传递给我的测试。我知道我可以将单个显式值传递给:testing::values,但传递stl容器是行不通的。

INSTANTIATE_TEST_CASE_P(
myTest,
myTestfixture,
::testing::Values(
getAllFilesInDirectory("myDir")
));
TEST_P(myTestfixture, ValidTest)
{
//test something
}

是否可以将容器作为参数源传递给gtest?

您几乎已经完成了,但有一个小错误。尝试使用ValuesIn()而不是Values()

INSTANTIATE_TEST_CASE_P(
myTest,
myTestfixture,
::testing::ValuesIn(
getAllFilesInDirectory("myDir")
));

最新更新