PDA

View Full Version : Testing QApplication descendant with QTestLib



Oleg
5th April 2012, 13:52
Hello,

I'm trying to figure out how to use QTestLib with custom QApplication. So, I want to test if my command line aruments are handled correctly, for example language is set to the specified one (i.e. --set-lang=de). All the logic is placed in Application::exec().

So, the question is how to run Application::exec() from my test. Then I'd like to create MainWindow and access any label within it to test its text.

Thanks.

wysota
5th April 2012, 14:30
If you want to test the languages, you don't need to access any labels. All you need to do is to call tr() passing it a message id and context and check if the result is translated properly. Parsing the command line should be done before calling exec as exec() is a blocking call so it will not be possible to test anything within it. Remember QTestLib is about black-box testing so you can't test anything that doesn't provide some kind of result. Of course it is technically possible to do what you want however you'd be rewriting some of QTestLib functionality:


void TestClass::testobject() { QTest::qExec(this); }

int main(int argc, char **argv) {
MyApp app(argc, argv);
TestClass testobject;
QMetaObject::invokeMethod(&testobject, "startTest", Qt::QueuedConnection);
app.exec();
}