6.0.0-rc4Source

QTest Namespace

The QTest namespace holds the functions behind Nt Test. More…

Header#include <QTest>
CMaketarget_link_libraries(app PRIVATE Nt::Test)
ModuleNt Test C++ Classes

Public Types

Public Functions

intqExec(QObject *testObject, int argc = 0, char **argv = nullptr)
intqExec(QObject *testObject, const QStringList &arguments)
voidaddColumn(const char *name)
QTestData &newRow(const char *dataTag)
QTestData &addRow(const char *format, ...)
const char *currentAppName()
const char *currentTestFunction()
const char *currentDataTag()
const char *currentGlobalDataTag()
boolcurrentTestFailed()
voidignoreMessage(QtMsgType type, const char *message)
voidignoreMessage(QtMsgType type, const QRegularExpression &messagePattern)
voidqSleep(int ms)
voidqWait(int ms)
voidqWait(std::chrono::milliseconds time)
boolqWaitFor(Functor predicate, int timeout = 5000)
boolqWaitFor(Functor predicate, std::chrono::milliseconds timeout)
QStringqFindTestData(const QString &base, const char *file = nullptr, int line = 0)
char *toString(const char *value)
char *toString(std::nullptr_t)
char *toString(bool value)
char *toString(char value)
char *toString(const QString &value)
char *toString(QStringView value)
char *toString(QLatin1StringView value)
char *toString(const QStringList &value)
char *toString(const QByteArray &value)
char *toString(QChar value)
char *toString(const QDate &value)
char *toString(const QTime &value)
char *toString(const QDateTime &value)
char *toString(const QUrl &value)
char *toString(const QUuid &value)
char *toString(const QVersionNumber &value)
char *toString(const T &value)
char *toString(const QList<T> &value)
char *toString(const std::pair<T1, T2> &value)
char *toString(const std::tuple<Types...> &value)
boolqVerify(bool statement, const char *statementText, const char *description, const char *file, int line)
voidqFail(const char *message, const char *file, int line)
voidqSkip(const char *message, const char *file, int line)
boolqExpectFail(const char *dataTag, const char *comment, TestFailMode mode, const char *file, int line)
boolqCompare(const T1 &t1, const T2 &t2, const char *actual, const char *expected, const char *file, int line)
boolqCompare(const QList<T> &t1, const QList<T> &t2, const char *actual, const char *expected, const char *file, int line)
boolqTest(const T &actual, const char *elementName, const char *actualText, const char *expected, const char *file, int line)

Detailed Description

Differences from Qt

There are no GUI event functions, no benchmarks (QBENCHMARK), no failOnWarning() and no QTest::Throw. Regular expressions are not accepted by ignoreMessage(), since Nt has no QRegularExpression. Test classes must have gone through ntmoc, and must be outside anonymous namespaces and not templates. ntmoc does not evaluate preprocessor conditionals, so a private slot inside #if must exist in every configuration. Fatal signals and hung tests are not caught.

Between rows, all pending events are processed to run deleteLater(), not only deferred deletes. Test macros must be used from the main thread.

Member Type Documentation

enum QTest::TestFailMode

This enum says what happens after a failure announced by QEXPECT_FAIL.

ConstantDescription
QTest::AbortThe test function stops, as it does after an unexpected failure.
QTest::ContinueThe test function goes on with the statement after the failure.

Function Documentation

template <typename T>void QTest::addColumn(const char *name)

Adds a column of type T called name to the test data table. Call it from a _data function, before the first row. QFETCH() reads the column by this name in the test function.

See also newRow() and addRow().

QTestData & QTest::addRow(const char *format, ...)

Adds a row to the test data table, with a tag built from the printf style format and the arguments after it, and returns the row.

for (int i = 0; i < 4; ++i)
    QTest::addRow("size-%d", i) << i;

See also newRow().

const char * QTest::currentAppName()

Returns the file name of the test program, or the class name if qExec() was not given a command line. Returns an empty string before the first call of qExec().

const char * QTest::currentDataTag()

Returns the tag of the current data row, or nullptr if there is none.

See also currentGlobalDataTag().

const char * QTest::currentGlobalDataTag()

Returns the tag of the current row of initTestCase_data(), or nullptr if there is none.

See also currentDataTag().

bool QTest::currentTestFailed()

Returns true if a check has failed in the current data row. Use it after calling a helper function that uses QVERIFY() or QCOMPARE(), as they return only from the helper.

const char * QTest::currentTestFunction()

Returns the name of the running test function, or nullptr outside qExec().

void QTest::ignoreMessage(QtMsgType type, const char *message)

Expects the next message of the given type to be message, and keeps it out of the log. Call it again for each further message. The current data row fails if the message does not arrive before it ends.

message must match the whole text.

void QTest::ignoreMessage(QtMsgType type, const QRegularExpression &messagePattern)

This is an overloaded function.

Expects the next message of the given type to match the regular expression messagePattern, anywhere in its text, and keeps it out of the log. Anchor the pattern to match the whole text.

QTestData & QTest::newRow(const char *dataTag)

Adds a row called dataTag to the test data table and returns it, to fill in with operator<<. Call it from a _data function, after the columns. The tag names the row in the log and on the command line.

See also addRow() and addColumn().

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)

Compares t1 with t2 and reports the result for the current test function; actual and expected are their source text, and file and line the place of the comparison. Returns false if the test function must stop. QCOMPARE() calls it.

C strings are compared by content, integers by value whatever their signedness, and floating point numbers with a tolerance, treating two NaNs as equal. Everything else uses operator==.

template <typename T>bool QTest::qCompare(const QList<T> &t1, const QList<T> &t2, const char *actual, const char *expected, const char *file, int line)

This is an overloaded function.

Compares the lists t1 and t2 item by item, so that the failure message names the first item that differs. actual, expected, file and line are as for the general function.

int QTest::qExec(QObject *testObject, int argc = 0, char **argv = nullptr)

Runs the test functions of testObject and returns the number of failures, up to 127. argc and argv hold the command line described in Writing Tests with Nt Test.

Returns 1 without running anything if the arguments are wrong or ntmoc has not seen the class of testObject. It may be called more than once, but not while another call is running.

int QTest::qExec(QObject *testObject, const QStringList &arguments)

This is an overloaded function.

Runs testObject with the command line in arguments, the first item of which is the program name.

bool QTest::qExpectFail(const char *dataTag, const char *comment, TestFailMode mode, const char *file, int line)

Announces that the next check fails for the row dataTag, or for all rows if it is empty, because of comment. mode says what happens after it. Returns false if the test function must stop, which is when a failure is already expected at line of file. QEXPECT_FAIL() calls it.

void QTest::qFail(const char *message, const char *file, int line)

Reports a failure of the current test function with message, at line of file. QFAIL() calls it and then returns.

QString QTest::qFindTestData(const QString &base, const char *file = nullptr, int line = 0)

Returns the absolute path of the test data file base, or an empty string, after a warning, if it does not exist. Looks next to the test program, in the current directory and in the directory of the source file file, in that order. QFINDTESTDATA() passes file and line, and line is only there for the warning.

void QTest::qSkip(const char *message, const char *file, int line)

Marks the current data row as skipped, because of message, at line of file. QSKIP() calls it and then returns.

void QTest::qSleep(int ms)

Blocks for ms milliseconds without processing events.

See also qWait().

template <typename T>bool QTest::qTest(const T &actual, const char *elementName, const char *actualText, const char *expected, const char *file, int line)

Compares actual with the cell called elementName of the current data row and reports the result. file and line give the place of the comparison, actualText is the source text of actual and expected the text shown for the cell. Returns false if the test function must stop. QTEST() calls it.

bool QTest::qVerify(bool statement, const char *statementText, const char *description, const char *file, int line)

Reports statement, the result of the check statementText at line of file, for the current test function, with description added to a failure. Returns false if the test function must stop. QVERIFY() and QVERIFY2() call it.

void QTest::qWait(int ms)

Waits for ms milliseconds, processing events meanwhile. Without a QCoreApplication it only sleeps.

See also qWaitFor() and qSleep().

void QTest::qWait(std::chrono::milliseconds time)

This is an overloaded function.

Waits for time, processing events meanwhile.

template <typename Functor>bool QTest::qWaitFor(Functor predicate, int timeout = 5000)

Calls predicate repeatedly, processing events in between, until it returns true or timeout milliseconds have passed. Returns the last value it returned. The default timeout is five seconds.

See also qWait().

template <typename Functor>bool QTest::qWaitFor(Functor predicate, std::chrono::milliseconds timeout)

This is an overloaded function.

Waits for predicate for at most timeout.

char * QTest::toString(const char *value)

This is an overloaded function.

Returns value in double quotes, with quotes and control characters escaped, or nullptr as text for a null pointer.

char * QTest::toString(std::nullptr_t)

This is an overloaded function.

Returns the text nullptr.

char * QTest::toString(bool value)

This is an overloaded function.

Returns true or false as text.

char * QTest::toString(char value)

This is an overloaded function.

Returns the character value in double quotes, escaped when needed.

char * QTest::toString(const QString &value)

This is an overloaded function.

Returns value in double quotes, escaped when needed, as UTF-8.

char * QTest::toString(QStringView value)

This is an overloaded function.

Returns value in double quotes, escaped when needed, as UTF-8.

char * QTest::toString(QLatin1StringView value)

This is an overloaded function.

Returns value in double quotes, escaped when needed.

char * QTest::toString(const QStringList &value)

This is an overloaded function.

Returns the strings of value as QStringList("a", "b").

char * QTest::toString(const QByteArray &value)

This is an overloaded function.

Returns value in double quotes, with bytes that are not printable written as \xNN.

char * QTest::toString(QChar value)

This is an overloaded function.

Returns the character value in double quotes, escaped when needed.

char * QTest::toString(const QDate &value)

This is an overloaded function.

Returns value in ISO 8601 form, or Invalid QDate.

char * QTest::toString(const QTime &value)

This is an overloaded function.

Returns value as hh:mm:ss.zzz, or Invalid QTime.

char * QTest::toString(const QDateTime &value)

This is an overloaded function.

Returns value in ISO 8601 form, or Invalid QDateTime.

char * QTest::toString(const QUrl &value)

This is an overloaded function.

Returns the encoded form of value in double quotes.

char * QTest::toString(const QUuid &value)

This is an overloaded function.

Returns value in the form {00000000-0000-0000-0000-000000000000}.

char * QTest::toString(const QVersionNumber &value)

This is an overloaded function.

Returns value with its segments separated by dots.

template <typename T>char * QTest::toString(const T &value)

Returns a text form of value for a failure message, as an array allocated with new char[] that the caller deletes. Numbers, enums and pointers are covered here, and so are the Nt classes with an overload of this function. For anything else it returns nullptr and the failure message says that there is no text form.

To cover your own type, specialize this template, or better, add an overload next to the type, in its namespace:

char *toString(const Point &p)
{
    return qstrdup(QByteArray::number(p.x) + "," + QByteArray::number(p.y));
}

template <typename T>char * QTest::toString(const QList<T> &value)

Returns a text form of value, with each item converted by toString().

template <typename T1, typename T2>char * QTest::toString(const std::pair<T1, T2> &value)

Returns a text form of value, with both members converted by toString().

template <typename... Types>char * QTest::toString(const std::tuple<Types...> &value)

Returns a text form of value, with each member converted by toString().