Google Mock和Catch.hpp集成

Ero*_*634 12 c++ unit-testing googlemock

我非常喜欢catch.hpp进行测试(https://github.com/philsquared/Catch).我喜欢它的BDD样式及其REQUIRE语句,它的断言版本.但是,catch并没有带有模拟框架.

我正在研究的项目有GMock和GTest,但我们也使用了catch来完成一些项目.我想用GMock和catch一起使用.

我在catch.hpp和gtests头文件中发现了2个冲突,用于宏FAIL和SUCCEED.由于我没有使用TDD风格,而是使用BDD风格我将它们评论出来,我检查了它们在catch.hpp中的任何其他地方都没有被引用.

问题:使用EXPECT_CALL()不返回任何内容或有回调来知道EXPECT是否通过.我想做的事情如下:

REQUIRE_NOTHROW(EXPECT_CALL(obj_a, an_a_method()).Times(::testing::AtLeast(1)));
Run Code Online (Sandbox Code Playgroud)

问题:如果EXPECT_CALL失败(或返回值),如何获得回调?

Ero*_*634 9

编辑:想出如何集成它并在这个github repo中添加一个例子https://github.com/ecokeley/catch_gmock_integration


经过几个小时的搜索,我回到了gmock,只是读了一堆关于它的信息.在"使用Google Mock with Any Testing Framework"中找到了这个:

::testing::GTEST_FLAG(throw_on_failure) = true;
::testing::InitGoogleMock(&argc, argv);
Run Code Online (Sandbox Code Playgroud)

这会导致失败时抛出异常.他们建议"处理测试事件"以实现更加无缝的集成.

class MinimalistPrinter : public ::testing::EmptyTestEventListener {
  // Called after a failed assertion or a SUCCEED() invocation.
  virtual void OnTestPartResult(const ::testing::TestPartResult& test_part_result) {
    printf("%s in %s:%d\n%s\n",
         test_part_result.failed() ? "*** Failure" : "Success",
         test_part_result.file_name(),
         test_part_result.line_number(),
         test_part_result.summary());
  }
}
Run Code Online (Sandbox Code Playgroud)