Qt 4.8
Namespaces | Macros | Functions
qtestcase.h File Reference
#include <QtTest/qtest_global.h>
#include <QtCore/qnamespace.h>
#include <QtCore/qmetatype.h>

Go to the source code of this file.

Namespaces

 QTest
 The QTest namespace contains all the functions and declarations that are related to the QTestLib tool.
 

Macros

#define DEPENDS_ON(funcName)
 
#define QCOMPARE(actual, expected)
 The QCOMPARE macro compares an actual value to an expected value using the equals operator. More...
 
#define QEXPECT_FAIL(dataIndex, comment, mode)
 The QEXPECT_FAIL() macro marks the next QCOMPARE() or QVERIFY() as an expected failure. More...
 
#define QFAIL(message)
 This macro can be used to force a test failure. More...
 
#define QFETCH(type, name)   type name = *static_cast<type *>(QTest::qData(#name, ::qMetaTypeId<type >()))
 The fetch macro creates a local variable named name with the type type on the stack. More...
 
#define QFETCH_GLOBAL(type, name)   type name = *static_cast<type *>(QTest::qGlobalData(#name, ::qMetaTypeId<type >()))
 
#define QSKIP(statement, mode)
 The QSKIP() macro stops execution of the test without adding a failure to the test log. More...
 
#define QTEST(actual, testElement)
 QTEST() is a convenience macro for QCOMPARE() that compares the value actual with the element testElement from the test's data. More...
 
#define QTEST_COMPARE_DECL(KLASS)   template<> Q_TESTLIB_EXPORT char *toString<KLASS >(const KLASS &);
 
#define QVERIFY(statement)
 The QVERIFY() macro checks whether the condition is true or not. More...
 
#define QVERIFY2(statement, description)
 The QVERIFY2() macro behaves exactly like QVERIFY(), except that it outputs a verbose message when condition is false. More...
 
#define QWARN(msg)   QTest::qWarn(msg)
 Appends message as a warning to the test log. More...
 

Functions

template<typename T >
void QTest::addColumn (const char *name, T *=0)
 Adds a column with type T to the current test data. More...
 
Q_TESTLIB_EXPORT void QTest::addColumnInternal (int id, const char *name)
 
Q_TESTLIB_EXPORT Qt::Key QTest::asciiToKey (char ascii)
 Convert an ascii char key value to a Qt Key value. More...
 
Q_TESTLIB_EXPORT bool QTest::compare_helper (bool success, const char *msg, const char *file, int line)
 
Q_TESTLIB_EXPORT bool QTest::compare_helper (bool success, const char *msg, char *val1, char *val2, const char *expected, const char *actual, const char *file, int line)
 
bool QTest::compare_ptr_helper (const void *t1, const void *t2, const char *actual, const char *expected, const char *file, int line)
 
Q_TESTLIB_EXPORT bool QTest::compare_string_helper (const char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line)
 
Q_TESTLIB_EXPORT const char * QTest::currentAppName ()
 Returns the name of the binary that is currently executed. More...
 
Q_TESTLIB_EXPORT const char * QTest::currentDataTag ()
 Returns the name of the current test data. More...
 
Q_TESTLIB_EXPORT bool QTest::currentTestFailed ()
 Returns true if the current test function failed, otherwise false. More...
 
Q_TESTLIB_EXPORT const char * QTest::currentTestFunction ()
 Returns the name of the test function that is currently executed. More...
 
Q_TESTLIB_EXPORT void QTest::ignoreMessage (QtMsgType type, const char *message)
 Ignores messages created by qDebug() or qWarning(). More...
 
Q_TESTLIB_EXPORT char QTest::keyToAscii (Qt::Key key)
 Convert a Qt Key to an ascii char value. More...
 
Q_TESTLIB_EXPORT QTestDataQTest::newRow (const char *dataTag)
 Appends a new row to the current test data. More...
 
template<typename T >
bool QTest::qCompare (T const &t1, T const &t2, const char *actual, const char *expected, const char *file, int line)
 
template<typename T1 , typename T2 >
bool QTest::qCompare (T1 const &, T2 const &, const char *, const char *, const char *, int)
 
template<typename T >
bool QTest::qCompare (const T *t1, const T *t2, const char *actual, const char *expected, const char *file, int line)
 
template<typename T >
bool QTest::qCompare (T *t1, T *t2, const char *actual, const char *expected, const char *file, int line)
 
template<typename T1 , typename T2 >
bool QTest::qCompare (const T1 *t1, const T2 *t2, const char *actual, const char *expected, const char *file, int line)
 
template<typename T1 , typename T2 >
bool QTest::qCompare (T1 *t1, T2 *t2, const char *actual, const char *expected, const char *file, int line)
 
template<>
bool QTest::qCompare (char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line)
 
template<>
bool QTest::qCompare (const char *t1, char *t2, const char *actual, const char *expected, const char *file, int line)
 
template<>
bool QTest::qCompare (bool const &t1, int const &t2, const char *actual, const char *expected, const char *file, int line)
 
template<>
bool QTest::qCompare< char > (const char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line)
 
template<>
bool QTest::qCompare< char > (char *t1, char *t2, const char *actual, const char *expected, const char *file, int line)
 
template<>
Q_TESTLIB_EXPORT bool QTest::qCompare< double > (double const &t1, double const &t2, const char *actual, const char *expected, const char *file, int line)
 
template<>
Q_TESTLIB_EXPORT bool QTest::qCompare< float > (float const &t1, float const &t2, const char *actual, const char *expected, const char *file, int line)
 
Q_TESTLIB_EXPORT void * QTest::qData (const char *tagName, int typeId)
 
Q_TESTLIB_EXPORT void * QTest::qElementData (const char *elementName, int metaTypeId)
 
Q_TESTLIB_EXPORT int QTest::qExec (QObject *testObject, int argc=0, char **argv=0)
 Executes tests declared in testObject. More...
 
Q_TESTLIB_EXPORT int QTest::qExec (QObject *testObject, const QStringList &arguments)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
Q_TESTLIB_EXPORT bool QTest::qExpectFail (const char *dataIndex, const char *comment, TestFailMode mode, const char *file, int line)
 
Q_TESTLIB_EXPORT void QTest::qFail (const char *statementStr, const char *file, int line)
 
Q_TESTLIB_EXPORT void * QTest::qGlobalData (const char *tagName, int typeId)
 
Q_TESTLIB_EXPORT void QTest::qSkip (const char *message, SkipMode mode, const char *file, int line)
 
Q_TESTLIB_EXPORT void QTest::qSleep (int ms)
 Sleeps for ms milliseconds, blocking execution of the test. More...
 
template<class T >
bool QTest::qTest (const T &actual, const char *elementName, const char *actualStr, const char *expected, const char *file, int line)
 
Q_TESTLIB_EXPORT bool QTest::qVerify (bool statement, const char *statementStr, const char *description, const char *file, int line)
 
Q_TESTLIB_EXPORT void QTest::qWarn (const char *message)
 
Q_TESTLIB_EXPORT QObjectQTest::testObject ()
 
char * QTest::toHexRepresentation (const char *ba, int length)
 Returns a pointer to a string that is the string ba represented as a space-separated sequence of hex characters. More...
 
template<typename T >
char * QTest::toString (const T &)
 Returns a textual representation of value. More...
 
Q_TESTLIB_EXPORT char * QTest::toString (const char *)
 
Q_TESTLIB_EXPORT char * QTest::toString (const void *)
 

Macro Definition Documentation

◆ DEPENDS_ON

#define DEPENDS_ON (   funcName)

Definition at line 103 of file qtestcase.h.

◆ QCOMPARE

#define QCOMPARE (   actual,
  expected 
)
Value:
do {\
if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\
return;\
} while (0)
bool qCompare(QString const &t1, QLatin1String const &t2, const char *actual, const char *expected, const char *file, int line)
Definition: qtest.h:177

The QCOMPARE macro compares an actual value to an expected value using the equals operator.

If actual and expected are identical, execution continues. If not, a failure is recorded in the test log and the test won't be executed further.

In the case of comparing floats and doubles, qFuzzyCompare() is used for comparing. This means that comparing to 0 will likely fail. One solution to this is to compare to 1, and add 1 to the produced output.

QCOMPARE tries to output the contents of the values if the comparison fails, so it is visible from the test log why the comparison failed.

QCOMPARE is very strict on the data types. Both actual and expected have to be of the same type, otherwise the test won't compile. This prohibits unspecified behavior from being introduced; that is behavior that usually occurs when the compiler implicitly casts the argument.

If you use QCOMPARE() to compare two QStringList objects, it will start comparing the objects from the end of the lists.

For your own classes, you can use QTest::toString() to format values for outputting into the test log.

Note
This macro can only be used in a test function that is invoked by the test framework.

Example:

QCOMPARE(QString("hello").toUpper(), QString("HELLO"));
See also
QVERIFY(), QTest::toString()

Definition at line 79 of file qtestcase.h.

◆ QEXPECT_FAIL

#define QEXPECT_FAIL (   dataIndex,
  comment,
  mode 
)
Value:
do {\
if (!QTest::qExpectFail(dataIndex, comment, QTest::mode, __FILE__, __LINE__))\
return;\
} while (0)
Q_TESTLIB_EXPORT bool qExpectFail(const char *dataIndex, const char *comment, TestFailMode mode, const char *file, int line)
Definition: qtestcase.cpp:2094

The QEXPECT_FAIL() macro marks the next QCOMPARE() or QVERIFY() as an expected failure.

Instead of adding a failure to the test log, an expected failure will be reported.

If a QVERIFY() or QCOMPARE() is marked as an expected failure, but passes instead, an unexpected pass (XPASS) is written to the test log.

The parameter dataIndex describes for which entry in the test data the failure is expected. Pass an empty string ("") if the failure is expected for all entries or if no test data exists.

comment will be appended to the test log for the expected failure.

mode is a QTest::TestFailMode and sets whether the test should continue to execute or not.

Note: This macro can only be used in a test function that is invoked by the test framework.

Example 1:

QEXPECT_FAIL("", "Will fix in the next release", Continue);
QCOMPARE(i, 42);
QCOMPARE(j, 43);

In the example above, an expected fail will be written into the test output if the variable i is not 42. If the variable i is 42, an unexpected pass is written instead. The QEXPECT_FAIL() has no influence on the second QCOMPARE() statement in the example.

Example 2:

QEXPECT_FAIL("data27", "Oh my, this is soooo broken", Abort);
QCOMPARE(i, 42);

The above testfunction will not continue executing for the test data entry data27.

See also
QTest::TestFailMode, QVERIFY(), QCOMPARE()

Definition at line 91 of file qtestcase.h.

◆ QFAIL

#define QFAIL (   message)
Value:
do {\
QTest::qFail(message, __FILE__, __LINE__);\
return;\
} while (0)
Q_TESTLIB_EXPORT void qFail(const char *statementStr, const char *file, int line)
Definition: qtestcase.cpp:2067

This macro can be used to force a test failure.

The test stops executing and the failure message is appended to the test log.

Note: This macro can only be used in a test function that is invoked by the test framework.

Example:

if (sizeof(int) != 4)
QFAIL("This test has not been ported to this platform yet.");

Definition at line 62 of file qtestcase.h.

◆ QFETCH

#define QFETCH (   type,
  name 
)    type name = *static_cast<type *>(QTest::qData(#name, ::qMetaTypeId<type >()))

The fetch macro creates a local variable named name with the type type on the stack.

name has to match the element name from the test's data. If no such element exists, the test will assert.

Assuming a test has the following data:

void TestQString::toInt_data()
{
QTest::addColumn<QString>("aString");
QTest::addColumn<int>("expected");
QTest::newRow("positive value") << "42" << 42;
QTest::newRow("negative value") << "-42" << -42;
QTest::newRow("zero") << "0" << 0;
}

The test data has two elements, a QString called aString and an integer called expected. To fetch these values in the actual test:

{
QFETCH(QString, aString);
QFETCH(int, expected);
QCOMPARE(aString.toInt(), expected);
}

aString and expected are variables on the stack that are initialized with the current test data.

Note: This macro can only be used in a test function that is invoked by the test framework. The test function must have a _data function.

Definition at line 97 of file qtestcase.h.

◆ QFETCH_GLOBAL

#define QFETCH_GLOBAL (   type,
  name 
)    type name = *static_cast<type *>(QTest::qGlobalData(#name, ::qMetaTypeId<type >()))

Definition at line 100 of file qtestcase.h.

◆ QSKIP

#define QSKIP (   statement,
  mode 
)
Value:
do {\
QTest::qSkip(statement, QTest::mode, __FILE__, __LINE__);\
return;\
} while (0)
Q_TESTLIB_EXPORT void qSkip(const char *message, SkipMode mode, const char *file, int line)
Definition: qtestcase.cpp:2083

The QSKIP() macro stops execution of the test without adding a failure to the test log.

You can use it to skip tests that wouldn't make sense in the current configuration. The text description is appended to the test log and should contain an explanation why the test couldn't be executed. mode is a QTest::SkipMode and describes whether to proceed with the rest of the test data or not.

Note: This macro can only be used in a test function that is invoked by the test framework.

Example:

if (!QSqlDatabase::drivers().contains("SQLITE"))
QSKIP("This test requires the SQLITE database driver", SkipAll);
See also
QTest::SkipMode

Definition at line 85 of file qtestcase.h.

◆ QTEST

#define QTEST (   actual,
  testElement 
)
Value:
do {\
if (!QTest::qTest(actual, testElement, #actual, #testElement, __FILE__, __LINE__))\
return;\
} while (0)
bool qTest(const T &actual, const char *elementName, const char *actualStr, const char *expected, const char *file, int line)
Definition: qtestcase.h:363

QTEST() is a convenience macro for QCOMPARE() that compares the value actual with the element testElement from the test's data.

If there is no such element, the test asserts.

Apart from that, QTEST() behaves exactly as QCOMPARE() .

Instead of writing:

QFETCH(QString, myString);
QCOMPARE(QString("hello").toUpper(), myString);

you can write:

QTEST(QString("hello").toUpper(), "myString");
See also
QCOMPARE()

Definition at line 105 of file qtestcase.h.

◆ QTEST_COMPARE_DECL

#define QTEST_COMPARE_DECL (   KLASS)    template<> Q_TESTLIB_EXPORT char *toString<KLASS >(const KLASS &);

Definition at line 117 of file qtestcase.h.

Referenced by QTest::compare_ptr_helper().

◆ QVERIFY

#define QVERIFY (   statement)
Value:
do {\
if (!QTest::qVerify((statement), #statement, "", __FILE__, __LINE__))\
return;\
} while (0)
Q_TESTLIB_EXPORT bool qVerify(bool statement, const char *statementStr, const char *description, const char *file, int line)
Definition: qtestcase.cpp:2074

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.

Note: This macro can only be used in a test function that is invoked by the test framework.

Example:

QVERIFY(1 + 1 == 2);
See also
QCOMPARE()

Definition at line 56 of file qtestcase.h.

◆ QVERIFY2

#define QVERIFY2 (   statement,
  description 
)
Value:
do {\
if (statement) {\
if (!QTest::qVerify(true, #statement, (description), __FILE__, __LINE__))\
return;\
} else {\
if (!QTest::qVerify(false, #statement, (description), __FILE__, __LINE__))\
return;\
}\
} while (0)
Q_TESTLIB_EXPORT bool qVerify(bool statement, const char *statementStr, const char *description, const char *file, int line)
Definition: qtestcase.cpp:2074

The QVERIFY2() macro behaves exactly like QVERIFY(), except that it outputs a verbose message when condition is false.

The message is a plain C string.

Example:

QVERIFY2(1 + 1 == 2, "A breach in basic arithmetic occurred.");
See also
QVERIFY(), QCOMPARE()

Definition at line 68 of file qtestcase.h.

◆ QWARN

#define QWARN (   msg)    QTest::qWarn(msg)

Appends message as a warning to the test log.

Note
This class or function is threadsafe.

This macro can be used anywhere in your tests.

Definition at line 111 of file qtestcase.h.