0

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 
19
  • 3
    Edit your question with a minimal reproducible example. In particular we'd need your main. Commented Jan 7, 2023 at 1:55
  • You need to use test macros like ASSERT_* and EXPECT_* to have test conditions evaluated by Google Test. If you do not use them the tests will pass unless an exception is thrown regardless of any error conditions generated by the code you are testing. You should read through more of the documentation starting the primer. For instance ASSERT_TRUE(ifs) or ASSERT_TRUE(ifs.is_open()) Commented Jan 7, 2023 at 2:00
  • 1
    You should learn about what current working directory means. When you open a file without specifying its location (directory), like "input.txt", the current working directory is assumed to be the location of that file. You seem to believe that "under the project folder of my google test project" is your current working directory, but your test result implies that it is not. Commented Jan 7, 2023 at 2:21
  • 1
    just to test it out if the file "input.txt"; is found by the code OR not; just specify the full file path in the variable 'filename'. example: std::string filename = "C:\\Users\\Tong\\source\\repos\\BookLibrary\\GoogleTest\\input.txt"; (use the double slash for escaping) Commented Jan 7, 2023 at 2:33
  • 1
    @TongZhao you can find an answer here. Commented Jan 7, 2023 at 2:46

1 Answer 1

0

Thanks to multiple comments in the original question, I found the root cause of my problem: I did not find my current directory path correctly.

I thought the current directory path was the project directory of the Google Test project. But it was not true. My current directory path was the other project that hosts my main() function. After knowing this, I placed the file to be read from into the correct current directory path, and the code worked.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.