5

我想做这样的事情:

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. 我使用预处理器,因此可能需要一些时间才能找到错误。
  3. 当函数真正嵌套时,该解决方案很难使用。
  4. 当许多期望失败时,它将多次打印消息。
  5. 有必要为各种检查重新定义宏
4

2 回答 2

5

做你想做的事很困难的事实实际上是测试代码的味道。特别是,这两个测试(1)做的太多,(2)不可读,因为它们没有描述被测单元的作用。

我推荐两本读物:Unit Tests are FIRSTModern C++ Programming with Test-Driven Development一书。

与其尝试调用 4 个函数,每个函数都检查一些东西,然后想知道在失败的情况下如何打印错误消息,我建议如下:

  • 问问自己:“我在这里测试什么?” 当您有答案时,请使用该答案为测试命名。如果你找不到答案,这意味着(我怀疑)测试做的太多了。尝试遵循“每个测试一个断言”指南并相应地拆分测试。
  • 本着同样的精神,查看 4 个函数中的每一个,并尝试为每个函数命名。如果你不能,每个函数都检查太多了。拆分这些功能。
  • 问问自己你是否真的需要期望(而不是断言)。通常使用 EXPECT 而不是 ASSERT 的唯一原因是因为单个测试做得太多。拆分它。

在此过程结束时,您应该会发现自己可以通过以下方式实现打印有关测试失败的附加信息的目标:

ASSERT_THAT(Response, EQ("something")) << "Request: " << request;

注意:如果比起点更好,我认为上面的例子还不够好。测试名称应该非常好,如此具有描述性,以至于您可以通过打印 的值来获得零信息request

我意识到这是一种哲学答案;另一方面,它直接来自我尝试编写好的、可维护的测试的经验。编写好的测试需要与编写好的代码一样的谨慎,并且会获得很多回报 :-)

于 2016-12-08T19:45:51.650 回答
1

一个非意识形态的答案(基于来自各地的信息):

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

于 2021-02-12T08:07:29.653 回答