diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2021-10-08 13:58:27 +0200 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@qt.io> | 2021-11-26 04:47:24 +0100 |
commit | efb283fb7f72e950c8ecf755b960a3c1b36b5507 (patch) | |
tree | 361930e1f30586e93f3b4165a9893cdd381d2517 /tests/auto/testlib/selftests/warnings | |
parent | 1edf153a6bc28e127531e852a0856025ca5d91b0 (diff) |
Add QTest::failOnWarning
This solves the long-standing problem of not being able to easily
fail a test when a certain warning is output.
[ChangeLog][QtTest] Added QTest::failOnWarning. When called in a test
function, any warning that matches the given pattern will cause a test
failure. The test will continue execution when a failure is added.
All patterns are cleared at the end of each test function.
Fixes: QTBUG-70029
Change-Id: I5763f8d4acf1cee8178be43a503619fbfb0f4f36
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'tests/auto/testlib/selftests/warnings')
-rw-r--r-- | tests/auto/testlib/selftests/warnings/tst_warnings.cpp | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/tests/auto/testlib/selftests/warnings/tst_warnings.cpp b/tests/auto/testlib/selftests/warnings/tst_warnings.cpp index 61f090c2c3..ee73bd4fe4 100644 --- a/tests/auto/testlib/selftests/warnings/tst_warnings.cpp +++ b/tests/auto/testlib/selftests/warnings/tst_warnings.cpp @@ -40,6 +40,16 @@ private slots: void testMissingWarningsRegularExpression(); void testMissingWarningsWithData_data(); void testMissingWarningsWithData(); + + void testFailOnWarnings(); + void testFailOnWarningsCleared(); +#if QT_CONFIG(regularexpression) + void testFailOnWarningsWithData_data(); + void testFailOnWarningsWithData(); + void testFailOnWarningsFailInHelper(); + void testFailOnWarningsThenSkip(); +#endif + void testFailOnWarningsAndIgnoreWarnings(); }; void tst_Warnings::testWarnings() @@ -124,6 +134,99 @@ void tst_Warnings::testMissingWarningsWithData() qWarning("Warning2"); } +void tst_Warnings::testFailOnWarnings() +{ + // failOnWarnings() wasn't called yet; shouldn't fail; + qWarning("Ran out of space!"); + +#if QT_CONFIG(regularexpression) + const auto warnRegex = QRegularExpression("Ran out of .*!"); + QTest::failOnWarning(warnRegex); + // Should now fail. + qWarning("Ran out of cabbage!"); + + // Should not fail; none of these are warnings. + qDebug("Ran out of tortillas!"); + qInfo("Ran out of oil!"); + + // Should not fail; regex doesn't match. + qWarning("nope"); + + // Should fail; matches regex. + qWarning("Ran out of biscuits!"); +#endif // QT_CONFIG(regularexpression) + + QTest::failOnWarning("Running low on toothpaste!"); + + // Should fail; strings match. + qWarning("Running low on toothpaste!"); + + // Shouldn't fail; strings don't match. + qWarning("Running low on flour!"); + + // Should not fail; none of these are warnings. + qDebug("Running low on toothpaste!"); + qInfo("Running low on toothpaste!"); +} + +void tst_Warnings::testFailOnWarningsCleared() +{ + // The patterns passed to failOnWarnings() should be cleared at the end of + // each test function, so this shouldn't fail because of the failOnWarning() call in the previous function. + // Note that this test always needs to come after testFailOnWarnings for it to work. + qWarning("Ran out of muffins!"); +} + +#if QT_CONFIG(regularexpression) +void tst_Warnings::testFailOnWarningsWithData_data() +{ + // The warning message that should cause a failure. + QTest::addColumn<QString>("warningMessage"); + + QTest::newRow("warning1") << "warning1"; + QTest::newRow("warning2") << "warning2"; + QTest::newRow("warning3") << "warning3"; +} + +void tst_Warnings::testFailOnWarningsWithData() +{ + QFETCH(QString, warningMessage); + + QTest::failOnWarning(QRegularExpression(warningMessage)); + + // Only one of these should fail, depending on warningMessage. + qWarning("warning1"); + qWarning("warning2"); + qWarning("warning3"); +} + +void tst_Warnings::testFailOnWarningsFailInHelper() +{ + [](){ QFAIL("This failure message should be printed but not cause the test to abort"); }(); + const auto warnRegex = QRegularExpression("Ran out of .*!"); + QTest::failOnWarning(warnRegex); + qWarning("Ran out of cabbage!"); + QFAIL("My cabbage! :("); +} + +void tst_Warnings::testFailOnWarningsThenSkip() +{ + const auto warnRegex = QRegularExpression("Ran out of .*!"); + QTest::failOnWarning(warnRegex); + qWarning("Ran out of cabbage!"); + QSKIP("My cabbage! :("); +} +#endif // QT_CONFIG(regularexpression) + +void tst_Warnings::testFailOnWarningsAndIgnoreWarnings() +{ + const auto warningStr = "Running low on toothpaste!"; + QTest::failOnWarning(warningStr); + QTest::ignoreMessage(QtWarningMsg, warningStr); + // Shouldn't fail; we ignored it. + qWarning(warningStr); +} + QTEST_MAIN(tst_Warnings) #include "tst_warnings.moc" |