C GTEST在测试结束时仅在测试失败时打印其他信息
c++ gtest print additional information in the end of a test when and only when the test fails
我想这样做:
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
但是这种解决方案不舒服,因为:
- 我检查两次
- 我使用预处理器,因此可能需要一些时间才能找到bug。
- 当功能真正嵌套时,难以使用解决方案。
- 当许多期望故障时,它会多次打印消息。
- 需要重新定义各种检查
做自己想做的事情很难实际上是测试代码的气味。特别是,这两个测试(1)做得太多了,(2)不可读,因为它们没有描述正在测试的单元所做的。
我建议两次读数:单位测试是第一个,并且具有测试驱动开发的现代C 编程。
而不是试图调用4个功能,每个功能都检查某些内容,然后想知道如何在失败时打印错误消息,而是建议以下内容:
- 问自己:"我在这里测试什么?"当您有答案时,请使用答案为测试命名。如果您找不到答案,那就意味着(我怀疑)测试做得太多。尝试遵循"每次测试一项主张"指南并相应地分配测试。
- 本着同样的精神,查看4个功能中的每个功能,并尝试给每个功能一个名字。如果不能,每个功能都会检查太多。拆分这些功能。
- 问问自己,您是否真的需要期望(而不是断言)。通常,有期望而不是断言的唯一原因是因为单个测试做得太多了。拆分。
在此过程结束时,您应该发现自己在打印有关测试失败的其他信息的目的是通过以下方式触及:
。ASSERT_THAT(Response, EQ("something")) << "Request: " << request;
注意:另外,如果起点更好,我认为上面的示例不够好。测试名称应该很好,如此描述性,以至于您通过打印request
的值获得零信息。
我意识到这是一种哲学答案。另一方面,这直接来自我尝试编写良好,可维护的测试的经验。编写良好的测试需要比编写良好代码相同的护理,并且它将还清很多次: - )
一个非意识形态答案(基于来自整个地方的信息):
QDebugTest.h
class QDebugTest : public ::testing::Test
{
public:
void SetUp() override;
void TearDown() override;
};
QDebugTest.cpp
static std::ostringstream qdebugString;
static void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
switch (type) {
case QtDebugMsg: qdebugString << "qDebug"; break;
case QtInfoMsg: qdebugString << "qInfo"; break;
case QtWarningMsg: qdebugString << "qWarning"; break;
case QtCriticalMsg: qdebugString << "qCritical"; break;
case QtFatalMsg: qdebugString << "qFatal"; break;
}
if (context.file) {
qdebugString << " (" << context.file << ":" << context.line ;
}
if (context.function) {
qdebugString << " " << context.function;
}
if(context.file || context.function) {
qdebugString << ")";
}
qdebugString << ": ";
qdebugString << msg.toLocal8Bit().constData();
qdebugString << "n";
}
void QDebugTest::SetUp()
{
assert(qdebugString.str().empty());
qInstallMessageHandler(myMessageOutput);
}
void QDebugTest::TearDown()
{
qInstallMessageHandler(0);
if(!qdebugString.str().empty()) {
const ::testing::TestInfo* const test_info = ::testing::UnitTest::GetInstance()->current_test_info();
if (test_info->result()->Failed()) {
std::cout << std::endl << qdebugString.str();
}
qdebugString.clear();
}
}
现在从QDebugTest
而不是::testing::Test
。
相关文章:
- 如何循环打印顶点结构
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 提升测试无法找到自定义打印
- 为什么当测试用例数为 1 时,我的程序不输入而是打印新行?
- 键入特征样式以测试 T 是basic_string<>还是可打印的类似字符串的类型?
- 如何使用谷歌测试打印标准::时间类型
- 测试用例之间的不同打印格式
- C GTEST在测试结束时仅在测试失败时打印其他信息
- Google单元测试以检查表打印
- 为什么谷歌测试不打印堆栈跟踪或文件名
- 谷歌测试:如何EXPECT_EQ不可打印的ASCII值
- 枚举类的GoogleTest测试错误打印
- 如何将谷歌测试输出打印到文本文件
- 使用 GTEST 1.6 进行单元测试:如何检查打印的内容
- 从Find Max方法返回一个int值并将其打印在测试文件中
- 教谷歌测试如何打印特征矩阵
- 如果在谷歌测试中测试失败,如何打印一些东西
- 漂亮的打印 - 使用格式不正确的C++代码测试漂亮的打印机