2011-01-11 19 views
4

我的一些代码抛出使用打印exception.what()在谷歌测试

if (failure) 
    throw std::runtime_error("a bad thing happened: ..."); 

我使用谷歌测试及TeamCity的自动执行我的测试。它运行在Windows上,所以我使用--gtest_catch_exceptions参数来报告测试失败,如果发生意外的异常。然而,谷歌测试只是通过一条消息,如

Exception thrown with code 0xe06d7363 in the test body. 
in (null) line -1 

这样的消息不是很有帮助。我宁愿像

Exception thrown: "a bad thing happened: ..." 

的消息我有一个实现方法

OnTestPartResult(const ::testing::TestPartResult& test_part_result) 

定制TestListener但它好像有到被谷歌测试捕捉到的异常没有提及。是否有任何其他方式向std :: cout或其他地方报告异常?

请注意,我不能使用

try 
{ 
    return RUN_ALL_TESTS(); 
} 
catch (std::exception& e) 
{ 
    std::cout << "EXCEPTION: " << e.what(); 
    return -1; 
} 
catch (...) 
{ 
    return -1; 
} 

没有--gtest_catch_exceptions,因为测试执行,然后获取第一异常“取消”。

我也不想更改投掷代码。

感谢您的任何想法!

+0

您使用的是哪种googletest版本?它看起来像树干支持自动输出字符串就像你想要的。 – 2011-01-11 08:54:27

回答

0

我使用gmock-1.7.0提供的gtest。以下是我从gmock-1.7.0目录所做的:

diff --git a/gtest/include/gtest/internal/gtest-internal.h b/gtest/include/gtest/internal/gtest-internal.h 
index 0dcc3a3..265093b 100644 
--- a/gtest/include/gtest/internal/gtest-internal.h 
+++ b/gtest/include/gtest/internal/gtest-internal.h 
@@ -1075,7 +1075,8 @@ class NativeArray { 
    try { \ 
     GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 
    } \ 
- catch (...) { \ 
+ catch (std::exception *e) { \ 
+  std::cout << e->what() << std::endl; \ 
     goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \ 
    } \ 
    } else \ 

在英语中,我明确地抓到的std ::例外,而不是..(我的一切扔从派生),并添加的回声在E->什么()

希望这有助于。