gpt4 book ai didi

c++ - 如果出现警告,如何立即使自动测试失败

转载 作者:行者123 更新时间:2023-12-01 14:54:18 25 4
gpt4 key购买 nike

当在测试代码中打印警告时,我想立即使任何自动测试失败,这样我就不会在输出中遗漏它并在以后以奇怪的测试失败告终。

我想我可以为此使用 qInstallMessageHandler()。我修改了 here 中的示例:

#include <QtTest>

class AutoTest : public QObject
{
Q_OBJECT

public:
AutoTest();
~AutoTest();

private slots:
void test_case1();

};

void warningMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
const char *file = context.file ? context.file : "";
const char *function = context.function ? context.function : "";
switch (type) {
case QtDebugMsg:
fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
break;
case QtInfoMsg:
fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
break;
case QtWarningMsg:
fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
exit(1);
case QtCriticalMsg:
fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
break;
case QtFatalMsg:
fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
break;
}
}

AutoTest::AutoTest()
{
qInstallMessageHandler(warningMessageHandler);
}

AutoTest::~AutoTest()
{

}

void AutoTest::test_case1()
{
qWarning() << "This should cause the application to exit with code 1";
}

QTEST_APPLESS_MAIN(AutoTest)

#include "tst_autotest.moc"

但是,测试应用程序并没有像预期的那样提前退出:

17:32:04: Starting /Users/mitch/dev/temp/autotest-qt5_14_fw-Debug/autotest.app/Contents/MacOS/autotest ...
********* Start testing of AutoTest *********
Config: Using QtTest library 5.14.0, Qt 5.14.0 (x86_64-little_endian-lp64 shared (dynamic) release build; by Clang 10.0.0 (clang-1000.11.45.2) (Apple))
PASS : AutoTest::initTestCase()
QWARN : AutoTest::test_case1() This should cause the application to exit
PASS : AutoTest::test_case1()
PASS : AutoTest::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted, 2ms
********* Finished testing of AutoTest *********
17:32:04: /Users/mitch/dev/temp/autotest-qt5_14_fw-Debug/autotest.app/Contents/MacOS/autotest exited with code 0

我在消息处理函数中放置了一个断点,但一次都没有命中。怎么回事?

最佳答案

虽然文档中的示例显示处理程序安装在 main() 的开头,但这不足以进行自动测试。似乎 Qt 本身在自动测试的生命周期中安装了其他消息处理程序,并且这些处理程序的安装时间比测试的构造函数要晚很多。如果您通过放置断点从源代码构建 Qt,您可以自己验证这一点 here .

Qt 的测试日志基础结构会在测试本身创建后立即安装自己的消息处理程序(在 QTEST_APPLESS_MAIN 宏中)。

如果您在 Creator 中使用 QML 调试,那么它也会安装自己的消息处理程序。

要解决这个问题,请尽可能晚地安装您的处理程序:

#include <QtTest>

class AutoTest : public QObject
{
Q_OBJECT

public:
AutoTest();
~AutoTest();

private slots:
void initTestCase();
void test_case1();

};

static QtMessageHandler oldMessageHandler;

// Exits on warnings
void warningMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
const char *file = context.file ? context.file : "";
const char *function = context.function ? context.function : "";
switch (type) {
case QtWarningMsg:
fprintf(stderr, "EXITING - TEST FAILED DUE TO WARNING: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
exit(1);
default:
oldMessageHandler(type, context, msg);
}
}

AutoTest::AutoTest()
{
}

AutoTest::~AutoTest()
{

}

void AutoTest::initTestCase()
{
oldMessageHandler = qInstallMessageHandler(warningMessageHandler);
}

void AutoTest::test_case1()
{
qWarning() << "This should cause the application to exit with code 1";
}

QTEST_APPLESS_MAIN(AutoTest)

#include "tst_autotest.moc"

一定要调用之前安装的消息处理程序,这样您就不会失去 Qt 日志记录提供的任何功能。

这个输出:

17:44:44: Starting /Users/mitch/dev/temp/autotest-qt5_14_fw-Debug/autotest.app/Contents/MacOS/autotest ...
********* Start testing of AutoTest *********
Config: Using QtTest library 5.14.0, Qt 5.14.0 (x86_64-little_endian-lp64 shared (dynamic) debug build; by Clang 10.0.0 (clang-1000.11.45.2) (Apple))
EXITING - TEST FAILED DUE TO WARNING: This should cause the application to exit with code 1 (../autotest/tst_autotest.cpp:50, void AutoTest::test_case1())
PASS : AutoTest::initTestCase()
17:44:44: /Users/mitch/dev/temp/autotest-qt5_14_fw-Debug/autotest.app/Contents/MacOS/autotest exited with code 1

关于c++ - 如果出现警告,如何立即使自动测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59093389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com