Home

QtTestTable Class Reference

The QtTestTable class stores test data. More...

#include <QtTestTable>

Public Functions


Detailed Description

The QtTestTable class stores test data.

QtTestTable is a helper class that contains a two-dimensional array of test data. It can have an arbitrary number of elements (columns) and an arbitrary number of test data (rows).

A single test could be written as:

    void TestQString::toInt()
    {
        string = "42";
        result = 42;

        COMPARE(string.toInt(), result);
    }

However, sometimes it is useful to run the same test on different sets of data. The way to achieve this is first to store the test data in a QTestTable. For example:

    void TestQString::toInt_data(QtTestTable &t)
    {
        t.defineElement("QString", "string");
        t.defineElement("int", "result");

        *t.newData("conversion of 42") << "42" << 42;
        *t.newData("conversion of -43") << "-43" << 43;
        *t.newData("invalid conversion") << "abc" << 0;
    }

which will logically look similar to this:

indexnamestringresult
0conversion of 42"42"42
1conversion of -43"-43"43
2invalid conversion"abc"0

Then the test function can be written as

        void TestQString::toInt()
        {
            FETCH(QString, string);
            FETCH(int, result);

            COMPARE(string.toInt(), result);
        }

We use the FETCH macro to retrieve the elements of a set of data; then we perform the test. The test function will be called once for each of the rows in the associated QTestTable.

If a test fails, it is recorded in the test log with a reference to the test function and the name of the data set that made the test fail.

Note: You never have to allocate a QtTestTable yourself, the QtTest framework will do it for you if it encounters a data slot for your test function.


Member Function Documentation

QtTestTable::QtTestTable ()

Constructs a new and empty QtTestTable. Usually, the QtTest framework creates test tables; you never have to construct it yourself.

QtTestTable::~QtTestTable ()

Destroys the test table and frees all data.

int QtTestTable::dataCount () const

Returns the amount of test data in the table, i.e. the number of rows.

See also elementCount().

const char * QtTestTable::dataTag ( int index ) const

Returns the name of the data at index.

void QtTestTable::defineElement ( const char * elementType, const char * elementName )

Defines a new element (column) in the test table with type elementType named elementName.

Example:

    t.defineElement("QString", "expectedString");

int QtTestTable::elementCount () const

Returns the amount of elements in the table, i.e. the number of columns.

See also dataCount().

const char * QtTestTable::elementType ( int index ) const

Returns the name of the element type at index. Use elementTypeId() if you want the type id rather than the name of the type.

Example:

    t.defineElement("QString", "expectedString");
    const char *etype = t.elementType(0); // etype is "QString"

See also elementTypeId().

int QtTestTable::elementTypeId ( int index ) const

Returns the type id of the element at index. Please read Qt's QMetaType documentation for more information on Qt's type ids.

Example:

    t.defineElement("QString", "expectedString");
    Q_ASSERT(t.elementTypeId(0) == QMetaType::QString);

See also elementType().

int QtTestTable::indexOf ( const char * elementName ) const

Returns the index of the element called elementName in the table.

bool QtTestTable::isEmpty () const

Returns true if the test table is empty, otherwise false.

QtTestData * QtTestTable::newData ( const char * tag )

Creates a new row in the test table named tag. Use this function to append data to a QtTestTable.

Example:

    t.defineElement("QString", "string");
    *t.newData("random string") << "sajdfhsdf";
    *t.newData("empty string") << "";

QtTestData * QtTestTable::testData ( int index ) const

Returns the test data (row) at index. You usually don't have to call this function, since the QtTest framework manages the test data.

See also newData().


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