gpt4 book ai didi

c++ - 我可以将 vector 与值参数化的 googletest 一起使用吗

转载 作者:行者123 更新时间:2023-11-30 05:28:37 24 4
gpt4 key购买 nike

我想参数化一个目录,意思是目录中的每个文件the directory structure with two empty json and two empty cfg files应该由参数化的 TEST_P 处理。这可能吗?

不幸的是我得到了一个错误:

g++ -std=c++11 -g -L/opt/gtest/lib -lgtest -lgtest_main -lpthread -I./ -I../../src -I/opt/gtest/include -o test2  parametrized2.cpp
parametrized2.cpp: In function 'testing::internal::ParamGenerator<std::vector<std::basic_string<char> > > gtest_ParametrizedGTestInstanceParametrizedGTest_EvalGenerator_()':
parametrized2.cpp:57:5: error: could not convert 'testing::ValuesIn(const Container&) [with Container = std::vector<std::basic_string<char> >; typename Container::value_type = std::basic_string<char>]()' from 'testing::internal::ParamGenerator<std::basic_string<char> >' to 'testing::internal::ParamGenerator<std::vector<std::basic_string<char> > >'
make: ***

我的错误是什么?

#include <iostream>
#include <string>
#include <vector>
#include <gtest/gtest.h>
#include <dirent.h>

using namespace std;

std::vector<std::string> jsonCfgFiles;

//opening any folder and saving all file-names in a vector<string>
std::vector<string> openDir(string path)
{
DIR* dir;
dirent* pdir;
vector<string> files;
dir = opendir(path.c_str());
while (pdir = readdir(dir)) {
files.push_back(pdir->d_name);
}
return files;
}

vector<string> GetJsonCofigFiles(void)
{
vector<string> f;
std::vector<std::string> jsonCfgFilesLocal;
string buffer = "";
f = openDir("oem"); // pass which dir to open

// collect only json files
for (auto i = f.begin(); i != f.end(); ++i) {
if ((*i).find(".json") != std::string::npos) {
buffer = "" + (*i);
jsonCfgFiles.push_back(buffer);
}
}
return jsonCfgFilesLocal;
}
// Using just string compiles well. Why is vector<string> not possible?
class ParametrizedGTest : public testing::TestWithParam<vector<string> > {
public:
ParametrizedGTest();
virtual ~ParametrizedGTest();
};

ParametrizedGTest::ParametrizedGTest()
{
}

ParametrizedGTest::~ParametrizedGTest()
{
}

TEST_P(ParametrizedGTest, testParameter)
{
cout << (*(GetParam().begin())) << "," << endl;
}

INSTANTIATE_TEST_CASE_P(ParametrizedGTestInstance,
ParametrizedGTest,
::testing::ValuesIn(jsonCfgFiles));

int main(int argc, char* argv[])
{
jsonCfgFiles = GetJsonCofigFiles();
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

感谢 Marko Popovic 现在我解决了这个问题:

#include <iostream>
#include <string>
#include <vector>
#include <gtest/gtest.h>
#include <dirent.h>

using namespace std;

std::vector<std::string> jsonCfgFiles;

std::vector<string> open1(string path) //opening any folder and saving all file-names in a vector<string>
{
DIR* dir;
dirent* pdir;
vector<string> files;
dir = opendir(path.c_str());
while (pdir = readdir(dir)) {
files.push_back(pdir->d_name);
}
return files;
}

std::vector<string> GetJsonCofigFiles(void)
{
vector<string> f;
string buffer = "";
std::vector<std::string> jsonCfgFiles;
f = open1("oem"); // pass which dir to open
for (auto i = f.begin(); i != f.end(); ++i) {
if ((*i).find(".json") != std::string::npos) {
buffer = "oem/" + (*i);
jsonCfgFiles.push_back(buffer);
}
}
return jsonCfgFiles;
}

class ParametrizedGTest : public testing::TestWithParam<string> {
public:
ParametrizedGTest();
virtual ~ParametrizedGTest();
};

ParametrizedGTest::ParametrizedGTest()
{
}

ParametrizedGTest::~ParametrizedGTest()
{
}

TEST_P(ParametrizedGTest, testParameter)
{
cout << GetParam() << endl;
}

INSTANTIATE_TEST_CASE_P(ParametrizedGTestInstance,
ParametrizedGTest,
::testing::ValuesIn(jsonCfgFiles));

int main(int argc, char* argv[])
{
jsonCfgFiles = GetJsonCofigFiles();
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

编译运行时

[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from ParametrizedGTestInstance/ParametrizedGTest
[ RUN ] ParametrizedGTestInstance/ParametrizedGTest.testParameter/0
oem/_PVO111k_.json
[ OK ] ParametrizedGTestInstance/ParametrizedGTest.testParameter/0 (0 ms)
[ RUN ] ParametrizedGTestInstance/ParametrizedGTest.testParameter/1
oem/_PVO112k.json
[ OK ] ParametrizedGTestInstance/ParametrizedGTest.testParameter/1 (0 ms)
[----------] 2 tests from ParametrizedGTestInstance/ParametrizedGTest (0 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (1 ms total)
[ PASSED ] 2 tests.

最佳答案

问题是你给类 testing::TestWithParam 的模板参数.文档指出模板参数“T 是您的参数值的类型”。由于您正在尝试创建由 json 文件的路径参数化的测试,因此您需要的类型是 std::string , 不是 std::vector<std::string> .换行

class ParametrizedGTest : public testing::TestWithParam<vector<string> > {

class ParametrizedGTest : public testing::TestWithParam<string> {

代码将编译。

关于c++ - 我可以将 vector<string> 与值参数化的 googletest 一起使用吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36743068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com