Google Mock and Catch.hpp Integration
Asked Answered
U

4

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)

Unpen answered 27/5, 2015 at 14:46 Comment(1)
trompeloeil sounds interesting, and is designed to work with Catch. Personally, I'd try that before Google's behemoth.Crosby
U
13

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());
  }
}
Unpen answered 27/5, 2015 at 14:55 Comment(0)
R
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

Ridglea answered 25/10, 2017 at 8:2 Comment(0)
P
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.

Pisa answered 18/2, 2020 at 16:51 Comment(2)
Still using catch2 and gmock?Preengage
Not using much C++ right now.Pisa
N
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

Niccolo answered 18/5, 2019 at 8:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.