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
Tong Zhao
  • 91
  • 7
  • 3
    [Edit] your question with a [mcve]. In particular we'd need your `main`. – Stephen Newell Jan 07 '23 at 01: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](https://google.github.io/googletest/) starting the [primer](https://google.github.io/googletest/primer.html). For instance `ASSERT_TRUE(ifs)` or `ASSERT_TRUE(ifs.is_open())` – Captain Obvlious Jan 07 '23 at 02:00
  • @StephenNewell You do not need `main` in an application linking Google Test and the code provided is a complete and sufficient example. From the documentation - *"Most users should not need to write their own `main` function and instead link with `gtest_main` (as opposed to with `gtest`), which defines a suitable entry point. "* – Captain Obvlious Jan 07 '23 at 02:07
  • @CaptainObvlious - You need a `main`, although that's usually provided by the `gtest_main` library. I've seen multiple people write their own `main`s for some reason though, hence why I don't believe this is a complete example. – Stephen Newell Jan 07 '23 at 02:10
  • @StephenNewell Seems a bit nitpicky to expect them to include boiler plate code that has no real value and you generally do not need just because you see some people do it. – Captain Obvlious Jan 07 '23 at 02:15
  • @CaptainObvlious thank you for your suggestion. I read the Primer, and updated my code. This time, gtest did point out that reading file failed. Now, I wonder why reading file failed. I placed my file inside the google test project folder, somehow it is not visible to my `test.cpp` under the google test project (which is automatically created when I create this google test project in Visual Studio 2019). See my updated code above. Thanks. – Tong Zhao Jan 07 '23 at 02:19
  • 1
    You should learn about what [current working directory](https://stackoverflow.com/questions/65670500/how-to-get-current-working-directory-in-cpp) 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. – Drew Dormann Jan 07 '23 at 02:21
  • @StephenNewell my original program was too long (~300 lines), so I would rather not drop it here. I think as long as my google test project is linked to and references my own code (which sits inside another static library project), the tool should work. Say, if in my static library project I just have an addition function definition. Then I can test it inside the google test project without having a main file. I do want to point out, that I have a main file in another console project, which simply prints out "Hello World!". I guess this is the main file you were looking for? – Tong Zhao Jan 07 '23 at 02:23
  • @DrewDormann thank you for mentioning the more correct technical term. I tried printing `__FILE__` value inside my TEST() function, and it indeed was pointing to the google test project folder.. – Tong Zhao Jan 07 '23 at 02:24
  • @TongZhao - Typically you'd build your tests and link them as a separate program. If you're using `gtest_main` (which is the sane way), then that provides the `main` for your test executable. – Stephen Newell Jan 07 '23 at 02:24
  • 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) – dorKKnight Jan 07 '23 at 02:33
  • @TongZhao `__FILE__` **does not** indicate the **current working directory**. It indicates where one file of your code exists. Read the link in my earlier comment to find out what you should print. Again, what you are guessing the current working directory is, is incorrect. – Drew Dormann Jan 07 '23 at 02:36
  • @dorKKnight your suggestion worked! I guess my previous use of `std::string filename = "input.txt"` was incorrect. I thought relative paths can be used this way.. I must be wrong somehow. – Tong Zhao Jan 07 '23 at 02:36
  • @DrewDormann thank you, I just realized current path and file path are two different things. But, since my C++ language setting does not seem to support the syntax of `std::filesystem::current_path`. how do I use the alternative, which is `boost::filesystem::get_path()` to obtain the path? My Visual Studio seems to reject both syntaxes. The C++ language setting is set to `C++17` though. – Tong Zhao Jan 07 '23 at 02:41
  • 1
    @TongZhao you can find an answer [here](https://stackoverflow.com/questions/875249/how-to-get-current-directory). – Drew Dormann Jan 07 '23 at 02:46
  • @DrewDormann thanks again. I see. I need to include a header ``. And indeed I realized the current path is NOT the google project folder, but a folder called `Debug` beneath it. However, even if I copy paste the `input.txt` file to the `Debug` folder, and use `std::string filename = "input.txt"`, it still cannot find the file. Is my syntax this time still missing something? – Tong Zhao Jan 07 '23 at 02:48
  • @TongZhao furthermoe, anyone running your program can have _any_ "current path". If you gave your program to someone else, it would be very unlikely that `C:\Users\Tong\source\repos\BookLibrary\GoogleTest` would even exist on their computer, let alone be their current working directory. – Drew Dormann Jan 07 '23 at 02:51
  • @DrewDormann you're right. Besides using the "current path," do you have a suggestion for a more portable solution? – Tong Zhao Jan 07 '23 at 03:13
  • @TongZhao if that is your newest question, I recommend posting it as a new question so you get an appropriate number of eyes on it. – Drew Dormann Jan 07 '23 at 03:14

1 Answers1

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.

Tong Zhao
  • 91
  • 7