PDA

View Full Version : QTest and QFETCH



ManuMies
27th April 2009, 13:28
Is there any simple way to read the test data from file instead of manually defining the test data with _data-functions? Or do I just have to make a file reader...


And when I create test class, which uses private slots as test cases, can I still use private functions without messing up the testcases?

wysota
28th April 2009, 22:25
Is there any simple way to read the test data from file instead of manually defining the test data with _data-functions? Or do I just have to make a file reader...
Read data from file/db from within the _data() method and fill the test vector with it.


And when I create test class, which uses private slots as test cases, can I still use private functions without messing up the testcases?

Yes, just don't declare them as slots.

ManuMies
30th April 2009, 09:41
Thanks, that's what I thought.

And other thing about testing, is there any way to monitor the heapsize with testcases, so I can check if my code is leaking memory? Like UHEAP macros in symbian?



__UHEAP_MARK;
QString* string = new QString();
string->copy("test");
delete string;
__UHEAP_MARKEND;

wysota
30th April 2009, 10:13
Qt itself doesn't have such capabilities. There are dedicated tools for that such as Valgrind.

ManuMies
30th April 2009, 10:56
Are you saying that there is no way to see, how much memory qt application has currently allocated? Then is it possible by using native C?

wysota
4th May 2009, 08:52
Are you saying that there is no way to see, how much memory qt application has currently allocated?
Not by using means built into Qt.

Then is it possible by using native C?

For Linux there is a way. There are system calls returning such information and you can also look into /proc/ to see details about your process. For other platforms - that I do not know.

auba
4th June 2009, 19:09
To get back to the original question... I'm trying to read my test cases from an XML file. There I wanted to define the columns and the rows. First case is checking if the program has the right name :rolleyes:



TDataLoader::fillData(meth) {
QDomElement m = m_Root.firstChildElement(meth);
QDomElement cols = m.firstChildElement(tagColumns);
for(QDomElement col=cols.firstChildElement(tagColumn); !col.isNull(); col=col.nextSiblingElement(tagColumn)) {
QString name = col.attribute(entryName);
QString type = col.attribute(entryType).toLower();
// Simplifying: here is type checking
QTest::addColumn<QString>(name.toLatin1());
}
...
QDomElement rows = m.firstChildElement(tagRows);
for(QDomElement row=rows.firstChildElement(tagRow); !row.isNull(); row=row.nextSiblingElement(tagRow)) {
QString name = row.attribute(entryName);
QTestData& values = QTest::newRow(name.toLatin1());
QDomNamedNodeMap attrs = row.attributes();
for(int i=0; i<attrs.count(); i++) {
QDomNode node = attrs.item(i);
// Simplifying: needs type casting
values << row.attribute(node.nodeName());
}
}
}


In the test method, I call fillData with the method's name (m_Data is the instance of the data loader):



void TTestRunner::testProgram_data()
{
m_Data.fillData("testProgram");
}
void TTestRunner::testProgram()
{
QFETCH(QString, title);
QCOMPARE(QCoreApplication::applicationName(), title);
}


but what I get is an error:


QFETCH: Requested testdata 'title' not available, check your _data function.


Is it wrong to add additional data to the QTestData reference or am I missing something? The XML file is read correctly:



<?xml version="1.0" encoding="utf-8" ?>
<Data>
<testProgram>
<columns>
<column name="title" type="QString" />
</columns>
<rows>
<row name="title" value="targs" />
</rows>
</testProgram>
</Data>

wysota
4th June 2009, 21:28
QTest::addColumn<QString>(name.toLatin1());
This should be:

QTest::addColumn<QString>("title");