Home

QtTest Namespace Reference

The QtTest namespace contains all the functions and declarations that are related to the QtTest tool. More...

#include <QtTest>

Types

Functions

Macros


Detailed Description

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

Please refer to the QtTest documentation for information on how to write unit tests.


Type Documentation

enum QtTest::KeyAction

This enum describes possible actions for key handling.

ConstantValueDescription
QtTest::Press0The key is pressed.
QtTest::Release1The key is released.
QtTest::Click2The key is clicked (pressed and released).

enum QtTest::MouseAction

This enum describes possible actions for mouse handling.

ConstantValueDescription
QtTest::MousePress0A mouse button is pressed.
QtTest::MouseRelease1A mouse button is released.
QtTest::MouseClick2A mouse button is clicked (pressed and released).
QtTest::MouseDClick3A mouse button is double clicked (pressed and released twice).
QtTest::MouseMove4The mouse pointer has moved.

enum QtTest::SkipMode

This enum describes the modes for skipping tests during execution of the test data.

ConstantValueDescription
QtTest::SkipSingle1Skips the current entry in the test table; continues execution of all the other entries in the table.
QtTest::SkipAll2Skips all the entries in the test table; the test won't be executed further.

See also SKIP().

enum QtTest::TestFailMode

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

ConstantValueDescription
QtTest::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.
QtTest::Continue2Continues execution of the test after the expected failure.

See also EXPECT_FAIL().


Function Documentation

const char * QtTest::currentDataTag ()

Returns the name of the current test data. If the test doesn't have any assigned testdata, the function returns 0.

See also QtTestTable.

bool QtTest::currentTestFailed ()

Returns true if the current test function failed, otherwise false.

const char * QtTest::currentTestFunction ()

Returns the name of the test function that is currently executed.

Example:

    void MyTestClass::cleanup()
    {
        if (qstrcmp(currentTestFunction(), "myDatabaseTest") == 0) {
            // clean up all database connections
            closeAllDatabases();
        }
    }

int QtTest::exec ( QObject * testObject, int argc = 0, char ** argv = 0 )

Executes tests declared in testObject. Optionally, the command line arguments argc and argv can be provided. For a list of recognized arguments, read QtTest Command Line Arguments.

For stand-alone tests, the convenience macro QTTEST_MAIN() can be used to declare a main method that parses the command line arguments and executes the tests.

See also QTTEST_MAIN().

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

Ignores messages created by qDebug() or qWarning(). If the message with the corresponding type is outputted, it will be removed from the test log. If the test finished and the message was not outputted, a test failure is appended to the test log.

Note: Invoking this function will only ignore one message. If the message you want to ignore is outputted twice, you have to call ignoreMessage() twice, too.

Example:

    QDir dir;

    QtTest::ignoreMessage(QtWarningMessage, "QDir::mkdir: Empty or null file name(s)");
    dir.mkdir("");

The example above tests that QDir::mkdir() outputs the right warning when invoked with an invalid file name.

void QtTest::keyClick ( QWidget * widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1 )   [static]

Simulates clicking of key with an optional modifier on a widget. If delay is larger than 0, the test will wait for delay milliseconds.

Examples:

    QtTest::keyClick(myWidget, Qt::Key_Escape);

    QtTest::keyClick(myWidget, Qt::Key_Escape, Qt::ShiftModifier, 200);

The first example above simulates clicking the escape key on myWidget without any keyboard modifiers and without delay. The second example simulates clicking shift-escape on myWidget with a following 200 ms delay of the test.

See also QtTest::keyClicks().

void QtTest::keyClick ( QWidget * widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1 )   [static]

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Simulates clicking of key with an optional modifier on a widget. If delay is larger than 0, the test will wait for delay milliseconds.

Example:

    QtTest::keyClick(myWidget, 'a');

The example above simulates clicking a on myWidget without any keyboard modifiers and without delay of the test.

See also QtTest::keyClicks().

void QtTest::keyClicks ( QWidget * widget, const QString & sequence, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1 )   [static]

Simulates clicking a sequence of keys on a widget. Optionally, a keyboard modifier can be specified as well as a delay (in milliseconds) of the test after each key click.

Example:

    QtTest::keyClicks(myWidget, "hello world");

The example above simulates clicking the sequence of keys representing "hello world" on myWidget without any keyboard modifiers and without delay of the test.

See also QtTest::keyClick().

void QtTest::keyEvent ( KeyAction action, QWidget * widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1 )   [static]

Sends a Qt key event to widget with the given key and an associated action. Optionally, a keyboard modifier can be specified, as well as a delay (in milliseconds) of the test after sending the event.

void QtTest::keyEvent ( KeyAction action, QWidget * widget, char ascii, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1 )   [static]

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Sends a Qt key event to widget with the given key ascii and an associated action. Optionally, a keyboard modifier can be specified, as well as a delay (in milliseconds) of the test after sending the event.

void QtTest::keyPress ( QWidget * widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1 )   [static]

Simulates pressing a key with an optional modifier on a widget. If delay is larger than 0, the test will wait for delay milliseconds.

Note: At some point you should release the key using keyRelease().

See also QtTest::keyRelease() and QtTest::keyClick().

void QtTest::keyPress ( QWidget * widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1 )   [static]

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Simulates pressing a key with an optional modifier on a widget. If delay is larger than 0, the test will wait for delay milliseconds.

Note: At some point you should release the key using keyRelease().

See also QtTest::keyRelease() and QtTest::keyClick().

void QtTest::keyRelease ( QWidget * widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1 )   [static]

Simulates releasing a key with an optional modifier on a widget. If delay is larger than 0, the test will wait for delay milliseconds.

See also QtTest::keyPress() and QtTest::keyClick().

void QtTest::keyRelease ( QWidget * widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1 )   [static]

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Simulates releasing a key with an optional modifier on a widget. If delay is larger than 0, the test will wait for delay milliseconds.

See also QtTest::keyClick().

void QtTest::mouseClick ( QWidget * widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay = -1 )

Simulates clicking a mouse button with an optional modifier on a widget. The position of the click is defined by pos; the default position is the center of the widget. If delay is specified, the test will wait for the specified amount of milliseconds after pressing and after releasing the button.

See also QtTest::mousePress() and QtTest::mouseRelease().

void QtTest::mouseDClick ( QWidget * widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay = -1 )

Simulates double clicking a mouse button with an optional modifier on a widget. The position of the click is defined by pos; the default position is the center of the widget. If delay is specified, the test will wait for the specified amount of milliseconds after each press and release.

See also QtTest::mouseClick().

void QtTest::mouseMove ( QWidget * widget, QPoint pos = QPoint(), int delay = -1 )

Moves the mouse pointer to a widget. If pos is not specified, the mouse pointer moves to the center of the widget. If a delay (in milliseconds) is given, the test will wait after moving the mouse pointer.

void QtTest::mousePress ( QWidget * widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay = -1 )

Simulates pressing a mouse button with an optional modifier on a widget. The position is defined by pos; the default position is the center of the widget. If delay is specified, the test will wait for the specified amount of milliseconds after the press.

See also QtTest::mouseRelease() and QtTest::mouseClick().

void QtTest::mouseRelease ( QWidget * widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay = -1 )

Simulates releasing a mouse button with an optional modifier on a widget. The position of the release is defined by pos; the default position is the center of the widget. If delay is specified, the test will wait for the specified amount of milliseconds after releasing the button.

See also QtTest::mousePress() and QtTest::mouseClick().

void QtTest::sleep ( int ms )   [static]

Sleeps for ms milliseconds, blocking execution of the test. sleep() will not do any event processing and leave your test unresponsive. Network communication might time out while sleeping. Use wait() to do non-blocking sleeping.

Note: The sleep() function calls either nanosleep() on unix or Sleep() on windows, so the accuracy of time spent in sleep() depends on the operating system.

Example:

    QSystem::sleep(250);

See also wait().

char * QtTest::toString ( const T & value )

Returns a textual representation of value. This function is used by COMPARE() to output verbose information in case of a test failure.

You can add specializations of this function to your test to enable verbose output.

Note: The caller of toString() must delete the returned data using delete[]. Your implementation should return a string created with new[] or qstrdup().

Example:

    namespace QtTest {
        template<>
        char *toString(const MyPoint &point)
        {
            QByteArray ba = "MyPoint(";
            ba += QByteArray::number(point.x()) + ", " + QByteArray::number(point.y());
            ba += ")";
            return qstrdup(ba.data());
        }
    }

The example above defines a toString() specialization for a class called MyPoint. Whenever a comparison of two instances of MyPoint fails, COMPARE() will call this function to output the contents of MyPoint to the test log.

See also COMPARE().

void QtTest::wait ( int ms )   [static]

Waits for ms milliseconds. While waiting, events will be processed and your test will stay responsive to user interface events or network communication.

Example:

    int i = 0;
    while (myNetworkServerNotResponding() && i++ < 50)
        QSystem::wait(250);

The code above will wait until the network server is responding for a maximum of about 12.5 seconds.

See also sleep().


Macro Documentation

COMPARE ( actual, expected )

The COMPARE 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.

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

Note: COMPARE 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 implicitely casts the argument.

Note that, for your own classes, you can use QtTest::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:

    COMPARE(QString("hello").toUpper(), QString("HELLO"));

See also VERIFY() and QtTest::toString().

EXPECT_FAIL ( dataIndex, comment, mode )

The EXPECT_FAIL() macro marks the next COMPARE() or VERIFY() as an expected failure. Instead of adding a failure to the test log, an expected failure will be reported.

If a VERIFY() or COMPARE() is marked as an expected failure, but passes instead, an unexpected pass (XPASS) is written to the test log. Unexpected passes behave exactly as failures, the test will stop executing.

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 QtTest::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:

    EXPECT_FAIL("", "Will fix in the next release", Continue);
    COMPARE(i, 42);
    COMPARE(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 EXPECT_FAIL() has no influence on the second COMPARE() statement in the example.

Example 2:

    EXPECT_FAIL("data27", "Oh my, this is soooo broken", Abort);
    COMPARE(i, 42);

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

See also QtTest::TestFailMode, VERIFY(), and COMPARE().

FAIL ( 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)
        FAIL("This test has not been ported to this platform yet.");

FETCH ( 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(QtTestTable &t)
    {
        t.defineElement("QString", "aString");
        t.defineElement("int", "expected");

        *t.newData("positive value") << "42" << 42;
        *t.newData("negative value") << "-42" << -42;
        *t.newData("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()
    {
         FETCH(QString, aString);
         FETCH(int, expected);

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

See also QtTestTable.

QTTEST_APPLESS_MAIN ( TestClass )

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

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

See also QTTEST_MAIN().

QTTEST_MAIN ( TestClass )

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

Example:

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

See also QTTEST_APPLESS_MAIN() and QtTest::exec().

QTTEST_NOOP_MAIN ()

Implements a main() function with a test class that does absolutely nothing. Use this macro to create a test that produces valid test output but just doesn't execute any test, for example in conditional compilations:

    #ifdef Q_WS_X11
        QTTEST_MAIN(MyX11Test)
    #else
        // do nothing on non-X11 platforms
        QTTEST_NOOP_MAIN
    #endif

See also QTTEST_MAIN().

SKIP ( description, mode )

The SKIP() 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 QtTest::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"))
        SKIP("This test requires the SQLITE database driver", SkipAll);

See also QtTest::SkipMode.

TEST ( actual, testElement )

TEST() is a convenience macro for COMPARE() 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, TEST() behaves exactly as COMPARE().

Instead of writing:

    FETCH(QString, myString);
    COMPARE(QString("hello").toUpper(), myString);

you can write:

    TEST(QString("hello").toUpper(), "myString");

See also COMPARE().

VERIFY ( condition )

The VERIFY() 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:

    VERIFY(1 + 1 == 2);

See also COMPARE().

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


Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies) Trademarks
Qt Solutions