summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2022-07-05 19:27:10 +0200
committerEdward Welbourne <edward.welbourne@qt.io>2022-09-22 17:34:51 +0200
commit0462dba7665450bdd0dc07e6a7e6ebe2805994a9 (patch)
tree1a35e16a62f3e59040719e5440ddae89efdeaab1 /src
parentabab3c5dadd7fb80c2807f061e4d6aa7724c78c8 (diff)
Skip early return from test loops during cleanup()
The QTRY_* macros and QTestEventLoop exit early if the test has resolved; however, in the cleanup phase of a test, even if the test has failed, these loops should continue as normal. [ChangeLog][QtTest] During the cleanup() phase of a test, the QTRY_* macros and QTestEventLoop now ignore the test resolution, in contrast to when they are used from the test itself, which (since 6.3.0) exits the loops early if the test has failed. Pick-to: 6.4 6.3 Fixes: QTBUG-104441 Change-Id: I2673161967cbbc57815155af698a9338ab98a686 Reviewed-by: Jason McDonald <macadder1@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/testlib/qtestcase.cpp14
-rw-r--r--src/testlib/qtestcase.h12
-rw-r--r--src/testlib/qtesteventloop.h2
3 files changed, 22 insertions, 6 deletions
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp
index 99be5afaef..2c9f2d4049 100644
--- a/src/testlib/qtestcase.cpp
+++ b/src/testlib/qtestcase.cpp
@@ -468,6 +468,7 @@ class WatchDog;
static QObject *currentTestObject = nullptr;
static QString mainSourcePath;
+static bool inTestFunction = false;
#if defined(Q_OS_MACOS)
static IOPMAssertionID macPowerSavingDisabled = 0;
@@ -1120,6 +1121,7 @@ void TestMethods::invokeTestOnData(int index) const
/* Benchmarking: for each accumulation iteration*/
bool invokeOk;
do {
+ QTest::inTestFunction = true;
if (m_initMethod.isValid())
m_initMethod.invoke(QTest::currentTestObject, Qt::DirectConnection);
@@ -1141,6 +1143,7 @@ void TestMethods::invokeTestOnData(int index) const
invokeOk = false;
}
+ QTest::inTestFunction = false;
QTestResult::finishedCurrentTestData();
if (!initQuit) {
@@ -3012,6 +3015,17 @@ bool QTest::currentTestFailed()
return QTestResult::currentTestFailed();
}
+/*
+ Returns \c true during the run of the test-function and its set-up.
+
+ Used by the \c{QTRY_*} macros and \l QTestEventLoop to check whether to
+ return when QTest::currentTestFailed() is true.
+*/
+bool QTest::runningTest()
+{
+ return QTest::inTestFunction;
+}
+
/*! \internal
*/
QObject *QTest::testObject()
diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h
index 3d268135a4..6affbf1507 100644
--- a/src/testlib/qtestcase.h
+++ b/src/testlib/qtestcase.h
@@ -148,9 +148,10 @@ inline void useVerifyThrowsException() {}
/* Ideally we would adapt qWaitFor(), or a variant on it, to implement roughly
* what the following provides as QTRY_LOOP_IMPL(); however, for now, the
* reporting of how much to increase the timeout to (if within a factor of two)
- * on failure and the check for QTest::currentTestFailed() go beyond
- * qWaitFor(). (We no longer care about the bug in MSVC < 2017 that precluded
- * using qWaitFor() in the implementation here, see QTBUG-59096.)
+ * on failure and the check for (QTest::runningTest() &&
+ * QTest::currentTestFailed()) go beyond qWaitFor(). (We no longer care about
+ * the bug in MSVC < 2017 that precluded using qWaitFor() in the implementation
+ * here, see QTBUG-59096.)
*/
// NB: not do {...} while (0) wrapped, as qt_test_i is accessed after it
@@ -159,14 +160,14 @@ inline void useVerifyThrowsException() {}
QTest::qWait(0); \
} \
int qt_test_i = 0; \
- for (; qt_test_i < timeoutValue && !QTest::currentTestFailed() \
+ for (; qt_test_i < timeoutValue && !(QTest::runningTest() && QTest::currentTestFailed()) \
&& !(expr); qt_test_i += step) { \
QTest::qWait(step); \
}
// Ends in a for-block, so doesn't want a following semicolon.
#define QTRY_TIMEOUT_DEBUG_IMPL(expr, timeoutValue, step) \
- if (!QTest::currentTestFailed() && !(expr)) { \
+ if (!(QTest::runningTest() && QTest::currentTestFailed()) && !(expr)) { \
QTRY_LOOP_IMPL(expr, 2 * (timeoutValue), step) \
if ((expr)) { \
QFAIL(qPrintable(QTest::Internal::formatTryTimeoutDebugMessage(\
@@ -419,6 +420,7 @@ namespace QTest
Q_TESTLIB_EXPORT const char *currentTestFunction();
Q_TESTLIB_EXPORT const char *currentDataTag();
Q_TESTLIB_EXPORT bool currentTestFailed();
+ Q_TESTLIB_EXPORT bool runningTest(); // Internal, for use by macros and QTestEventLoop.
Q_TESTLIB_EXPORT Qt::Key asciiToKey(char ascii);
Q_TESTLIB_EXPORT char keyToAscii(Qt::Key key);
diff --git a/src/testlib/qtesteventloop.h b/src/testlib/qtesteventloop.h
index a8126bc333..2ac8bdecdf 100644
--- a/src/testlib/qtesteventloop.h
+++ b/src/testlib/qtesteventloop.h
@@ -60,7 +60,7 @@ inline void QTestEventLoop::enterLoopMSecs(int ms)
Q_ASSERT(!loop);
_timeout = false;
- if (QTest::currentTestFailed())
+ if (QTest::runningTest() && QTest::currentTestFailed())
return;
QEventLoop l;