1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtCore/QCoreApplication>
#include <QtTest/QtTest>
Q_DECLARE_METATYPE(QTest::TestFailMode)
class tst_ExpectFail: public QObject
{
Q_OBJECT
private slots:
void xfailAndContinue() const;
void xfailAndAbort() const;
void xfailTwice() const;
void xfailWithQString() const;
void xfailDataDrivenWithQVerify_data() const;
void xfailDataDrivenWithQVerify() const;
void xfailDataDrivenWithQCompare_data() const;
void xfailDataDrivenWithQCompare() const;
void xfailOnWrongRow_data() const;
void xfailOnWrongRow() const;
void xfailOnAnyRow_data() const;
void xfailOnAnyRow() const;
void xfailWithoutVerify_data() const;
void xfailWithoutVerify() const;
void xpass() const;
void xpassDataDrivenWithQVerify_data() const;
void xpassDataDrivenWithQVerify() const;
void xpassDataDrivenWithQCompare_data() const;
void xpassDataDrivenWithQCompare() const;
};
void tst_ExpectFail::xfailAndContinue() const
{
qDebug("begin");
QEXPECT_FAIL("", "This should xfail", Continue);
QVERIFY(false);
qDebug("after");
}
void tst_ExpectFail::xfailAndAbort() const
{
qDebug("begin");
QEXPECT_FAIL("", "This should xfail", Abort);
QVERIFY(false);
// If we get here the test did not correctly abort on the previous QVERIFY.
QVERIFY2(false, "This should not be reached");
}
void tst_ExpectFail::xfailTwice() const
{
QEXPECT_FAIL("", "Calling QEXPECT_FAIL once is fine", Abort);
QEXPECT_FAIL("", "Calling QEXPECT_FAIL when already expecting a failure is "
"an error and should abort this test function", Abort);
// If we get here the test did not correctly abort on the double call to QEXPECT_FAIL.
QVERIFY2(false, "This should not be reached");
}
void tst_ExpectFail::xfailWithQString() const
{
QEXPECT_FAIL("", QString("A string").toLatin1().constData(), Continue);
QVERIFY(false);
int bugNo = 5;
QString msg("The message");
QEXPECT_FAIL( "", QString("Bug %1 (%2)").arg(bugNo).arg(msg).toLatin1().constData(), Continue);
QVERIFY(false);
}
void tst_ExpectFail::xfailDataDrivenWithQVerify_data() const
{
QTest::addColumn<bool>("shouldPass");
QTest::addColumn<QTest::TestFailMode>("failMode");
QTest::newRow("Pass 1") << true << QTest::Abort;
QTest::newRow("Pass 2") << true << QTest::Continue;
QTest::newRow("Abort") << false << QTest::Abort;
QTest::newRow("Continue") << false << QTest::Continue;
}
void tst_ExpectFail::xfailDataDrivenWithQVerify() const
{
QFETCH(bool, shouldPass);
QFETCH(QTest::TestFailMode, failMode);
// You can't pass a variable as the last parameter of QEXPECT_FAIL,
// because the macro adds "QTest::" in front of the last parameter.
// That is why the following code appears to be a little strange.
if (!shouldPass) {
if (failMode == QTest::Abort)
QEXPECT_FAIL(QTest::currentDataTag(), "This test should xfail", Abort);
else
QEXPECT_FAIL(QTest::currentDataTag(), "This test should xfail", Continue);
}
QVERIFY(shouldPass);
// If we get here, we either expected to pass or we expected to
// fail and the failure mode was Continue.
if (!shouldPass)
QCOMPARE(failMode, QTest::Continue);
}
void tst_ExpectFail::xfailDataDrivenWithQCompare_data() const
{
QTest::addColumn<bool>("shouldPass");
QTest::addColumn<QTest::TestFailMode>("failMode");
QTest::newRow("Pass 1") << true << QTest::Abort;
QTest::newRow("Pass 2") << true << QTest::Continue;
QTest::newRow("Abort") << false << QTest::Abort;
QTest::newRow("Continue") << false << QTest::Continue;
}
void tst_ExpectFail::xfailDataDrivenWithQCompare() const
{
QFETCH(bool, shouldPass);
QFETCH(QTest::TestFailMode, failMode);
// You can't pass a variable as the last parameter of QEXPECT_FAIL,
// because the macro adds "QTest::" in front of the last parameter.
// That is why the following code appears to be a little strange.
if (!shouldPass) {
if (failMode == QTest::Abort)
QEXPECT_FAIL(QTest::currentDataTag(), "This test should xfail", Abort);
else
QEXPECT_FAIL(QTest::currentDataTag(), "This test should xfail", Continue);
}
QCOMPARE(1, shouldPass ? 1 : 2);
// If we get here, we either expected to pass or we expected to
// fail and the failure mode was Continue.
if (!shouldPass)
QCOMPARE(failMode, QTest::Continue);
}
void tst_ExpectFail::xfailOnWrongRow_data() const
{
QTest::addColumn<int>("dummy");
QTest::newRow("right row") << 0;
}
void tst_ExpectFail::xfailOnWrongRow() const
{
// QEXPECT_FAIL for a row that is not the current row should be ignored.
QEXPECT_FAIL("wrong row", "This xfail should be ignored", Abort);
QVERIFY(true);
}
void tst_ExpectFail::xfailOnAnyRow_data() const
{
QTest::addColumn<int>("dummy");
QTest::newRow("first row") << 0;
QTest::newRow("second row") << 1;
}
void tst_ExpectFail::xfailOnAnyRow() const
{
// In a data-driven test, passing an empty first parameter to QEXPECT_FAIL
// should mean that the failure is expected for all data rows.
QEXPECT_FAIL("", "This test should xfail", Abort);
QVERIFY(false);
}
void tst_ExpectFail::xfailWithoutVerify_data() const
{
QTest::addColumn<int>("dummy");
QTest::newRow("first row") << 0;
QTest::newRow("second row") << 1;
}
void tst_ExpectFail::xfailWithoutVerify() const
{
QVERIFY(true);
QEXPECT_FAIL("", "This expected failure should be ignored", Abort);
}
void tst_ExpectFail::xpass() const
{
QEXPECT_FAIL("", "This test should xpass", Abort);
QVERIFY(true);
// If we get here the test did not correctly abort on the previous
// unexpected pass.
QVERIFY2(false, "This should not be reached");
}
void tst_ExpectFail::xpassDataDrivenWithQVerify_data() const
{
QTest::addColumn<bool>("shouldXPass");
QTest::newRow("XPass") << true;
QTest::newRow("Pass") << false;
}
void tst_ExpectFail::xpassDataDrivenWithQVerify() const
{
QFETCH(bool, shouldXPass);
if (shouldXPass)
QEXPECT_FAIL(QTest::currentDataTag(), "This test should xpass", Abort);
QVERIFY(true);
// We should only get here if the test wasn't supposed to xpass.
QVERIFY2(!shouldXPass, "Test failed to terminate on XPASS");
}
void tst_ExpectFail::xpassDataDrivenWithQCompare_data() const
{
QTest::addColumn<bool>("shouldXPass");
QTest::newRow("XPass") << true;
QTest::newRow("Pass") << false;
}
void tst_ExpectFail::xpassDataDrivenWithQCompare() const
{
QFETCH(bool, shouldXPass);
if (shouldXPass)
QEXPECT_FAIL(QTest::currentDataTag(), "This test should xpass", Abort);
QCOMPARE(1, 1);
// We should only get here if the test wasn't supposed to xpass.
QVERIFY2(!shouldXPass, "Test failed to terminate on XPASS");
}
QTEST_MAIN(tst_ExpectFail)
#include "tst_expectfail.moc"
|