views:

1595

answers:

6

I am just starting up a new project that needs some cross-platform GUI, and we have chosen Qt as the GUI-framework.

We need a unit-testing framework, too. Until about a year ago we used an in-house developed unit-testing framework for C++-projects, but we are now transitioning to using Google Test for new projects.

Anyone have any experience with using Google Test for Qt-applications? Is QtTest/QTestLib a better alternative?

I am still not sure how much we want to use Qt in the non-GUI parts of the project - we would probably prefer to just use STL/Boost in the core-code with a small interface to the Qt-based GUI.

EDIT:

Looks like many are leaning towards QtTest. Any experiences with integrating this with a Continous Integration server? Also, it would seem to me that having to handle a separate application for each new test case would cause a lot of friction. Any good ways to solve that? Does Qt Creator have a good way of handling such test cases or would you need to have a project per test case?

+3  A: 

Why not using the unit-testing framework included in Qt? An example : http://doc.trolltech.com/4.1/qtestlib-tutorial1.html.

Patrice Bernassola
+1  A: 

QtTest is mostly useful for testing parts that require the Qt event loop/signal dispatching. It's designed in a way that each test case requires a separate executable, so it should not conflict with any existing test framework used for the rest of the application.

(Btw, I highly recommend using QtCore even for non-GUI parts of the applications. It's much nicer to work with.)

Lukáš Lalinský
A: 

If you are using Qt, I would recommend using QtTest, because is has facilities to test the UI and is simple to use.

If you use QtCore, you can probably do without STL. I frequently find the Qt classes easier to use than the STL counterparts.

drhirsch
+2  A: 

I don't know that QTestLib is "better" than one framework for another in such general terms. There is one thing that it does well, and that's provide a good way to test Qt based applications.

You could integrate QTest into your new Google Test based setup. I haven't tried it, but based on how QTestLib is architected, it seems like it would not be too complicated.

Tests written with pure QTestLib have an -xml option that you could use, along with some XLST transformations to convert to the needed format for a continuous integration server. However, a lot of that depend upon with CI server you go with. I would imagine the smae applies to GTest.

A single test app per test case never caused a lot of friction for me, but that depends on having a build system that would do a decent job of managing the building and execution of the test cases.

I don't know of anything in Qt Creator that would require a seperate project per test case but it could have changed since the last time I looked at Qt Creator.

I would also suggest sticking with QtCore and staying awhile from the STL. Using QtCore throughout will make dealing with the GUI bits that require the Qt data types easier. You won't have to worry about converting from one data type to another in that case.

Matt Rogers
A: 

For GUI testing, try Squish.

You might also want to consider KD Executor.

These have both been around since Qt 3.

I also noticed a blog post about unit testing with Qt Script.

Sam Dutton
+1  A: 

You don't have to create separate tests applications. Just use qExec in an independent main() function similar to this one:

int main(int argc, char *argv[])
{
    TestClass1 test1;
    QTest::qExec(&test1, argc, argv);

    TestClass2 test2;
    QTest::qExec(&test2, argc, argv);

    // ...

    return 0;
}

This will execute all test methods in each class in one batch.

Your testclass .h files would look as follows:

class TestClass1 : public QObject
{
Q_OBJECT

private slots:
    void testMethod1();
    // ...
}

Unfortunately this setup isn't really described well in the Qt documentation even though it would seem to be quite useful for a lot of people.

Joe