Results 1 to 7 of 7

Thread: QtTest - use it in your application

  1. #1
    Join Date
    Apr 2011
    Posts
    132
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QtTest - use it in your application

    I am little concerned that I should use my .pro file to include CONFIG += qtestlib.

    I would like to write testing classes for my project. But is that mean that all testing classes will be build into my project ? How to hook up QtTest to existing project. Should it be a separate Testing project which includes to your testing.pro all headers and sources from project ?

    Thanks

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QtTest - use it in your application

    You can just create a seciton in you .pro file, something like this:
    Qt Code:
    1. // project.pro
    2.  
    3. // some regular stuff
    4. INCLUDEPATH += ...
    5. SOURCES += ..
    6. // etc
    7.  
    8. // here for testing
    9. test_conf{
    10. TARGET = my_app_test # different app name for testing suite
    11. QT += testlib
    12. SOURCES += test.cpp test2.cpp
    13. HEADERS += test.h ...
    14. ... other stuff for tests
    15. }
    To copy to clipboard, switch view to plain text mode 
    Then you can generate makefiles for test with qmake:
    Qt Code:
    1. qmake -config test_conf
    To copy to clipboard, switch view to plain text mode 
    After that, simply build the project as usual. Instead of your regular target, a my_app_test.exe will be created, using your test sources.

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

    migel (20th December 2011)

  4. #3
    Join Date
    Apr 2011
    Posts
    132
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QtTest - use it in your application

    Hi, thanks for answer.

    I am trying to build the first test but sort of does not work

    Qt Code:
    1. test.h:15: error: multiple definition of `main'
    To copy to clipboard, switch view to plain text mode 

    This is the knows issue. Adding a lines below tare not helping

    Qt Code:
    1. #ifdef Q_WS_X11
    2. QTEST_MAIN(Test)
    3. #include "test.moc"
    4. #else
    5. QTEST_NOOP_MAIN
    6. #endif
    To copy to clipboard, switch view to plain text mode 

    A file #include "test.moc" does not exists. I have tried with out it too. Base on this howto all I am doing is correct. so what's wrong ?

    http://developer.qt.nokia.com/doc/qt...a-ea7744df50ea

  5. #4
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtTest - use it in your application

    Quote Originally Posted by migel View Post
    Qt Code:
    1. test.h:15: error: multiple definition of `main'
    To copy to clipboard, switch view to plain text mode 
    When building unit tests, QtTest provides its own main function so you should not build the main function of your actual application.

    Qt Code:
    1. test_conf {
    2. # test specific files and options here
    3. SOURCEs += test.cpp
    4. ...
    5. } else {
    6. # app specific files and options here
    7. SOURCEs += main.cpp
    8. ...
    9. }
    10.  
    11. # common stuff here
    To copy to clipboard, switch view to plain text mode 
    Current Qt projects : QCodeEdit, RotiDeCode

  6. #5
    Join Date
    Apr 2011
    Posts
    132
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QtTest - use it in your application

    Yes I have done that. And it still complaining

    I have ran make clean, removed project.pro.user, removed Makefile manually and re ran the qmake -config test_conf.

    Qt Code:
    1. test.h:15: error: multiple definition of `main'
    2. test.h:15: error: first defined here
    3. :-1: error: collect2: ld returned 1 exit status
    To copy to clipboard, switch view to plain text mode 


    line 15: QTEST_MAIN(Test)

    What else I can do ?

    Found the problem

    QTEST_MAIN(Test) should be in source file .cpp not in the header.

    Thanks for all your help.

  7. #6
    Join Date
    Apr 2011
    Posts
    132
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QtTest - use it in your application

    One more thing to ask.

    I have two classes for testing, and how to run all tests from both classes the same time if I can't add

    Qt Code:
    1. QTEST_MAIN(Test1)
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. QTEST_MAIN(Test2)
    To copy to clipboard, switch view to plain text mode 

    the same time ?

    If I will enable it in both classes a compiler complains about the main method

    Qt Code:
    1. test1.cpp:57: error: multiple definition of `main'
    2. test2.cpp:24: error: first defined here
    3. :-1: error: collect2: ld returned 1 exit status
    To copy to clipboard, switch view to plain text mode 

    Thanks

  8. #7
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtTest - use it in your application

    The QTEST_MAIN macro creates a main function so you can only have one per application. The standard approach is to have a different executable for each unit test. This is easy to do with CMake, but more annoying with qmake. An alternative is to write your own main function that does multiple unit tests.

    The code below could prove useful if you take the second path :
    Qt Code:
    1. // header
    2.  
    3. class Test {
    4. public:
    5. static Test* get();
    6.  
    7.  
    8. int runAll(int argc, char **argv);
    9. void addTestObject(QObject *o);
    10.  
    11.  
    12. private:
    13. Test();
    14. ~Test();
    15.  
    16.  
    17. QList<QObject*> m_testObjects;
    18. };
    19.  
    20.  
    21. template <class T>
    22. class TestRegistar {
    23. public:
    24. inline TestRegistar(const char *name) {
    25. T *test = new T;
    26. test->setObjectName(name);
    27. Test::get()->addTestObject(test);
    28. }
    29. };
    30.  
    31. #define TEST(T) \
    32. static ::TestRegistar<T> _test_registar_##T(""#T);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //source
    2.  
    3.  
    4. Test* Test::get() {
    5. static Test inst;
    6. return &inst;
    7. }
    8.  
    9.  
    10. /*!
    11.   \brief Run all unit tests
    12.  
    13.  
    14.   The following arguments are recognized :
    15.   <ul>
    16.   <li>--restrict : only run tests to which an option is passed
    17.   <li>@&lt;testname&gt;[:&lt;option&gt;] : pass an argument to a single test.
    18.   The test will be run with --restrict on even if the option is empty
    19.   </ul>
    20.   All other arguments are forwarded to all unit tests. Arguments passed to test
    21.   can be any of those supported by regular QtTest-based unit tests.
    22. */
    23. int Test::runAll(int argc, char **argv) {
    24. bool restrict = false;
    25. QStringList common;
    26. QHash<QString, QStringList> specific;
    27. for (int i = 0; i < argc; ++i) {
    28. QString s = QString::fromLocal8Bit(argv[i]);
    29. if (i && *argv[i] == '@') {
    30. int idx = s.indexOf(':');
    31. if (idx != -1)
    32. specific[s.mid(1, idx - 1)] << s.mid(idx + 1);
    33. else
    34. specific[s.mid(1)] = specific.value(s.mid(1));
    35. } else if (!qstrcmp(argv[i], "--restrict")) {
    36. restrict = true;
    37. } else {
    38. common << s;
    39. }
    40. }
    41.  
    42.  
    43. int ret = 0;
    44. foreach (QObject *o, m_testObjects) {
    45. if (restrict && !specific.contains(o->objectName()))
    46. continue;
    47. QStringList args = common;
    48. args << specific.value(o->objectName());
    49. if ((ret = QTest::qExec(o, args)))
    50. break;
    51. }
    52. return ret;
    53. }
    54.  
    55.  
    56. void Test::addTestObject(QObject *o) {
    57. m_testObjects << o;
    58. }
    59.  
    60.  
    61. Test::Test() {
    62. }
    63.  
    64.  
    65.  
    66. Test::~Test() {
    67. qDeleteAll(m_testObjects);
    68. }
    To copy to clipboard, switch view to plain text mode 

    With that, you can register multiple unit test classes with the TEST macro and invoke them all from anywhere you fancy (but most likely from a main()) via "Test::get()->runAll(argc, argv)"
    Current Qt projects : QCodeEdit, RotiDeCode

  9. The following user says thank you to fullmetalcoder for this useful post:

    migel (23rd December 2011)

Similar Threads

  1. qttest output
    By BalaQT in forum Qt Programming
    Replies: 3
    Last Post: 28th May 2011, 17:17
  2. QtTest and asserts
    By doberkofler in forum Qt Programming
    Replies: 11
    Last Post: 21st September 2010, 07:22
  3. QTTest and my own gui
    By GrahamLabdon in forum Newbie
    Replies: 0
    Last Post: 19th March 2010, 10:26
  4. QtTest example not compiling
    By GrahamLabdon in forum Newbie
    Replies: 3
    Last Post: 19th March 2010, 09:41
  5. QtTest bug
    By graeme in forum Qt Programming
    Replies: 4
    Last Post: 19th February 2006, 21:16

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.