Results 1 to 7 of 7

Thread: how to test slot using QTest?

  1. #1
    Join Date
    Jun 2009
    Posts
    74
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Question how to test slot using QTest?

    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:

    Qt Code:
    1. class testHttp : public QObject
    2. {
    3. Q_OBJECT
    4. private slots:
    5. void testGet();
    6. void compareConfig();
    7. private:
    8. CMyhttp *_myhttp;
    9. };
    10.  
    11. void initTestCase()
    12. {
    13. _myhttp = new CMyhttp ;
    14.  
    15. }
    16.  
    17. void testGet()
    18. {
    19. _myhttp->get("some url");
    20. //here I need to wait until the contents returned from web server,
    21. //but it seemed that qtest lib will not wait
    22. connect(_myhttp ,SIGNAL(finished()),this,SLOT(compareConfig()));
    23. }
    24.  
    25. void compareConfig()
    26. {
    27. QCOMPARE
    28. (
    29. _myhttp.content = "content expected ..."
    30. )
    31. }
    To copy to clipboard, switch view to plain text mode 

    Any ideas about it?

    Thanks advance for your help.

    Best regards,
    hb

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to test slot using QTest?

    Take a look at qWait and/or QEventLoop.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    hashb (14th December 2009)

  4. #3
    Join Date
    Jun 2009
    Posts
    74
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Default Re: how to test slot using QTest?

    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

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to test slot using QTest?

    Do you have the event loop running?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Jun 2009
    Posts
    74
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Default Re: how to test slot using QTest?

    Hi Wysota,

    How to running the event loop?

    I run the test using

    Qt Code:
    1. class testHttp : public QObject
    2. {
    3. Q_OBJECT
    4. private slots:
    5. void testGet();
    6. void compareConfig();
    7. private:
    8. CMyhttp *_myhttp;
    9. };
    10.  
    11. void testHttp::initTestCase()
    12. {
    13. _myhttp = new CMyhttp ;
    14.  
    15. }
    16.  
    17. void testHttp::testGet()
    18. {
    19. _myhttp->get("some url");
    20. connect(_myhttp ,SIGNAL(finished()),this,SLOT(compareConfig()));
    21. QTest::qSleep(5000);
    22. }
    23.  
    24. void testHttp::compareConfig()
    25. {
    26. QCOMPARE
    27. (
    28. _myhttp.content = "content expected ..."
    29. )
    30. }
    31.  
    32. int main()
    33. {
    34. testHttp ts3;
    35. QTest::qExec(&ts3);
    36. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to test slot using QTest?

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following 2 users say thank you to wysota for this useful post:

    BalaQT (14th January 2011), hashb (14th December 2009)

  9. #7
    Join Date
    Jun 2009
    Posts
    74
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Default Re: how to test slot using QTest?

    Hi Wysota,

    Thank you very much for your help ,it works now!

    attach the codes ,hope will help others:

    Qt Code:
    1. ///////////////////////////////////////////
    2. //head file
    3. ///////////////////////////////////////////
    4. #include <QHttp>
    5. #include <QHttpResponseHeader>
    6. #include <QString>
    7. class hp:public QHttp
    8. {
    9. Q_OBJECT
    10. public:
    11. hp();
    12. QString data;
    13. signals:
    14. void ok();
    15. public slots:
    16. void done(bool);
    17. };
    18.  
    19. ///////////////////////////////////////////
    20. //cpp file
    21. ///////////////////////////////////////////
    22. #include <QDebug>
    23. #include "hp.h"
    24. hp::hp()
    25. {
    26. connect(this, SIGNAL(done(bool)), this, SLOT(done(bool)));
    27.  
    28. }
    29. void hp::done( bool)
    30. {
    31. data=readAll().mid(0,10);
    32. qDebug()<<data<<endl;
    33. emit ok();
    34. }
    35.  
    36. //////////////////////////////////////
    37. // test main
    38. //////////////////////////////////////
    39. #include <QtTest>
    40. #include <QtCore>
    41. #include <QSignalSpy>
    42. #include "hp.h"
    43.  
    44. class testDate: public QObject
    45. {
    46. Q_OBJECT
    47. private slots:
    48. void testValidity();
    49. };
    50.  
    51. void testDate::testValidity()
    52. {
    53. hp h;
    54. h.setHost("webserver");
    55. h.get("url");
    56. QSignalSpy spy(&h, SIGNAL(ok()));
    57. while (spy.count() == 0)
    58. QTest::qWait(200);
    59. QVERIFY( h.data == "expected data" );
    60. }
    61.  
    62.  
    63.  
    64. QTEST_MAIN(testDate)
    65. #include "t.moc"
    To copy to clipboard, switch view to plain text mode 

    Best regards,
    hb

  10. The following user says thank you to hashb for this useful post:

    BalaQT (14th January 2011)

Similar Threads

  1. How to declare SLOT as a parameter to member function?
    By QPlace in forum Qt Programming
    Replies: 2
    Last Post: 17th July 2018, 00:41
  2. Slot gets called twice
    By greatgatsby in forum Newbie
    Replies: 7
    Last Post: 20th August 2009, 15:11
  3. Replies: 12
    Last Post: 18th September 2008, 15:04
  4. Problem When Creating my own Slot
    By Fatla in forum Qt Programming
    Replies: 12
    Last Post: 6th June 2008, 14:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.