Googletest 在 bazel test 中永远不会失败(在它应该失败的地方),但与 cmake & clion 一起工作



我正在尝试将googletest与bazel和cmake一起使用。

cmake 和 clion 对于测试部分来说效果很好,它在应该失败的地方失败,在应该通过的地方通过。但是,bazel test将通过所有测试,即使他们不应该。

挡板测试不起作用

例如,我有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 和 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不起作用,它至少应该使构建失败或引发错误/警告。它永远不应该说:

Executed 1 out of 1 test: 1 test passes.

作为输出。

我认为这是一个错误。

相关内容

最新更新