Nominate our 2022 Qt Champions!

Writing Unit Tests with QtTest that access objects in another project

  • I have a fairly large QT Project in QT Creator where I wish to test various functions. It appears that there is no easy way, however, to create the unit tests in the same project that the tested objects reside. This is creating numerous problems, including the need to create either a static lib and link to that from the unit test project, or list each .obj file in the LIB var. Either is impractical.

    So I created a separate project to hold the first group of Tests using QtTest. But I was wondering how other people structure this sort of thing. Ideally, I would want to have the unit tests in the same project, but that doesn't seem possible because it looks like the only way to invoke the tests is either through the QTEST_MAIN macro or qexec().

    Has anyone successful done something like this?

  • I have been following the same policy - separate unit test project and library projects. I usually build dynamic libraries and just link to it in the unit test project as well as specify the library project as a dependency for the test project. This is even more of an issue on Mac OS X, since you need to copy the dylib file into the unit test project build directory to satisfy the runtime loader.

  • I was afraid that was the case. So then I suppose I will have to create either a DLL or static Library within the process that builds my project that I wish to test, so whenever the actual test project(s) is executed, it will be testing the latest code.

    This seems highly unproductive and unlike other testing frameworks I have used for other frameworks(Java, .NET).

  • Btw, thank you for the respond! :)

  • Yes, it is a bit different from how integrated unit testing is with Java/.NET etc., but I do not think it is that unproductive. The primary target in each of my IDE sessions is the test project. I add code to my library project, add unit test code to the test project and just hit run. Since the library project is marked as a dependency of the test project, it automatically builds the library and then builds and executes the tests. The end result is you get most of the same type of integration as with Java/.NET

Log in to reply