how to verify QVERIFY ?
-
I cannot find decent description of this macro nor where it outputs when it fails. I did test of one and if "statement" is preceded with (!) - thus forcing failure , I get same results , no output to console.
I like to redirect the output to regular list widget .
#define QVERIFY(statement)
do {
if (!QTest::qVerify(static_cast<bool>(statement), #statement, "", FILE, LINE))
return;
} while (false) -
QVERIFY is used in unit tests and will do what described in the documentation: "The QVERIFY() macro checks whether the condition is true or not. If it is true, execution continues. If not, a failure is recorded in the test log and the test won't be executed further."
-
@AnneRanch
As the code shows and @Christian-Ehrlicher has said,QVERIFY()
does not return any result for you see/test. It just callsQTest::qVerify()
for you, and exits the function you are in if that fails (returns false).Depending on what you want here in your code, if the intention is this must be the case, and if it is not print an error message and exit my whole program --- i.e. you use this while developing, and if it does occur you will fix the code so that it won't happen --- don't forget there is
Q_ASSERT(expressionWhichMustBeTrue)
for that.@Christian-Ehrlicher
I am interested in the choice of definition for#define QVERIFY(statement) do { if (!QTest::qVerify(static_cast<bool>(statement), #statement, "", FILE, LINE)) return; } while (false)
I understand why they are writing a standalone statement for this. I understand they must not write a "naked"-
if
for it, because there might be anelse
in user's code immediately after it. However, why do they bother with thatdo ... while
construct to achieve it? Why is the following, simpler statement not good enough?#define QVERIFY(statement) { if (!QTest::qVerify(static_cast<bool>(statement), #statement, "", FILE, LINE)) return; }
-
@JonB: See e.g. the discussion here: https://stackoverflow.com/questions/2314066/do-whilefalse
-
@Christian-Ehrlicher
Interesting, thank you. Most of the answers, including the "accepted solution", there say to allowbreak
orcontinue
in the body, which of course is not being used in this macro. The presumably "correct" answer there for this macro is https://stackoverflow.com/a/2314201/489865In a macro, OTOH,
do { something; } while (false)
is a convenient way to FORCE a semicolon after the macro invocation, absolutely no other token is allowed to follow -
So I made few "mistakes" - searching for "QVERIFY" instead of "QTest".
Secondly I missed "it writes to the log", hence I need to find out something about this log.
However , what I gather form this discussion - when the QVERIFY fails it basically exits the process.
Hence it is back to analyzing the so far unknown "test log"...
This may be desirable debugging , but definitely not KISS approach , in my opinion. -
@AnneRanch said in how to verify QVERIFY ?:
when the QVERIFY fails it basically exits the process.
That seems to depend on https://doc.qt.io/qt-5/qtest.html#TestFailMode-enum.
The test log will presumably just state that the
QTest::qVerify
failed at the specified line number in the specified file. Similar to aQ_ASSERT
.Hence it is back to analyzing the so far unknown "test log"...
I have not used
QTest
anything, but in the Qt Test Overview docs https://doc.qt.io/qt-5/qtest-overview.html#logging-options seems to show a-o
option which determines where the test log file is output?