我希望能做类似于以下的事情:
```
我希望在期望和断言失败时打印一些额外的信息。
我知道可以这样做:
```
我想实现这样的效果:
```TEST(MyTestFixture, printAfterExpectationFailure)
{
const string request("bring me tea");
const string&& response = sendRequestAndGetResponse(request);
checkResponseWithExpectarions1(response);
checkResponseWithExpectarions2(response);
checkResponseWithExpectarions3(response);
checkResponseWithExpectarions4(response);
if (anyOfExpectsFailed())
cout << "Request: " << request << "\nresponse: " << response << endl;
}
TEST(MyTestFixture, printAfterAssertionFailure)
{
const string request("bring me tea");
const string&& response = sendRequestAndGetResponse(request);
doWhenFailure([&request, &response]()
{
cout << "Request: " << request << "\nresponse: " << response << endl;
});
checkResponseWithAssertion1(response);
checkResponseWithAssertion2(response);
checkResponseWithAssertion3(response);
checkResponseWithAssertion4(response);
}
我希望在期望和断言失败时打印一些额外的信息。
我知道可以这样做:
#define MY_ASSERT_EQ(lhr, rhs, message) if(lhs != rhs) ASSERT_EQ(lhs, rhs) << message
但这种解决方案并不舒适,因为:
- 我需要检查两次
- 我使用预处理器,所以找到错误可能需要一些时间。
- 当函数真正嵌套时,这个解决方案很难使用。
- 当许多EXPECTations失败时,它会多次打印消息。
- 必须重新定义所有类型的检查宏。