Results 1 to 10 of 10

Thread: QTestLib questions not covered by the manual.

  1. #1

    Question QTestLib questions not covered by the manual.

    I'm very new to QT, although I did a lot of C++ many years ago.

    I've got a project set up, and a couple files. I'm using QTCreator and QT 4.5 (although I don't think this question is particularly version-specific).

    I decided I wanted to try and write some tests.

    What isn't clear to me, after reading the manual and the first couple pages of the tutorial is: What's the best practice for locating unit tests, what project do they go into, etc?

    It looks like the manual assumes you're building a completely separate project for unit testing?
    The syntax to execute an autotest takes the following simple form:

    testname [options] [testfunctions[:testdata]]...

    Substitute testname with the name of your executable. testfunctions can contain names of test functions to be executed. If no testfunctions are passed, all tests are run. If you append the name of an entry in testdata, the test function will be run only with that test data.
    Is there a way to run tests if you build them into your main project?

    Is there an easy way to build a separate test project against the compiled output of your main project, rather than re-including all the same source files? (I've gotten spoiled by my dual-quad-core-cpu box at work, the old G4 I'm using at home has compile times measured in minutes...)

    And this may be a more general project layout question, but if I want to have my classes separated into logical groups, it seems like the way to do that is with subdirectories with .PRI files in them, and one main project including all the PRIs. Any advice how to group tests with the code in the file system and/or in projects?

    Thanks,
    Jeff

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

    Default Re: QTestLib questions not covered by the manual.

    Quote Originally Posted by jeffadams78 View Post
    It looks like the manual assumes you're building a completely separate project for unit testing?
    Yes, that's correct.

    Is there a way to run tests if you build them into your main project?
    In practice - no. The assumption is tests are separate applications.

    Is there an easy way to build a separate test project against the compiled output of your main project, rather than re-including all the same source files? (I've gotten spoiled by my dual-quad-core-cpu box at work, the old G4 I'm using at home has compile times measured in minutes...)
    You can create the bulk of your application as a library (static or dynamic) and then link the test (and your main application) against it. Then you won't have to recompile the same files all over.

    And this may be a more general project layout question, but if I want to have my classes separated into logical groups, it seems like the way to do that is with subdirectories with .PRI files in them, and one main project including all the PRIs. Any advice how to group tests with the code in the file system and/or in projects?
    There are no rules related to groupping subprojects. You can have all tests in a single binary or each test in a separate binary. From QTestLib's point of view this doesn't matter. If you have multiple tests within one binary you can still call only those tests that you desire by using proper command line options.

    The whole idea behind the current architecture of tests is that the test application is completely separate from the "real" project - you can move it anywhere (i.e. to a different machine) and it will still work.
    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. #3

    Default Re: QTestLib questions not covered by the manual.

    Thanks, that helps. Should a test app be a console or Gui app? I'm guessing console...

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

    Default Re: QTestLib questions not covered by the manual.

    It depends what you mean by "GUI app" in this case. You can test the GUI so it can be a "GUI app" but the test interface is console based as the goal of having such tests is to automate them.
    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.


  5. #5

    Default Re: QTestLib questions not covered by the manual.

    When I say "new project" in QTCreator, I get the choice of "GUI App", "Console App", or "Library". It uses it to set the TEMPLATE line in the project file. So sounds like the answer is "Console App".

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

    Default Re: QTestLib questions not covered by the manual.

    There are really no differences between a "GUI" and "console" app. You can choose either of the two, especially as you can switch between the two by changing entries in the .pro file. The only difference is whether the QtGui module is included or excluded from the project and you will want that included if you are testing a graphical application.
    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.


  7. #7

    Default Re: QTestLib questions not covered by the manual.

    How do you run multiple test classes?

    I've got a directory structure like this:

    Qt Code:
    1. /proj
    2. proj.pro
    3. main.cpp
    4. /tests
    5. projtests.pro
    6. main.cpp
    7. /data
    8. data.pri
    9. dataclass.h
    10. dataclass.cpp
    11. /tests
    12. datatests.pri
    13. dataclasstests.h
    14. dataclasstests.cpp
    To copy to clipboard, switch view to plain text mode 

    Every dir has a /tests dir beneath it with tests, and main.pro includes data.pri whereas maintests.pro includes datatests.pri.

    The only thing is, I can't figure out how to run tests when set up like this?
    The tutorial says you have to do this in your test class:
    Qt Code:
    1. QTEST_MAIN(<test class name>)
    2. #include "<test class name>.moc"
    To copy to clipboard, switch view to plain text mode 

    But as far as I can tell, QTEST_MAIN creates a main function that tests that one class. I'm going to have multiple classes to test, and I want the test main function to live in /proj/tests/main.cpp.

    Also the #include "<name>.moc" always shows a warning in QtCreator that the file doesn't exist, although there is no compile-time error.

    How do you do this?
    Last edited by jeffadams78; 21st March 2009 at 14:46. Reason: fixing formatting

  8. #8
    Join Date
    Feb 2010
    Posts
    20
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: QTestLib questions not covered by the manual.

    There are no rules related to groupping subprojects. You can have all tests in a single binary or each test in a separate binary. From QTestLib's point of view this doesn't matter. If you have multiple tests within one binary you can still call only those tests that you desire by using proper command line options.
    how can i call only one test at time if i have all test cases in one binary?
    for example :

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    TestClass1 obj;
    QTest::qExec(&obj, argc, argv);

    TestClass1 obj1;
    QTest::qExec(&obj1, argc, argv);

    TestClass2 obj2;
    QTest::qExec(&obj2, argc, argv);
    return 0;
    }

    what are the command line options for that. i did not find any suitable on e in manual or net?


    Thanks!

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

    Default Re: QTestLib questions not covered by the manual.

    These are not test cases actually. If you want to call only some of the three classes you have here then simply use argc and argv with your own custom arguments to check which of the three classes the user wanted to run. The thing that is already provided by Qt is deciding which of the tests (private slots) of the actuall test class to perform.
    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.


  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTestLib questions not covered by the manual.

    And this may be a more general project layout question, but if I want to have my classes separated into logical groups, it seems like the way to do that is with subdirectories with .PRI files in them, and one main project including all the PRIs. Any advice how to group tests with the code in the file system and/or in projects?
    You could also look at the "subdirs" template in the QMake manual to get recursive directory processing. There's also a feature (undocumented AFAICT) where a PRO file can be listed as if it were a sub directory: you can process a second PRO in the directory and also recursively handle subdirectories.

Tags for this Thread

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.