QTest Namespace

The QTest namespace contains all the functions and declarations that are related to Qt Test. More...

Header: #include <QTest>
qmake: QT += testlib

Types

enum AttributeIndex { AI_Undefined, AI_Name, AI_Result, AI_Tests, ..., AI_Iterations }
enum LogElementType { LET_Undefined, LET_Property, LET_Properties, LET_Failure, ..., LET_SystemError }
enum QBenchmarkMetric { FramesPerSecond, BitsPerSecond, BytesPerSecond, WalltimeMilliseconds, ..., EmulationFaults }
enum TestFailMode { Abort, Continue }

Functions

void addColumn(const char *name, T * = nullptr)
Q_DECL_EXPORT addColumnInternal(int id, const char *name)
const char *benchmarkMetricName(QTest::QBenchmarkMetric metric)
const char *benchmarkMetricUnit(QTest::QBenchmarkMetric metric)
Q_DECL_EXPORT compare_helper(bool success, const char *failureMsg, char *val1, char *val2, const char *actual, const char *expected, const char *file, int line)
bool compare_ptr_helper(const volatile void *t1, const volatile void *t2, const char *actual, const char *expected, const char *file, int line)
bool compare_ptr_helper(const volatile void *t1, std::nullptr_t, const char *actual, const char *expected, const char *file, int line)
bool compare_ptr_helper(std::nullptr_t, const volatile void *t2, const char *actual, const char *expected, const char *file, int line)
Q_DECL_EXPORT compare_string_helper(const char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line)
const Q_DECL_EXPORT *currentAppName()
const Q_DECL_EXPORT *currentDataTag()
Q_DECL_EXPORT currentTestFailed()
const Q_DECL_EXPORT *currentTestFunction()
Q_DECL_EXPORT *formatString(const char *prefix, const char *suffix, int numArguments, ...)
Q_DECL_EXPORT ignoreMessage(int type, const char *message)
Q_DECL_EXPORT qCleanup()
bool qCompare(const T &t1, const T &t2, const char *actual, const char *expected, const char *file, int line)
Q_DECL_EXPORT qCompare(const float &t1, const float &t2, const char *actual, const char *expected, const char *file, int line)
Q_DECL_EXPORT qCompare(const double &t1, const double &t2, const char *actual, const char *expected, const char *file, int line)
bool qCompare(const T1 &t1, const T2 &t2, const char *actual, const char *expected, const char *file, int line)
bool qCompare(const double &t1, const float &t2, const char *actual, const char *expected, const char *file, int line)
bool qCompare(const float &t1, const double &t2, const char *actual, const char *expected, const char *file, int line)
bool qCompare(const T *t1, const T *t2, const char *actual, const char *expected, const char *file, int line)
bool qCompare(T *t1, T *t2, const char *actual, const char *expected, const char *file, int line)
bool qCompare(T *t1, std::nullptr_t, const char *actual, const char *expected, const char *file, int line)
bool qCompare(std::nullptr_t, T *t2, const char *actual, const char *expected, const char *file, int line)
bool qCompare(const T1 *t1, const T2 *t2, const char *actual, const char *expected, const char *file, int line)
bool qCompare(T1 *t1, T2 *t2, const char *actual, const char *expected, const char *file, int line)
bool qCompare(const char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line)
bool qCompare(char *t1, char *t2, const char *actual, const char *expected, const char *file, int line)
bool qCompare(char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line)
bool qCompare(const char *t1, char *t2, const char *actual, const char *expected, const char *file, int line)
Q_DECL_EXPORT *qData(const char *tagName, int typeId)
Q_DECL_EXPORT *qElementData(const char *elementName, int metaTypeId)
Q_DECL_EXPORT qExec(QObject *testObject, int argc = 0, char **argv = nullptr)
Q_DECL_EXPORT qExec(QObject *testObject, const QStringList &arguments)
Q_DECL_EXPORT qExpectFail(const char *dataIndex, const char *comment, QTest::TestFailMode mode, const char *file, int line)
Q_DECL_EXPORT qFail(const char *statementStr, const char *file, int line)
Q_DECL_EXPORT *qGlobalData(const char *tagName, int typeId)
Q_DECL_EXPORT qInit(QObject *testObject, int argc = 0, char **argv = nullptr)
Q_DECL_EXPORT qRun()
Q_DECL_EXPORT qSkip(const char *message, const char *file, int line)
Q_DECL_EXPORT qSleep(int ms)
bool qTest(const T &actual, const char *elementName, const char *actualStr, const char *expected, const char *file, int line)
Q_DECL_EXPORT qVerify(bool statement, const char *statementStr, const char *description, const char *file, int line)
bool qWaitForWindowActive(QWindow *window, int timeout = 5000)
bool qWaitForWindowActive(QWidget *widget, int timeout = 5000)
bool qWaitForWindowExposed(QWindow *window, int timeout = 5000)
bool qWaitForWindowExposed(QWidget *widget, int timeout = 5000)
Q_DECL_EXPORT qWarn(const char *message, const char *file = nullptr, int line = 0)
int qt_asprintf(QTestCharBuffer *buf, const char *format, ...)
Q_DECL_EXPORT setMainSourcePath(const char *file, const char *builddir = nullptr)
Q_DECL_EXPORT *toHexRepresentation(const char *ba, int length)
Q_DECL_EXPORT *toPrettyCString(const char *unicode, int length)
Q_DECL_EXPORT *toPrettyUnicode(int string)
char *toString(const T &t)
char *toString(const int &pair)
char *toString(const std::tuple<Types...> &tuple)
Q_DECL_EXPORT *toString(const char *)
Q_DECL_EXPORT *toString(const void *)

Macros

QBENCHMARK
QBENCHMARK_ONCE
QCOMPARE(actual, expected)
QEXPECT_FAIL(dataIndex, comment, mode)
QFAIL(message)
QFETCH(type, name)
QFINDTESTDATA(filename)
QSKIP(description)
QTEST(actual, testElement)
QTEST_APPLESS_MAIN(TestClass)
QTEST_GUILESS_MAIN(TestClass)
QTEST_MAIN(TestClass)
QTRY_COMPARE(actual, expected)
QTRY_COMPARE_WITH_TIMEOUT(actual, expected, timeout)
QTRY_VERIFY2(condition, message)
QTRY_VERIFY(condition)
QTRY_VERIFY2_WITH_TIMEOUT(condition, message, timeout)
QTRY_VERIFY_WITH_TIMEOUT(condition, timeout)
QVERIFY2(condition, message)
QVERIFY(condition)
QVERIFY_EXCEPTION_THROWN(expression, exceptiontype)
QWARN(message)

Detailed Description

The QTest namespace contains all the functions and declarations that are related to Qt Test.

See the Qt Test Overview for information about how to write unit tests.

Type Documentation

enum QTest::AttributeIndex

This enum numbers the different tests.

ConstantValue
QTest::AI_Undefined-1
QTest::AI_Name0
QTest::AI_Result1
QTest::AI_Tests2
QTest::AI_Failures3
QTest::AI_Errors4
QTest::AI_Type5
QTest::AI_Description6
QTest::AI_PropertyValue7
QTest::AI_QTestVersion8
QTest::AI_QtVersion9
QTest::AI_File10
QTest::AI_Line11
QTest::AI_Metric12
QTest::AI_Tag13
QTest::AI_Value14
QTest::AI_Iterations15

enum QTest::LogElementType

The enum specifies the kinds of test log messages.

ConstantValue
QTest::LET_Undefined-1
QTest::LET_Property0
QTest::LET_Properties1
QTest::LET_Failure2
QTest::LET_Error3
QTest::LET_TestCase4
QTest::LET_TestSuite5
QTest::LET_Benchmark6
QTest::LET_SystemError7

enum QTest::QBenchmarkMetric

This enum lists all the things that can be benchmarked.

ConstantValueDescription
QTest::FramesPerSecond0Frames per second
QTest::BitsPerSecond1Bits per second
QTest::BytesPerSecond2Bytes per second
QTest::WalltimeMilliseconds3Clock time in milliseconds
QTest::WalltimeNanoseconds7Clock time in nanoseconds
QTest::BytesAllocated8Memory usage in bytes
QTest::Events6Event count
QTest::CPUTicks4CPU time
QTest::CPUMigrations9Process migrations between CPUs
QTest::CPUCycles10CPU cycles
QTest::RefCPUCycles30Reference CPU cycles
QTest::BusCycles11Bus cycles
QTest::StalledCycles12Cycles stalled
QTest::InstructionReads5Instruction reads
QTest::Instructions13Instructions executed
QTest::BranchInstructions14Branch-type instructions
QTest::BranchMisses15Branch instructions that were mispredicted
QTest::CacheReferences16Cache accesses of any type
QTest::CacheMisses20Cache misses of any type
QTest::CacheReads17Cache reads / loads
QTest::CacheReadMisses21Cache read / load misses
QTest::CacheWrites18Cache writes / stores
QTest::CacheWriteMisses22Cache write / store misses
QTest::CachePrefetches19Cache prefetches
QTest::CachePrefetchMisses23Cache prefetch misses
QTest::ContextSwitches24Context switches
QTest::PageFaults25Page faults of any type
QTest::MinorPageFaults26Minor page faults
QTest::MajorPageFaults27Major page faults
QTest::AlignmentFaults28Faults caused due to misalignment
QTest::EmulationFaults29Faults that needed software emulation

Note that WalltimeNanoseconds and BytesAllocated are only provided for use via setBenchmarkResult(), and results in those metrics are not able to be provided automatically by the QTest framework.

This enum was introduced or modified in Qt 4.7.

See also QTest::benchmarkMetricName() and QTest::benchmarkMetricUnit().

enum QTest::TestFailMode

This enum describes the modes for handling an expected failure of the QVERIFY() or QCOMPARE() macros.

ConstantValueDescription
QTest::Abort1Aborts the execution of the test. Use this mode when it doesn't make sense to execute the test any further after the expected failure.
QTest::Continue2Continues execution of the test after the expected failure.

See also QEXPECT_FAIL().

Function Documentation

void QTest::addColumn(const char *name, T * = nullptr)

Q_DECL_EXPORT QTest::addColumnInternal(int id, const char *name)

const char *QTest::benchmarkMetricName(QTest::QBenchmarkMetric metric)

Returns the enum value metric as a character string.

This function was introduced in Qt 4.7.

const char *QTest::benchmarkMetricUnit(QTest::QBenchmarkMetric metric)

Retuns the units of measure for the specified metric.

This function was introduced in Qt 4.7.

Q_DECL_EXPORT QTest::compare_helper(bool success, const char *failureMsg, char *val1, char *val2, const char *actual, const char *expected, const char *file, int line)

bool QTest::compare_ptr_helper(const volatile void *t1, const volatile void *t2, const char *actual, const char *expected, const char *file, int line)

bool QTest::compare_ptr_helper(const volatile void *t1, std::nullptr_t, const char *actual, const char *expected, const char *file, int line)

bool QTest::compare_ptr_helper(std::nullptr_t, const volatile void *t2, const char *actual, const char *expected, const char *file, int line)

Q_DECL_EXPORT QTest::compare_string_helper(const char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line)

const Q_DECL_EXPORT *QTest::currentAppName()

const Q_DECL_EXPORT *QTest::currentDataTag()

Q_DECL_EXPORT QTest::currentTestFailed()

const Q_DECL_EXPORT *QTest::currentTestFunction()

Q_DECL_EXPORT *QTest::formatString(const char *prefix, const char *suffix, int numArguments, ...)

Q_DECL_EXPORT QTest::ignoreMessage(int type, const char *message)

Q_DECL_EXPORT QTest::qCleanup()

bool QTest::qCompare(const T &t1, const T &t2, const char *actual, const char *expected, const char *file, int line)

Q_DECL_EXPORT QTest::qCompare(const float &t1, const float &t2, const char *actual, const char *expected, const char *file, int line)

Q_DECL_EXPORT QTest::qCompare(const double &t1, const double &t2, const char *actual, const char *expected, const char *file, int line)

bool QTest::qCompare(const T1 &t1, const T2 &t2, const char *actual, const char *expected, const char *file, int line)

bool QTest::qCompare(const double &t1, const float &t2, const char *actual, const char *expected, const char *file, int line)

bool QTest::qCompare(const float &t1, const double &t2, const char *actual, const char *expected, const char *file, int line)

bool QTest::qCompare(const T *t1, const T *t2, const char *actual, const char *expected, const char *file, int line)

bool QTest::qCompare(T *t1, T *t2, const char *actual, const char *expected, const char *file, int line)

bool QTest::qCompare(T *t1, std::nullptr_t, const char *actual, const char *expected, const char *file, int line)

bool QTest::qCompare(std::nullptr_t, T *t2, const char *actual, const char *expected, const char *file, int line)

bool QTest::qCompare(const T1 *t1, const T2 *t2, const char *actual, const char *expected, const char *file, int line)

bool QTest::qCompare(T1 *t1, T2 *t2, const char *actual, const char *expected, const char *file, int line)

bool QTest::qCompare(const char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line)

bool QTest::qCompare(char *t1, char *t2, const char *actual, const char *expected, const char *file, int line)

bool QTest::qCompare(char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line)

bool QTest::qCompare(const char *t1, char *t2, const char *actual, const char *expected, const char *file, int line)

Q_DECL_EXPORT *QTest::qData(const char *tagName, int typeId)

Q_DECL_EXPORT *QTest::qElementData(const char *elementName, int metaTypeId)

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

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

Q_DECL_EXPORT QTest::qExpectFail(const char *dataIndex, const char *comment, QTest::TestFailMode mode, const char *file, int line)

Q_DECL_EXPORT QTest::qFail(const char *statementStr, const char *file, int line)

Q_DECL_EXPORT *QTest::qGlobalData(const char *tagName, int typeId)

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

Q_DECL_EXPORT QTest::qRun()

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

Q_DECL_EXPORT QTest::qSleep(int ms)

bool QTest::qTest(const T &actual, const char *elementName, const char *actualStr, const char *expected, const char *file, int line)

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

bool QTest::qWaitForWindowActive(QWindow *window, int timeout = 5000)

Waits for timeout milliseconds or until the window is active.

Returns true if window is active within timeout milliseconds, otherwise returns false.

This function was introduced in Qt 5.0.

See also qWaitForWindowExposed() and QWindow::isActive().

bool QTest::qWaitForWindowActive(QWidget *widget, int timeout = 5000)

bool QTest::qWaitForWindowExposed(QWindow *window, int timeout = 5000)

Waits for timeout milliseconds or until the window is exposed. Returns true if window is exposed within timeout milliseconds, otherwise returns false.

This is mainly useful for asynchronous systems like X11, where a window will be mapped to screen some time after being asked to show itself on the screen.

Note that a window that is mapped to screen may still not be considered exposed if the window client area is completely covered by other windows, or if the window is otherwise not visible. This function will then time out when waiting for such a window.

This function was introduced in Qt 5.0.

See also qWaitForWindowActive() and QWindow::isExposed().

bool QTest::qWaitForWindowExposed(QWidget *widget, int timeout = 5000)

Q_DECL_EXPORT QTest::qWarn(const char *message, const char *file = nullptr, int line = 0)

int QTest::qt_asprintf(QTestCharBuffer *buf, const char *format, ...)

Q_DECL_EXPORT QTest::setMainSourcePath(const char *file, const char *builddir = nullptr)

Q_DECL_EXPORT *QTest::toHexRepresentation(const char *ba, int length)

Q_DECL_EXPORT *QTest::toPrettyCString(const char *unicode, int length)

Q_DECL_EXPORT *QTest::toPrettyUnicode(int string)

char *QTest::toString(const T &t)

char *QTest::toString(const int &pair)

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

Q_DECL_EXPORT *QTest::toString(const char *)

Q_DECL_EXPORT *QTest::toString(const void *)

Macro Documentation

QBENCHMARK

This macro is used to measure the performance of code within a test. The code to be benchmarked is contained within a code block following this macro.

For example:


  void TestBenchmark::simple()
  {
      QString str1 = QLatin1String("This is a test string");
      QString str2 = QLatin1String("This is a test string");

      QCOMPARE(str1.localeAwareCompare(str2), 0);

      QBENCHMARK {
          str1.localeAwareCompare(str2);
      }
  }

See also Creating a Benchmark and Writing a Benchmark.

QBENCHMARK_ONCE

The QBENCHMARK_ONCE macro is for measuring performance of a code block by running it once.

This macro is used to measure the performance of code within a test. The code to be benchmarked is contained within a code block following this macro.

Unlike QBENCHMARK, the contents of the contained code block is only run once. The elapsed time will be reported as "0" if it's to short to be measured by the selected backend. (Use)

This function was introduced in Qt 4.6.

See also Creating a Benchmark and Writing a Benchmark.

QCOMPARE(actual, expected)

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.

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(), QTRY_COMPARE(), and QTest::toString().

QEXPECT_FAIL(dataIndex, comment, mode)

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(), and QCOMPARE().

QFAIL(message)

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.");

QFETCH(type, name)

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:


  void TestQString::toInt()
  {
       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.

QFINDTESTDATA(filename)

Returns a QString for the testdata file referred to by filename, or an empty QString if the testdata file could not be found.

This macro allows the test to load data from an external file without hardcoding an absolute filename into the test, or using relative paths which may be error prone.

The returned path will be the first path from the following list which resolves to an existing file or directory:

  • filename relative to QCoreApplication::applicationDirPath() (only if a QCoreApplication or QApplication object has been created).
  • filename relative to the test's standard install directory (QLibraryInfo::TestsPath with the lowercased testcase name appended).
  • filename relative to the directory containing the source file from which QFINDTESTDATA is invoked.

If the named file/directory does not exist at any of these locations, a warning is printed to the test log.

For example, in this code:


  // Source: /home/user/sources/myxmlparser/tests/tst_myxmlparser/tst_myxmlparser.cpp
  // Build:  /home/user/build/myxmlparser/tests/tst_myxmlparser
  // Qt:     /usr/local/Qt-5.0.0
  void tst_MyXmlParser::parse()
  {
      MyXmlParser parser;
      QString input = QFINDTESTDATA("testxml/simple1.xml");
      QVERIFY(parser.parse(input));
  }

The testdata file will be resolved as the first existing file from:

  • /home/user/build/myxmlparser/tests/tst_myxmlparser/testxml/simple1.xml
  • /usr/local/Qt-5.0.0/tests/tst_myxmlparser/testxml/simple1.xml
  • /home/user/sources/myxmlparser/tests/tst_myxmlparser/testxml/simple1.xml

This allows the test to find its testdata regardless of whether the test has been installed, and regardless of whether the test's build tree is equal to the test's source tree.

Note: reliable detection of testdata from the source directory requires either that qmake is used, or the QT_TESTCASE_BUILDDIR macro is defined to point to the working directory from which the compiler is invoked, or only absolute paths to the source files are passed to the compiler. Otherwise, the absolute path of the source directory cannot be determined.

Note: For tests that use the QTEST_APPLESS_MAIN() macro to generate a main() function, QFINDTESTDATA will not attempt to find test data relative to QCoreApplication::applicationDirPath(). In practice, this means that tests using QTEST_APPLESS_MAIN() will fail to find their test data if run from a shadow build tree.

This function was introduced in Qt 5.0.

QSKIP(description)

If called from a test function, 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 of why the test couldn't be executed.

If the test is data-driven, each call to QSKIP() will skip only the current row of test data, so an unconditional call to QSKIP will produce one skip message in the test log for each row of test data.

If called from an _data function, the QSKIP() macro will stop execution of the _data function and will prevent execution of the associated test function.

If called from initTestCase() or initTestCase_data(), the QSKIP() macro will skip all test and _data functions.

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

Example:


  if (!QSqlDatabase::drivers().contains("SQLITE"))
      QSKIP("This test requires the SQLITE database driver");

QTEST(actual, testElement)

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().

QTEST_APPLESS_MAIN(TestClass)

Implements a main() function that executes all tests in TestClass.

Behaves like QTEST_MAIN(), but doesn't instantiate a QApplication object. Use this macro for really simple stand-alone non-GUI tests.

See also QTEST_MAIN().

QTEST_GUILESS_MAIN(TestClass)

Implements a main() function that instantiates a QCoreApplication object and the TestClass, and executes all tests in the order they were defined. Use this macro to build stand-alone executables.

Behaves like QTEST_MAIN(), but instantiates a QCoreApplication instead of the QApplication object. Use this macro if your test case doesn't need functionality offered by QApplication, but the event loop is still necessary.

This function was introduced in Qt 5.0.

See also QTEST_MAIN().

QTEST_MAIN(TestClass)

Implements a main() function that instantiates an application object and the TestClass, and executes all tests in the order they were defined. Use this macro to build stand-alone executables.

If QT_WIDGETS_LIB is defined, the application object will be a QApplication, if QT_GUI_LIB is defined, the application object will be a QGuiApplication, otherwise it will be a QCoreApplication. If qmake is used and the configuration includes QT += widgets, then QT_WIDGETS_LIB will be defined automatically. Similarly, if qmake is used and the configuration includes QT += gui, then QT_GUI_LIB will be defined automatically.

Note: On platforms that have keypad navigation enabled by default, this macro will forcefully disable it if QT_WIDGETS_LIB is defined. This is done to simplify the usage of key events when writing autotests. If you wish to write a test case that uses keypad navigation, you should enable it either in the initTestCase() or init() functions of your test case by calling QApplication::setNavigationMode().

Example:


  class TestQString: public QObject { ... };
  QTEST_MAIN(TestQString)

See also QTEST_APPLESS_MAIN(), QTEST_GUILESS_MAIN(), QTest::qExec(), and QApplication::setNavigationMode().

QTRY_COMPARE(actual, expected)

Performs a comparison of the actual and expected values by invoking QTRY_COMPARE_WITH_TIMEOUT() with a timeout of five seconds.

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

This function was introduced in Qt 5.0.

See also QTRY_COMPARE_WITH_TIMEOUT(), QCOMPARE(), QVERIFY(), and QTRY_VERIFY().

QTRY_COMPARE_WITH_TIMEOUT(actual, expected, timeout)

The QTRY_COMPARE_WITH_TIMEOUT() macro is similar to QCOMPARE(), but performs the comparison of the actual and expected values repeatedly, until either the two values are equal or the timeout (in milliseconds) is reached. Between each comparison, events will be processed. If the timeout is reached, 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.

This function was introduced in Qt 5.0.

See also QTRY_COMPARE(), QCOMPARE(), QVERIFY(), and QTRY_VERIFY().

QTRY_VERIFY2(condition, message)

Checks the condition by invoking QTRY_VERIFY2_WITH_TIMEOUT() with a timeout of five seconds. If condition is then still false, message is output. The message is a plain C string.

Example:


  QTRY_VERIFY2_WITH_TIMEOUT(list.size() > 2, QByteArray::number(list.size()).constData());

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

This function was introduced in Qt 5.6.

See also QTRY_VERIFY2_WITH_TIMEOUT(), QTRY_VERIFY2(), QVERIFY(), QCOMPARE(), and QTRY_COMPARE().

QTRY_VERIFY(condition)

Checks the condition by invoking QTRY_VERIFY_WITH_TIMEOUT() with a timeout of five seconds.

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

This function was introduced in Qt 5.0.

See also QTRY_VERIFY_WITH_TIMEOUT(), QTRY_VERIFY2(), QVERIFY(), QCOMPARE(), and QTRY_COMPARE().

QTRY_VERIFY2_WITH_TIMEOUT(condition, message, timeout)

The QTRY_VERIFY2_WITH_TIMEOUT macro is similar to QTRY_VERIFY_WITH_TIMEOUT() except that it outputs a verbose message when condition is still false after the specified timeout (in milliseconds). The message is a plain C string.

Example:


  QTRY_VERIFY2_WITH_TIMEOUT(list.size() > 2, QByteArray::number(list.size()).constData(), 10000);

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

This function was introduced in Qt 5.6.

See also QTRY_VERIFY(), QTRY_VERIFY_WITH_TIMEOUT(), QVERIFY(), QCOMPARE(), and QTRY_COMPARE().

QTRY_VERIFY_WITH_TIMEOUT(condition, timeout)

The QTRY_VERIFY_WITH_TIMEOUT() macro is similar to QVERIFY(), but checks the condition repeatedly, until either the condition becomes true or the timeout (in milliseconds) is reached. Between each evaluation, events will be processed. If the timeout is reached, 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.

This function was introduced in Qt 5.0.

See also QTRY_VERIFY(), QTRY_VERIFY2_WITH_TIMEOUT(), QVERIFY(), QCOMPARE(), and QTRY_COMPARE().

QVERIFY2(condition, message)

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() and QCOMPARE().

QVERIFY(condition)

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() and QTRY_VERIFY().

QVERIFY_EXCEPTION_THROWN(expression, exceptiontype)

The QVERIFY_EXCEPTION_THROWN macro executes an expression and tries to catch an exception thrown from the expression. If the expression throws an exception and its type is the same as exceptiontype or exceptiontype is substitutable with the type of thrown exception (i.e. usually the type of thrown exception is publicly derived from exceptiontype) then execution will be continued. If not-substitutable type of exception is thrown or the expression doesn't throw an exception at all, then a failure will be 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.

This function was introduced in Qt 5.3.

QWARN(message)

Appends message as a warning to the test log. This macro can be used anywhere in your tests.

Note: This function is thread-safe.