View Full Version : how to test slot using QTest?
hashb
13th December 2009, 09:09
Hi All,
I want to test a http application using QTest, my purpose is to
get contents from a url ,and check whether the content is as expected.
the code something like:
class testHttp : public QObject
{
Q_OBJECT
private slots:
void testGet();
void compareConfig();
private:
CMyhttp *_myhttp;
};
void initTestCase()
{
_myhttp = new CMyhttp ;
}
void testGet()
{
_myhttp->get("some url");
//here I need to wait until the contents returned from web server,
//but it seemed that qtest lib will not wait
connect(_myhttp ,SIGNAL(finished()),this,SLOT(compareConfig()));
}
void compareConfig()
{
QCOMPARE
(
_myhttp.content = "content expected ..."
)
}
Any ideas about it?
Thanks advance for your help.
Best regards,
hb
wysota
13th December 2009, 10:11
Take a look at qWait and/or QEventLoop.
hashb
13th December 2009, 11:13
Hi Wysota,
Thanks a lot for your reply,
I tried qtest:qwait, but it seemed it does help,
after execute qwait,the network process also blocked.
Best regards,
hb
wysota
13th December 2009, 11:26
Do you have the event loop running?
hashb
13th December 2009, 11:37
Hi Wysota,
How to running the event loop?
I run the test using
class testHttp : public QObject
{
Q_OBJECT
private slots:
void testGet();
void compareConfig();
private:
CMyhttp *_myhttp;
};
void testHttp::initTestCase()
{
_myhttp = new CMyhttp ;
}
void testHttp::testGet()
{
_myhttp->get("some url");
connect(_myhttp ,SIGNAL(finished()),this,SLOT(compareConfig()));
QTest::qSleep(5000);
}
void testHttp::compareConfig()
{
QCOMPARE
(
_myhttp.content = "content expected ..."
)
}
int main()
{
testHttp ts3;
QTest::qExec(&ts3);
}
wysota
13th December 2009, 12:00
In that case you don't have an event loop running and network will not work. You need an application object. Take a look at QTEST_MAIN.
hashb
14th December 2009, 04:22
Hi Wysota,
Thank you very much for your help ,it works now!
attach the codes ,hope will help others:
///////////////////////////////////////////
//head file
///////////////////////////////////////////
#include <QHttp>
#include <QHttpResponseHeader>
#include <QString>
class hp:public QHttp
{
Q_OBJECT
public:
hp();
QString data;
signals:
void ok();
public slots:
void done(bool);
};
///////////////////////////////////////////
//cpp file
///////////////////////////////////////////
#include <QDebug>
#include "hp.h"
hp::hp()
{
connect(this, SIGNAL(done(bool)), this, SLOT(done(bool)));
}
void hp::done( bool)
{
data=readAll().mid(0,10);
qDebug()<<data<<endl;
emit ok();
}
//////////////////////////////////////
// test main
//////////////////////////////////////
#include <QtTest>
#include <QtCore>
#include <QSignalSpy>
#include "hp.h"
class testDate: public QObject
{
Q_OBJECT
private slots:
void testValidity();
};
void testDate::testValidity()
{
hp h;
h.setHost("webserver");
h.get("url");
QSignalSpy spy(&h, SIGNAL(ok()));
while (spy.count() == 0)
QTest::qWait(200);
QVERIFY( h.data == "expected data" );
}
QTEST_MAIN(testDate)
#include "t.moc"
Best regards,
hb
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.