C GTEST在测试结束时仅在测试失败时打印其他信息

c++ gtest print additional information in the end of a test when and only when the test fails

本文关键字:测试 打印 其他 信息 失败 结束 GTEST      更新时间:2023-10-16

我想这样做:

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

但是这种解决方案不舒服,因为:

  1. 我检查两次
  2. 我使用预处理器,因此可能需要一些时间才能找到bug。
  3. 当功能真正嵌套时,难以使用解决方案。
  4. 当许多期望故障时,它会多次打印消息。
  5. 需要重新定义各种检查

做自己想做的事情很难实际上是测试代码的气味。特别是,这两个测试(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