我可以使用<string>带有值参数化谷歌测试的向量吗
can I use a vector<string> with value-parametrized googletest
我想参数化一个目录,意味着目录中的每个文件,具有两个空 json 和两个空 cfg 文件的目录结构应由参数化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();
}
感谢马尔科·波波维奇,现在我解决了这个问题:
#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> {
代码将编译。
相关文章:
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 谷歌测试中的期望值存储在哪里
- 谷歌模拟和覆盖关键字
- 如何使用ESP8266向谷歌主页发送命令
- 更正 CMakeList.txt 用于谷歌测试的文件?
- 如何检查谷歌模拟中作为空指针传递的字符串参数
- 使用谷歌模拟测试C++时无法推断模板参数
- 谷歌测试参数化的高级使用
- 引用谷歌测试/模拟框架中的数组参数
- 谷歌测试框架允许未定义的参数进行测试
- 我可以使用<string>带有值参数化谷歌测试的向量吗
- 谷歌C++风格指南.为什么要先输入后输出参数排序
- 谷歌模拟:在EXPECT_CALL中设置参数
- 谷歌测试框架参数化夹具
- 谷歌模拟成员函数中的std::pair参数编译失败
- 谷歌关于输入/输出参数作为指针的风格指南
- 我可以将参数传递给谷歌测试功能吗
- C++谷歌模拟SaveArg:如何保存指针参数
- 在谷歌测试或cppunit下移动参数化测试
- 谷歌模拟,使用另一个参数的参数匹配器