Results 1 to 5 of 5

Thread: Using QT inside boost unit test - how to call QCoreApplication?

  1. #1
    Join Date
    Mar 2010
    Location
    Gurnee, IL USA
    Posts
    1
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Using QT inside boost unit test - how to call QCoreApplication?

    Hi,
    I am developing QT based non-GUI code (using QT events, shared memory, logging, etc). I want to unit test this code using boost unit test framework. The tests all pass ok, but when the test suite ends there are memory leaks. I think this is caused by the QT message loop not being run so QObject (QFile, QDir, etc) instances are not being freed. What I can't figure out is how to run QCoreApplication.

    I do not have access to the program main function - it is hidden away inside boost somewhere. How do I call QCoreApplication so that QObjects are properly destroyed and the boost test framework properly runs?

    I am using:
    QT: 4.6.2
    Boost 1.42
    Windows Visual Studio 2008 SP1 Version 9.0.30729.1
    Microsoft .NET 3.5 SP1
    Windows XP SP3

    I'm calling this from the beginning of test case 1 - it never returns.

    Qt Code:
    1. int initQT()
    2. {
    3. BOOST_TEST_MESSAGE("Initializing QT...");
    4.  
    5. // fake some data for QCodeApplication
    6.  
    7. int argc = 1;
    8. char argv1[] = "IRL_QT.exe";
    9. char* argv[] = {argv1, NULL};
    10. QCoreApplication qca(argc, argv);
    11. return qca.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    With memory leaks errors:

    1>Detected memory leaks!
    1>Dumping objects ->
    1>{253997} normal block at 0x0091D968, 24 bytes long.
    1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
    1>{253996} normal block at 0x0091C0A8, 4 bytes long.
    1> Data: <h > 68 D9 91 00
    1>{252618} normal block at 0x00910928, 24 bytes long.
    1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
    1>{252617} normal block at 0x003EFB60, 4 bytes long.
    1> Data: <( > 28 09 91 00
    1>{251898} normal block at 0x00912FC8, 24 bytes long.
    1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
    1>{251897} normal block at 0x00912F98, 4 bytes long.
    1> Data: < / > C8 2F 91 00
    1>{251677} normal block at 0x00913030, 24 bytes long.
    1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
    1>{251676} normal block at 0x009112F8, 4 bytes long.
    1> Data: <00 > 30 30 91 00
    1>{221032} normal block at 0x0092B108, 4097 bytes long.
    1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
    1>{221011} normal block at 0x0092A0D8, 4097 bytes long.
    1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
    1>{220990} normal block at 0x009290A8, 4097 bytes long.
    1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
    1>{220969} normal block at 0x00928078, 4097 bytes long.
    1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
    1>{220948} normal block at 0x00927048, 4097 bytes long.
    1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
    1>{220927} normal block at 0x00926018, 4097 bytes long.
    1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
    1>{198116} normal block at 0x009176A8, 4097 bytes long.
    1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
    1>{198095} normal block at 0x00916678, 4097 bytes long.
    1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
    1>{198074} normal block at 0x00913160, 4097 bytes long.
    1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
    1>{129667} normal block at 0x00916630, 24 bytes long.
    1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
    1>{129666} normal block at 0x00915358, 4 bytes long.
    1> Data: <0f > 30 66 91 00
    1>{128143} normal block at 0x00910528, 24 bytes long.
    1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
    1>{128142} normal block at 0x003E79F0, 4 bytes long.
    1> Data: <( > 28 05 91 00
    1>{127285} normal block at 0x00924FE8, 4097 bytes long.
    1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
    1>{127264} normal block at 0x009141A0, 4097 bytes long.
    1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
    1>{127243} normal block at 0x009113B8, 4097 bytes long.
    1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
    1>{119426} normal block at 0x00912C88, 24 bytes long.
    1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
    1>{119425} normal block at 0x003E7570, 4 bytes long.
    1> Data: < , > 88 2C 91 00
    1>{117904} normal block at 0x0091C6B0, 24 bytes long.
    1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
    1>{117903} normal block at 0x0091AFF8, 4 bytes long.
    1> Data: < > B0 C6 91 00
    1>{94218} normal block at 0x0091B028, 4097 bytes long.
    1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
    1>{94197} normal block at 0x00919F48, 4097 bytes long.
    1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
    1>{94176} normal block at 0x009105D0, 24 bytes long.
    1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
    1>{94175} normal block at 0x003E81A0, 4 bytes long.
    1> Data: < > D0 05 91 00
    1>{94174} normal block at 0x00918F18, 4097 bytes long.
    1> Data: < > 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
    1>{47534} normal block at 0x00910980, 24 bytes long.
    1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
    1>{47533} normal block at 0x009101B8, 4 bytes long.
    1> Data: < > 80 09 91 00
    1>{1011} normal block at 0x00910EA0, 24 bytes long.
    1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
    1>{1010} normal block at 0x003E73C8, 4 bytes long.
    1> Data: < > A0 0E 91 00
    1>{766} normal block at 0x00910890, 24 bytes long.
    1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
    1>{765} normal block at 0x003E83A8, 4 bytes long.
    1> Data: < > 90 08 91 00
    1>{764} normal block at 0x00910638, 552 bytes long.
    1> Data: < H > 83 00 00 00 83 00 00 00 48 06 91 00 CD CD CD CD
    1>{753} normal block at 0x00910400, 24 bytes long.
    1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
    1>{752} normal block at 0x00910360, 116 bytes long.
    1> Data: <xT)g( 9g> 78 54 29 67 28 03 91 00 00 00 00 00 C0 E8 39 67
    1>{751} normal block at 0x00910328, 8 bytes long.
    1> Data: <8T)g` > 38 54 29 67 60 03 91 00
    1>{750} normal block at 0x009102E0, 24 bytes long.
    1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
    1>{749} normal block at 0x00910298, 24 bytes long.
    1> Data: < > 00 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
    1>{748} normal block at 0x00910230, 56 bytes long.
    1> Data: < ( > 01 00 00 00 28 03 91 00 00 CD CD CD 00 00 00 00

    Thanks,
    Clarence

  2. #2
    Join Date
    Feb 2012
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QT inside boost unit test - how to call QCoreApplication?

    did you get a solution???

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QT inside boost unit test - how to call QCoreApplication?

    Qt Code:
    1. int initQT()
    2. {
    3. BOOST_TEST_MESSAGE("Initializing QT...");
    4.  
    5. // fake some data for QCodeApplication
    6.  
    7. int argc = 1;
    8. char argv1[] = "IRL_QT.exe";
    9. char* argv[] = {argv1, NULL};
    10. QCoreApplication qca(argc, argv);
    11. return qca.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    learning, the op should have added something to the test that will call quit/exit on the application to make it end.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. #4
    Join Date
    Feb 2012
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: Using QT inside boost unit test - how to call QCoreApplication?

    MY code is this.

    Qt Code:
    1. #include <QApplication>
    2. #include <QPalette>
    3. #include <QPixmap>
    4. #include <QSplashScreen>
    5. #include <qthread.h>
    6.  
    7. #include "boost/scoped_ptr.hpp"
    8. #define BOOST_TEST_NO_MAIN
    9. #define BOOST_TEST_ALTERNATIVE_INIT_API
    10. #include <boost/test/unit_test.hpp>
    11. using namespace boost::unit_test;
    12.  
    13. #include "MainWindow.h"
    14.  
    15. class I : public QThread
    16. {
    17. public:
    18. static void sleep(unsigned long secs) {
    19. QThread::sleep(secs);
    20. }
    21. };
    22.  
    23.  
    24. BOOST_AUTO_TEST_SUITE( test_suite_1 )
    25.  
    26. BOOST_AUTO_TEST_CASE( test_case_1 )
    27. {
    28. BOOST_MESSAGE( "Testing is in progress" );
    29.  
    30. BOOST_CHECK( false );
    31. }
    32.  
    33. BOOST_AUTO_TEST_SUITE_END()
    34.  
    35. bool init_function()
    36. {
    37. return true;
    38. }
    39.  
    40. int main(int argc, char *argv[])
    41. {
    42.  
    43. QApplication app(argc, argv);
    44.  
    45.  
    46. QPixmap pixmap(":/images/betaLogo.png");
    47. QSplashScreen splash(pixmap);
    48. splash.show();
    49. splash.showMessage("Loading...");
    50. qApp->processEvents();
    51. I::sleep(2);//delay 2 sec
    52. ///Palette color setting for the MainWindow
    53. QPalette pal = app.palette();
    54. pal.setColor(QPalette::Window, Qt::darkGray);
    55. app.setPalette(pal);
    56. ///Calling Main Window
    57. MainWindow window("artists");
    58. window.show();
    59. //Makes the splash screen wait until the widget mainWin is displayed
    60. splash.finish(&window);
    61. //app.exec();
    62. return ::boost::unit_test::unit_test_main( &init_function, argc, argv );
    63.  
    64. }
    To copy to clipboard, switch view to plain text mode 

    I am now getting a linker error

    main.cpp.4.o : error LNK2019: unresolved external symbol "int __cdecl boost::unit_test::unit_test_main(bool (__cdecl*)(void),int,char * * const)" (?unit_test_main@unit_test@boost@@YAHP6A_NXZHQEAPE AD@Z) referenced in function main
    C:\cygwin\home\sobingt\tunebasket\p2p\build\test\U I\app.exe : fatal error LNK1120: 1 unresolved externals
    can u help me ??

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QT inside boost unit test - how to call QCoreApplication?

    yes, read boost docs on test suite. boost test is nothing to do with Qt.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. How can we make unit test for multithread in QT
    By learning_qt in forum Qt Programming
    Replies: 8
    Last Post: 15th January 2013, 16:09
  2. Unit Test Organisation, and having qmake what I want
    By jrharshath in forum Qt Programming
    Replies: 1
    Last Post: 15th September 2009, 18:59
  3. trying to call Xlib inside qt app
    By phixx in forum Qt Programming
    Replies: 4
    Last Post: 12th February 2008, 20:28
  4. Replies: 4
    Last Post: 20th February 2007, 12:35
  5. How to unit test a Qt Gui
    By mitskits in forum Qt Programming
    Replies: 1
    Last Post: 20th January 2006, 07:36

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.