Google C++ 单元测试:如何编写持久数据文件?
Google unit testing in C++: how to write a persistent data file?
问题:
1) C++ Google 单元测试创建的文件去哪里了?
2) 有没有办法在 C++ Google 单元测试中编写持久数据文件,以便在测试运行后可以访问该文件?
代码和期望的行为
我 运行 在 Ubuntu 14.04 上与 catkin_make 进行单元测试。我希望代码在某个地方写一个文件,以便在测试运行后可以找到它。下面的代码写了一个文件,但我不知道它去了哪里,或者它是否在单元测试完成后仍然存在。
TEST(GUnitTestFileIo, Test_One)
{
std::ofstream csvFile;
csvFile.open("helloWorldTestFile.csv");
if (csvFile.is_open()) {
csvFile << "Hello, World, !" << std::endl;
csvFile.close();
} else {
std::cout << "Failed to open the file!" << std::endl;
}
}
int main(int argc, char **argv){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
一种解决方案是简单地写入绝对文件路径。以下代码从 google 单元测试内部将文件写入用户的主目录:
TEST(GUnitTestFileIo, Test_One)
{
char const* tmp = getenv( "HOME" );
if ( tmp == NULL ) {
std::cout << "$(HOME) environment variable is not defined!";
} else {
std::string home( tmp ); // string for the home directory
std::ofstream csvFile; // write the file
csvFile.open(home + "/helloWorldTestFile.csv");
if (csvFile.is_open()) {
csvFile << "Hello, World, !" << std::endl;
csvFile.close();
} else {
std::cout << "Failed to open the file!" << std::endl;
}
}
}
// Run all the tests that were declared with TEST()
int main(int argc, char **argv){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
问题:
1) C++ Google 单元测试创建的文件去哪里了?
2) 有没有办法在 C++ Google 单元测试中编写持久数据文件,以便在测试运行后可以访问该文件?
代码和期望的行为
我 运行 在 Ubuntu 14.04 上与 catkin_make 进行单元测试。我希望代码在某个地方写一个文件,以便在测试运行后可以找到它。下面的代码写了一个文件,但我不知道它去了哪里,或者它是否在单元测试完成后仍然存在。
TEST(GUnitTestFileIo, Test_One)
{
std::ofstream csvFile;
csvFile.open("helloWorldTestFile.csv");
if (csvFile.is_open()) {
csvFile << "Hello, World, !" << std::endl;
csvFile.close();
} else {
std::cout << "Failed to open the file!" << std::endl;
}
}
int main(int argc, char **argv){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
一种解决方案是简单地写入绝对文件路径。以下代码从 google 单元测试内部将文件写入用户的主目录:
TEST(GUnitTestFileIo, Test_One)
{
char const* tmp = getenv( "HOME" );
if ( tmp == NULL ) {
std::cout << "$(HOME) environment variable is not defined!";
} else {
std::string home( tmp ); // string for the home directory
std::ofstream csvFile; // write the file
csvFile.open(home + "/helloWorldTestFile.csv");
if (csvFile.is_open()) {
csvFile << "Hello, World, !" << std::endl;
csvFile.close();
} else {
std::cout << "Failed to open the file!" << std::endl;
}
}
}
// Run all the tests that were declared with TEST()
int main(int argc, char **argv){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}