2015-05-27 55 views
9

我真的很喜歡catch.hpp進行測試(https://github.com/philsquared/Catch)。我喜歡它的BDD風格和它的REQUIRE語句,它的assert版本。然而,catch並不帶有嘲諷的框架。Google Mock和Catch.hpp集成

我正在開發的項目有GMock和GTest,但我們也使用了catch幾個項目。我想用catch來使用GMock。

我在catch.hpp和gtests頭文件中找到了2個與宏FAIL和SUCCEED相關的衝突。由於我沒有使用TDD風格,而是我將它們評論爲BDD風格,所以我檢查了它們沒有在catch.hpp的其他地方引用。

問題:使用EXPECT_CALL()不會返回任何東西或有回調知道是否通過了期望。我想要做的事,如:

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

問題:我怎樣才能得到一個回調,如果EXPECT_CALL失敗(或返回值)

+3

[trompeloeil](https://github.com/rollbear/trompeloeil)聽起來很有趣,並且設計用於使用Catch。就我個人而言,我會在Google的巨獸面前嘗試。 –

回答

8

編輯:想出如何將其整合,把一個例如,在此GitHub庫https://github.com/ecokeley/catch_gmock_integration


後搜索我的時間又回到gmock和剛讀了一堆關於它。在"Using Google Mock with Any Testing Framework"中發現:

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

這會導致在發生故障時引發異常。他們建議"Handling Test Events"進行更加無縫的整合。

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()); 
    } 
} 
0

由於宏失敗,並在版本1.8.0 SUCCEED添加gmock以下到gtest.h:

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

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

所以通過增加GTEST_DONT_DEFINE_FAIL和GTEST_DONT_DEFINE_SUCCEED到預處理器定義,你會避免衝突