Questions:
1) Where do files go that are created by a C++ Google unit test?
2) Is there a way to write a persistent data file in a C++ Google unit test, such that the file is accessible after the test runs?
Code and desired behavior
I'm running the unit test on Ubuntu 14.04 with catkin_make. I would like the code to write a file somewhere that I can find it after the test runs. The following code writes a file, but I don't know where it goes, or if it persists after the unit tests complete.
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();
}
One solution is to simply write to an absolute file path. The following code writes a file to the user's home directory from inside of a google unit test:
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();
}