Home |
The QtTestTable class stores test data. More...
#include <QtTestTable>
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:
index | name | string | result |
---|---|---|---|
0 | conversion of 42 | "42" | 42 |
1 | conversion of -43 | "-43" | 43 |
2 | invalid 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.
Constructs a new and empty QtTestTable. Usually, the QtTest framework creates test tables; you never have to construct it yourself.
Destroys the test table and frees all data.
Returns the amount of test data in the table, i.e. the number of rows.
See also elementCount().
Returns the name of the data at index.
Defines a new element (column) in the test table with type elementType named elementName.
Example:
t.defineElement("QString", "expectedString");
Returns the amount of elements in the table, i.e. the number of columns.
See also dataCount().
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().
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().
Returns the index of the element called elementName in the table.
Returns true if the test table is empty, otherwise false.
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") << "";
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 |