UPDATED: I am learning about GoogleTest to test C/C++ programs. I heard GoogleTest is a good tool for it. I tried one simple thing, which is to read input from a file, during a TEST() function, and it did not seem to work:
test.cpp
in the google test project:
#include <iostream>
#include <fstream>
#include <filesystem>
TEST(IOTest, ReadFile) {
std::string filename = "input.txt";
std::string buffer;
std::ifstream ifs(filename, std::ios::binary);
std::filesystem::path cwd = std::filesystem::current_path();
std::cout << "Current directory path: " << cwd << std::endl;
std::cout << "Current file path: " << __FILE__ << std::endl;
ASSERT_TRUE(ifs.is_open());
}
The resulting test failed. And the output is:
Current directory path: "C:\\Users\\Tong\\source\\repos\\BookLibrary\\Debug"
Current file path: C:\Users\Tong\source\repos\BookLibrary\GoogleTest\test.cpp
Value of: ifs.is_open()
Actual: false
Expected: true
I placed the file "input.txt" under the current directory, which is:
C:\Users\Tong\source\repos\BookLibrary\GoogleTest\Debug\input.txt
but it seems that the program cannot find or open it.
Am I missing something here? I also tried
std::string filename = "./input.txt";
but still no luck in reading the file.
UPDATE: problem solved; everything was correct except that I had placed the input.txt
file in the wrong folder. It should be placed in
C:\Users\Tong\source\repos\BookLibrary\Debug
and not
C:\Users\Tong\source\repos\BookLibrary\GoogleTest\Debug