Googletest 永远不会通过 bazel 测试失败(应该失败),但可以与 cmake 和 clion 一起使用

Googletest never fails (where it should) with bazel test but works with cmake & clion

我正在尝试使用 googletest with bazel 和 cmake。

对于测试部分,cmake 和 clion 工作完美,它在应该失败的地方失败了,在应该通过的地方通过了。 然而,bazel test 将通过所有的测试,即使他们不应该通过。

Bazel 测试不工作

例如,我有 stupid_test.cc 文件:

#include "gtest/gtest.h"

TEST(StupidTests, Stupid1) {
  EXPECT_EQ(100, 20);
  EXPECT_TRUE(false);
}

应该总是失败。

挡板BUILD 文件:

cc_test(
    name = "stupid_test",
    srcs = ["stupid_test.cc"],
    deps = [
        "//bazel_build/googletest:gtest",
    ],
)

其中 bazel_build/googletest 与从 googletest github 下载的文件完全相同。

然后我运行bazel test :stupid_test,输出是:

⇒  blaze test :stupid_test
DEBUG: /private/var/tmp/_bazel_myusername/741c62b201e51840aa320b156e05fd70/external/bazel_tools/tools/osx/xcode_configure.bzl:87:9: Invoking xcodebuild failed, developer dir: /Users/honghaoli/Downloads/Xcode.app/Contents/Developer ,return code 1, stderr: xcrun: error: invalid DEVELOPER_DIR path (/Users/honghaoli/Downloads/Xcode.app/Contents/Developer), missing xcrun at: /Users/honghaoli/Downloads/Xcode.app/Contents/Developer/usr/bin/xcrun
, stdout: 
INFO: Analysed target //:stupid_test (0 packages loaded, 0 targets configured).
INFO: Found 1 test target...
Target //:stupid_test up-to-date:
  bazel-bin/stupid_test
INFO: Elapsed time: 1.840s, Critical Path: 1.55s
INFO: 4 processes: 4 darwin-sandbox.
INFO: Build completed successfully, 4 total actions
//:stupid_test                                                           PASSED in 0.1s

Executed 1 out of 1 test: 1 test passes.
INFO: Build completed successfully, 4 total actions

我不知道为什么它通过了。

CMake 作品

使用 cmake 和 clion 时,相同的测试文件会失败并显示非常明确的消息。例如CMakeLists

的一部分
add_executable(stupid_test stupid_test.cc)
target_link_libraries(stupid_test gtest gtest_main)
add_test(NAME stupid_test COMMAND stupid_test)

输出失败信息:

Testing started at 19:16 ...
/Users/myusername...blabla/cmake-build-debug/stupid_test --gtest_filter=* --gtest_color=no
Running main() from /Users/myusername...blabla/cmake-build-debug/googletest-src/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test suite./Users/myusername...blabla/stupid_test.cc:11: Failure
Expected equality of these values:
  100
  20
/Users/myusername...blabla/stupid_test.cc:12: Failure
Value of: false
  Actual: false
Expected: true

/Users/myusername...blabla/stupid_test.cc:13: Failure
Value of: true
  Actual: true
Expected: false


Process finished with exit code 1

如果有帮助,我正在使用 Mac OS 10.14。

找到原因了

改变一下

"//bazel_build/googletest:gtest",

BUILD文件中放入

"//bazel_build/googletest:gtest_main",

我还是一头雾水! 如果 gtest 不起作用,它至少应该使构建失败或抛出 error/warnings。它永远不应该说:

Executed 1 out of 1 test: 1 test passes.

作为输出。

我认为这是一个错误。