testlib: Deprecate QWARN() in favor of qWarning()

The QtTest best practices documentations recommends using output
mechanisms such as qDebug() and qWarning() for diagnostic messages,
and this is also what most of our own tests do.

The QWARN() macro and corresponding internal QTest::qWarn() function
was added when QtTest was first implemented, but was likely meant as
an internal implementation detail, like its cousin QTestLog::info(),
which does not have any corresponding macro.

This theory is backed by our own QtTest self-test (tst_silent)
describing the output from QWARN() as "an internal testlib warning".

The only difference between QWARN() and qWarning(), besides the much
richer feature set of the latter, is that qWarning() will not pass
on file and line number information in release mode, but QWARN() will.
This is an acceptable loss of functionality, considering that the user
can override this behavior by defining QT_MESSAGELOGCONTEXT.

[ChangeLog][QtTest] QWARN() has been deprecated in favor of qWarning()

Pick-to: 6.2
Change-Id: I5a2431ce48c47392244560dd520953b9fc735c85
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Tor Arne Vestbø 2021-07-31 18:25:42 +02:00
parent fe9c705e9a
commit bef57b317f
26 changed files with 53 additions and 45 deletions

View File

@ -208,9 +208,6 @@ do {\
return;\
} while (false)
#define QWARN(msg)\
QTest::qWarn(static_cast<const char *>(msg), __FILE__, __LINE__)
#ifdef QT_TESTCASE_BUILDDIR
#ifndef QT_TESTCASE_SOURCEDIR
@ -576,6 +573,19 @@ namespace QTest
#undef QTEST_COMPARE_DECL
#if QT_DEPRECATED_SINCE(6, 2)
namespace QTestPrivate {
QT_DEPRECATED_VERSION_X_6_2("Use qWarning() instead")
Q_DECL_UNUSED static inline void qWarnMacro(const char *message, const char *file = nullptr, int line = 0)
{
QTest::qWarn(message, file, line);
}
}
#endif
#define QWARN(msg) \
QTestPrivate::qWarnMacro(static_cast<const char *>(msg), __FILE__, __LINE__)
QT_END_NAMESPACE
#endif

View File

@ -336,6 +336,7 @@
\relates QTest
\threadsafe
\deprecated Use qWarning() instead.
Appends \a message as a warning to the test log. This macro can be used anywhere
in your tests.

View File

@ -299,7 +299,7 @@ void tst_LargeFile::createSparseFile()
DWORD bytes;
if (!::DeviceIoControl(handle, FSCTL_SET_SPARSE, NULL, 0, NULL, 0,
&bytes, NULL)) {
QWARN("Unable to set test file as sparse. "
qWarning("Unable to set test file as sparse. "
"Limiting test file to 16MiB.");
maxSizeBits = 24;
}
@ -347,11 +347,10 @@ void tst_LargeFile::fillFileSparsely()
{
if (failed) {
this_->maxSizeBits = lastKnownGoodIndex;
QWARN( qPrintable(
QString("QFile::error %1: '%2'. Maximum size bits reset to %3.")
.arg(this_->largeFile.error())
.arg(this_->largeFile.errorString())
.arg(this_->maxSizeBits)) );
qWarning("QFile::error %d: '%s'. Maximum size bits reset to %d.",
this_->largeFile.error(),
qPrintable(this_->largeFile.errorString()),
this_->maxSizeBits);
} else
lastKnownGoodIndex = qMax<int>(index_, lastKnownGoodIndex);
}

View File

@ -1360,7 +1360,7 @@ void tst_QFileInfo::isSymbolicLink_data()
#if defined(Q_OS_WIN)
const auto creationResult = FileSystem::createSymbolicLink("symlink", m_sourceFile);
if (creationResult.dwErr == ERROR_PRIVILEGE_NOT_HELD) {
QWARN(msgInsufficientPrivileges(creationResult.errorMessage));
qWarning() << qPrintable(msgInsufficientPrivileges(creationResult.errorMessage));
} else {
QVERIFY2(creationResult.dwErr == ERROR_SUCCESS, qPrintable(creationResult.errorMessage));
QTest::newRow("NTFS-symlink")
@ -1423,7 +1423,7 @@ void tst_QFileInfo::link_data()
#if defined(Q_OS_WIN)
auto creationResult = FileSystem::createSymbolicLink("link", m_sourceFile);
if (creationResult.dwErr == ERROR_PRIVILEGE_NOT_HELD) {
QWARN(msgInsufficientPrivileges(creationResult.errorMessage));
qWarning() << qPrintable(msgInsufficientPrivileges(creationResult.errorMessage));
} else {
QVERIFY2(creationResult.dwErr == ERROR_SUCCESS, qPrintable(creationResult.errorMessage));
QTest::newRow("link")
@ -1432,7 +1432,7 @@ void tst_QFileInfo::link_data()
creationResult = FileSystem::createSymbolicLink("brokenlink", "dummyfile");
if (creationResult.dwErr == ERROR_PRIVILEGE_NOT_HELD) {
QWARN(msgInsufficientPrivileges(creationResult.errorMessage));
qWarning() << qPrintable(msgInsufficientPrivileges(creationResult.errorMessage));
} else {
QVERIFY2(creationResult.dwErr == ERROR_SUCCESS, qPrintable(creationResult.errorMessage));
QTest::newRow("broken link")

View File

@ -2073,7 +2073,7 @@ void SettingsThread::run()
settings.setValue(QString::number((param * NumIterations) + i), param);
settings.sync();
if (settings.status() != QSettings::NoError) {
QWARN(qPrintable(QString("Unexpected QSettings status %1").arg((int)settings.status())));
qWarning() << qPrintable(QString("Unexpected QSettings status %1").arg((int)settings.status()));
++numThreadSafetyFailures;
}
}

View File

@ -38,7 +38,7 @@ private Q_SLOTS:
void tst_QString_NoCastFromByteArray::initTestCase()
{
QWARN("This is a compile test only");
qWarning("This is a compile test only");
}
QTEST_APPLESS_MAIN(tst_QString_NoCastFromByteArray)

View File

@ -2063,7 +2063,7 @@ void tst_QWindow::modalDialog()
QGuiApplication::processEvents();
if (isPlatformOffscreenOrMinimal()) {
QWARN("Focus stays in normalWindow on offscreen/minimal platforms");
qWarning("Focus stays in normalWindow on offscreen/minimal platforms");
QTRY_COMPARE(QGuiApplication::focusWindow(), &normalWindow);
return;
}
@ -2109,7 +2109,7 @@ void tst_QWindow::modalDialogClosingOneOfTwoModal()
QGuiApplication::processEvents();
if (isPlatformOffscreenOrMinimal()) {
QWARN("Focus is lost when closing modal dialog on offscreen/minimal platforms");
qWarning("Focus is lost when closing modal dialog on offscreen/minimal platforms");
QTRY_COMPARE(QGuiApplication::focusWindow(), nullptr);
return;
}

View File

@ -599,7 +599,7 @@ QImage tst_QPainter::getResImage( const QString &dir, const QString &addition, c
QImage res;
QString resFilename = dir + QLatin1String("/res_") + addition + QLatin1Char('.') + extension;
if ( !res.load( resFilename ) ) {
QWARN(QString("Could not load result data %s %1").arg(resFilename).toLatin1());
qWarning() << "Could not load result data" << resFilename;
return QImage();
}
return res;
@ -610,14 +610,14 @@ QBitmap tst_QPainter::getBitmap( const QString &dir, const QString &filename, bo
QBitmap bm;
QString bmFilename = dir + QLatin1Char('/') + filename + QLatin1String(".xbm");
if ( !bm.load( bmFilename ) ) {
QWARN(QString("Could not load bitmap '%1'").arg(bmFilename).toLatin1());
qWarning() << "Could not load bitmap" << bmFilename;
return QBitmap();
}
if ( mask ) {
QBitmap mask;
QString maskFilename = dir + QLatin1Char('/') + filename + QLatin1String("-mask.xbm");
if (!mask.load(maskFilename)) {
QWARN(QString("Could not load mask '%1'").arg(maskFilename).toLatin1());
qWarning() << "Could not load mask" << maskFilename;
return QBitmap();
}
bm.setMask( mask );

View File

@ -180,7 +180,7 @@ static QByteArray readFile(const QString &absFilePath)
{
QFile file(absFilePath);
if (!file.open(QIODevice::ReadOnly)) {
QWARN("failed to open file");
qWarning("failed to open file");
return QByteArray();
}
return file.readAll();

View File

@ -212,7 +212,7 @@ static QByteArray readFile(const QString &absFilePath)
{
QFile file(absFilePath);
if (!file.open(QIODevice::ReadOnly)) {
QWARN("failed to open file");
qWarning("failed to open file");
return QByteArray();
}
return file.readAll();

View File

@ -127,7 +127,7 @@ void tst_Lancelot::initTestCase()
QDir qpsDir(scriptsDir);
qpsFiles = qpsDir.entryList(QStringList() << QLatin1String("*.qps"), QDir::Files | QDir::Readable);
if (qpsFiles.isEmpty()) {
QWARN("No qps script files found in " + qpsDir.path().toLatin1());
qWarning() << "No qps script files found in" << qpsDir.path();
QSKIP("Aborted due to errors.");
}

View File

@ -344,7 +344,7 @@ void tst_QFocusEvent::checkReason_ActiveWindow()
#if defined(Q_OS_WIN)
if (QSysInfo::kernelVersion() == "10.0.15063") {
// Activate window of testFocusWidget, focus in that window goes to childFocusWidgetOne
QWARN("Windows 10 Creators Update (10.0.15063) requires explicit activateWindow()");
qWarning("Windows 10 Creators Update (10.0.15063) requires explicit activateWindow()");
testFocusWidget->activateWindow();
}
#endif
@ -359,7 +359,7 @@ void tst_QFocusEvent::checkReason_ActiveWindow()
if (!QGuiApplication::platformName().compare(QLatin1String("offscreen"), Qt::CaseInsensitive)
|| !QGuiApplication::platformName().compare(QLatin1String("minimal"), Qt::CaseInsensitive)) {
// Activate window of testFocusWidget, focus in that window goes to childFocusWidgetOne
QWARN("Platforms offscreen and minimal require explicit activateWindow()");
qWarning("Platforms offscreen and minimal require explicit activateWindow()");
testFocusWidget->activateWindow();
}

View File

@ -1888,10 +1888,10 @@ void tst_QSqlQuery::precision()
// TDS has crappy precisions by default
if (dbType == QSqlDriver::Sybase) {
if (i < 18)
QWARN("TDS didn't return the right precision");
qWarning("TDS didn't return the right precision");
} else {
QWARN(QString(tst_Databases::dbToString(db) + " didn't return the right precision (" +
QString::number(i) + " out of 21), " + val).toUtf8());
qWarning() << tst_Databases::dbToString(db) << "didn't return the right precision ("
<< i << "out of 21)," << val;
}
}
} // SQLITE scope

View File

@ -89,7 +89,7 @@ static void abort_handler(int)
void tst_Blacklisted::messages()
{
qWarning("This is a warning that should not appear in silent test output");
QWARN("This is an internal testlib warning that should not appear in silent test output");
QTestLog::warn("This is an internal testlib warning that should not appear in silent test output", __FILE__, __LINE__);
qDebug("This is a debug message that should not appear in silent test output");
qCritical("This is a critical message that should not appear in silent test output");
qInfo("This is an info message that should not appear in silent test output");

View File

@ -7,7 +7,7 @@
</properties>
<testcase name="initTestCase" result="pass" time="@TEST_DURATION@"/>
<testcase name="testFunc1" result="pass" time="@TEST_DURATION@">
<!-- type="warn" message="just a QWARN() !" -->
<!-- type="qwarn" message="just a qWarning() !" -->
</testcase>
<testcase name="testFunc2" result="fail" time="@TEST_DURATION@">
<!-- type="qdebug" message="a qDebug() call with comment&#x002D;ending stuff &#x002D;&#x002D;&gt;" -->
@ -38,6 +38,6 @@
<![CDATA[this failure is also expected]]>
</system-out>
<system-err>
<![CDATA[just a QWARN() !]]>
<![CDATA[just a qWarning() !]]>
</system-err>
</testsuite>

View File

@ -51,7 +51,7 @@ tst_JUnit::tst_JUnit()
void tst_JUnit::testFunc1()
{
QWARN("just a QWARN() !");
qWarning("just a qWarning() !");
QCOMPARE(1,1);
}

View File

@ -86,7 +86,7 @@ static void abort_handler(int)
void tst_Silent::messages()
{
qWarning("This is a warning that should not appear in silent test output");
QWARN("This is an internal testlib warning that should not appear in silent test output");
QTestLog::warn("This is an internal testlib warning that should not appear in silent test output", __FILE__, __LINE__);
qDebug("This is a debug message that should not appear in silent test output");
qCritical("This is a critical message that should not appear in silent test output");
qInfo("This is an info message that should not appear in silent test output");

View File

@ -181,10 +181,8 @@ static QStringList readLinesFromFile(const QString &fileName,
QFile file(fileName);
bool ok = file.open(QIODevice::ReadOnly | QIODevice::Text);
if (!ok) {
QWARN(qPrintable(QString::fromLatin1("Could not open testdata file %1: %2")
.arg(fileName, file.errorString())));
}
if (!ok)
qWarning() << "Could not open testdata file" << fileName << ":" << file.errorString();
return QString::fromUtf8(file.readAll()).split(QLatin1Char('\n'), splitBehavior);
}

View File

@ -260,7 +260,7 @@ void tst_QFontDialog::testNonStandardFontSize()
if (accepted)
QCOMPARE(testFont.pointSize(), resultFont.pointSize());
else
QWARN("Fail using a non-standard font size.");
qWarning("Fail using a non-standard font size.");
}
QTEST_MAIN(tst_QFontDialog)

View File

@ -1219,7 +1219,7 @@ void tst_QGraphicsScene::addPath()
QVERIFY(scene.items(QPointF(-10, 20)).isEmpty());
QVERIFY(scene.items(QPointF(10, 20)).isEmpty());
if (sizeof(qreal) != sizeof(double))
QWARN("Skipping test because of rounding errors when qreal != double");
qWarning("Skipping test because of rounding errors when qreal != double");
else
QVERIFY(scene.items(QPointF(-10, 30)).isEmpty());
QVERIFY(scene.items(QPointF(10.1, 30)).isEmpty());

View File

@ -967,7 +967,7 @@ void tst_QAbstractItemView::dragAndDrop()
if (successes < attempts) {
QString msg = QString("# successes (%1) < # attempts (%2)").arg(successes).arg(attempts);
QWARN(msg.toLatin1());
qWarning() << qPrintable(msg);
}
QVERIFY(successes > 0); // allow for some "event unstability" (i.e. unless
// successes == 0, QAbstractItemView is probably ok!)

View File

@ -3460,7 +3460,7 @@ void tst_QComboBox::task_QTBUG_52027_mapCompleterIndex()
completer->setModel(model);
if (QGuiApplication::platformName() == "offscreen") {
QWARN("Offscreen platform requires explicit activateWindow()");
qWarning("Offscreen platform requires explicit activateWindow()");
cbox.activateWindow();
}

View File

@ -1045,7 +1045,7 @@ void tst_QDoubleSpinBox::undoRedo()
QVERIFY(!spin.lineEdit()->isUndoAvailable());
QVERIFY(spin.lineEdit()->isRedoAvailable());
} else {
QWARN("Undo not tested because no key sequence associated to QKeySequence::Redo");
qWarning("Undo not tested because no key sequence associated to QKeySequence::Redo");
}
@ -1058,7 +1058,7 @@ void tst_QDoubleSpinBox::undoRedo()
QVERIFY(!spin.lineEdit()->isRedoAvailable());
QVERIFY(spin.lineEdit()->isUndoAvailable());
} else {
QWARN("Redo not tested because no key sequence associated to QKeySequence::Redo");
qWarning("Redo not tested because no key sequence associated to QKeySequence::Redo");
}

View File

@ -1811,7 +1811,7 @@ void tst_QMenu::menuSize_Scrolling()
QSize s = size();
if (!QGuiApplication::platformName().compare(QLatin1String("minimal"), Qt::CaseInsensitive)
|| !QGuiApplication::platformName().compare(QLatin1String("offscreen"), Qt::CaseInsensitive)) {
QWARN("Skipping test on minimal/offscreen platforms - QTBUG-73522");
qWarning("Skipping test on minimal/offscreen platforms - QTBUG-73522");
QMenu::showEvent(e);
return;
}

View File

@ -921,7 +921,7 @@ void tst_QMenuBar::check_escKey()
if (!QGuiApplication::platformName().compare(QLatin1String("minimal"), Qt::CaseInsensitive)
|| !QGuiApplication::platformName().compare(QLatin1String("offscreen"), Qt::CaseInsensitive)) {
QWARN("Skipping menu button test on minimal/offscreen platforms");
qWarning("Skipping menu button test on minimal/offscreen platforms");
return;
}

View File

@ -101,7 +101,7 @@ void tst_LanceBench::initTestCase()
QDir qpsDir(scriptsDir);
qpsFiles = qpsDir.entryList(QStringList() << QLatin1String("*.qps"), QDir::Files | QDir::Readable);
if (qpsFiles.isEmpty()) {
QWARN("No qps script files found in " + qpsDir.path().toLatin1());
qWarning() << "No qps script files found in" << qpsDir.path();
QSKIP("Aborted due to errors.");
}