'Why does changing the name of a unit test in Google Test somehow makes me unable to open a file?
The problem
I'm trying to test a C++ function using Google Test that reads a text file into a vector of strings that delimits by whitespace. The implementation details are not causing the error to my knowledge since using any generic file open triggers the error.
The issue I ran into is that I opened the file for the first time under the test name 'TestReadFile', and since that action, I am unable to open the file in any other test with a different name. I do suspect that it has something to do with the name, because when I change a test's name to 'TestReadFile,' no matter if the code is different, it is able to read the file successfully.
#include "gtest/gtest.h"
#include "words.h"
#include <fstream>
TEST(CommonWordsTests, TestReadFile) {
std::ifstream fin;
fin.open("test.txt");
EXPECT_TRUE(fin.is_open());
fin.close();
}
TEST(CommonWordsTests, NotTestReadFile) {
std::ifstream fin;
fin.open("test.txt");
EXPECT_TRUE(fin.is_open());
fin.clear();
fin.seekg(0, std::ios::beg);
fin.close();
}
Upon running this code, the first test (TestReadFile) passes, whereas the second test (NotTestReadFile) fails, returning the following output:
Failure
Value of: fin.is_open()
Actual: false
Expected: true
What I tried
Originally, I thought this was due to me not closing the file, so I appended fin.close() and
fin.clear();
fin.seekg(0, std::ios::beg);
to no avail.
Does anyone have any idea what this could be?
Edit: I downloaded GoogleTest into my local directory using CMake but one could also clone Google Test from https://github.com/google/googletest into the local directory, and the include statements would work fine.
Let me know if this is still not reproducible.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
