2016-04-20 10 views
1

我想参数化一个目录,意味着来自目录the directory structure with two empty json and two empty cfg files的每个文件应该由参数化的TEST_P处理。这可能吗?我可以使用具有值参数化的矢量<string> googletest

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

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. 

回答

1

的问题是,你是给到testing::TestWithParam类模板参数。该文档指出模板参数“T是您的参数值的类型”。由于您正尝试创建通过路径参数化为json文件的测试,因此您的案例类型为std::string,而不是std::vector<std::string>。改线

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

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

和代码进行编译。

+0

非常感谢。是的,它编译与字符串很好。不幸的是,当我运行这个时,没有测试用例会被执行:“从0个测试用例中运行0个测试” – jejej

+0

@jejej测试在编译期间被实例化。这意味着你不能在程序执行期间加载文件路径,但你需要创建一个将被使用的虚拟路径列表。试试像这样:'std :: vector jsonCfgFiles {“path1”,“path2”};'。无论如何,你都不应该编写依赖于当前环境状态的单元测试。单元测试应该完全与环境隔离。 –

+0

测试需要读取json文件的内容。只有在这里,json文件是空的虚拟文件。不幸的是,没有其他办法。 – jejej

相关问题