PDA

View Full Version : QTestLib questions not covered by the manual.



jeffadams78
15th March 2009, 14:05
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 (http://doc.trolltech.com/4.5/qtestlib-manual.html) and the first couple pages of the tutorial (http://doc.trolltech.com/4.5/qtestlib-tutorial.html) 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

wysota
15th March 2009, 17:34
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.

jeffadams78
15th March 2009, 18:10
Thanks, that helps. Should a test app be a console or Gui app? I'm guessing console...

wysota
15th March 2009, 19:55
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.

jeffadams78
15th March 2009, 20:37
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".

wysota
15th March 2009, 21:02
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.

jeffadams78
21st March 2009, 13:46
How do you run multiple test classes?

I've got a directory structure like this:


/proj
proj.pro
main.cpp
/tests
projtests.pro
main.cpp
/data
data.pri
dataclass.h
dataclass.cpp
/tests
datatests.pri
dataclasstests.h
dataclasstests.cpp

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:

QTEST_MAIN(<test class name>)
#include "<test class name>.moc"

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?

netmat
30th June 2010, 13:03
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!

wysota
9th July 2010, 08:46
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.

ChrisW67
10th July 2010, 05:35
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.