I'm using GoogleTest with bazel and when I run bazel test :tokens_test (rule defined below) I have a failing test, but when I run the compiled tests I see the expected result. It is failing because the test can not open the test data file. The directory layout and the BUILD rule for the test I'm running look like this:
tokens/
BUILD
tokens_test.cpp
test_data/
test_input1.txt
cc_test(
name = "tokens_test",
srcs = ["tokens_test.cpp"],
deps = [
'@gtest//:gtest'
'@gtest//:gtest_main',
],
data = ["//tokens/test_data:test_input1.txt"]
)
At this point the test is just a wrapper to open the file and read in the test data which is the bit thats failing.
TEST(Tokenizer, OpenFileTest) {
auto fin = std::ifstream("test_data/test_input1.txt");
std::cout << a.get(); // Outputs -1.
}
When I navigate to the bazel-out location and find my way to the tokens runfiles directory I can see the compiled test excutable.
[jibberish]/__main__/tokens>ls
test_data/
test_input1.txt
tokens_test
And when I run the executable:
[jibberish]/__main__/tokens>./tokens_test
...normal test output...
The correct output!
...more test output...
I'm lost as to where to start looking for the problem. I've tried including a BUILD in the test_data directory with an exports_files rule, using a variety of relatives paths in the BUILD files and my source code and a lot of permutations of those relative paths.