Googletest不会通过bazel测试(应该在任何地方失败),但可以与cmake和clion一起使用

时间:2019-03-25 02:23:53

标签: c++ unit-testing cmake googletest bazel

我正在尝试将googletestbazel和cmake一起使用。

cmake和clion就像测试零件一样完美,可以在应该失败的地方失败,可以通过的地方通过。 但是,bazel test将通过所有测试,即使它们不应该通过。

Bazel测试无效

例如,我有stupid_test.cc文件:

#include "gtest/gtest.h"

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

应该总是失败的。

Bazel 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 Works

使用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。

1 个答案:

答案 0 :(得分:1)

我弄清楚了原因。

只是改变

"//bazel_build/googletest:gtest",

BUILD文件中

"//bazel_build/googletest:gtest_main",

我还是很困惑! 如果gtest不起作用,它至少应该使构建失败或抛出错误/警告。它永远不要说:

Executed 1 out of 1 test: 1 test passes.

作为输出。

我认为这是一个错误。