15

I really like catch.hpp for testing (https://github.com/philsquared/Catch). I like its BDD style and its REQUIRE statements, its version of asserts. However, catch does not come with a mocking framework.

The project I'm working on has GMock and GTest but we've used catch for a few projects as well. I'd like to use GMock with catch.

I found 2 conflicts in the catch.hpp and gtests header files for the macros FAIL and SUCCEED. Since I'm not using the TDD style but instead the BDD style I commented them out, I checked that they weren't referenced anywhere else in catch.hpp.

Problem: Using EXPECT_CALL() doesn't return anything or have callbacks to know if the EXPECT passed. I want to do something like:

REQUIRE_NOTHROW(EXPECT_CALL(obj_a, an_a_method()).Times(::testing::AtLeast(1)));

Question: How can I get a callback if EXPECT_CALL fails (or a return value)

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Erock 634
  • 591
  • 3
  • 18
  • 4
    [trompeloeil](https://github.com/rollbear/trompeloeil) sounds interesting, and is designed to work with Catch. Personally, I'd try that before Google's behemoth. – Mike Seymour May 27 '15 at 14:53

4 Answers4

12

EDIT: Figured out how to integrate it and put an example in this github repo https://github.com/ecokeley/catch_gmock_integration


After hours of searching I went back to gmock and just read a bunch about it. Found this in "Using Google Mock with Any Testing Framework":

::testing::GTEST_FLAG(throw_on_failure) = true;
::testing::InitGoogleMock(&argc, argv);

This causes an exception to be thrown on a failure. They recommend "Handling Test Events" for more seamless integration.

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());
  }
}
metal
  • 6,202
  • 1
  • 34
  • 49
Erock 634
  • 591
  • 3
  • 18
4

Because of the macros FAIL and SUCCEED in version 1.8.0 gmock added the following to gtest.h:

#if !GTEST_DONT_DEFINE_FAIL
  # define FAIL() GTEST_FAIL()
#endif

#if !GTEST_DONT_DEFINE_SUCCEED
  # define SUCCEED() GTEST_SUCCEED()
#endif

So by adding GTEST_DONT_DEFINE_FAIL and GTEST_DONT_DEFINE_SUCCEED to the preprocessor definitions you will avoid the conflict

Alon
  • 41
  • 2
2

I created a small example how to integrate GMock with Catch2.

https://github.com/matepek/catch2-with-gmock

Hope it helps someone.

Disclaimer: It is not bulletproof. Feel free to contribute and improve.

Mate059
  • 103
  • 5
0

There is also gtestbdd in the cppbdd project which adds BDD support in a single header for gtest (rather than replacing it). It recently had an improvement to enable parameterized tests to work in a BDD style. There is a tutorial in the readme of:

https://github.com/Resurr3ction/cppbdd

Dan
  • 146
  • 4