Monday, October 12, 2009

Automatically Running Unit Tests

QMake supports a useful post link step via the QMAKE_POST_LINK keyword that allows you to execute a command once your project has been built. One handy use for this is to execute unit tests - simply add something similar to your .pro file:

CONFIG(debug, debug|release) {
QMAKE_POST_LINK = ./debug/$${TARGET}.exe
else {
QMAKE_POST_LINK = ./release/$${TARGET}.exe

When linking is complete the appropriate EXE will be launched, and your tests will run (note that the above would need to be changed for Unix/Mac builds as the .exe file won't exist - I'll leave this as a lesson for the user.)

You can use this in conjunction with a subdirs project to ensure that your tests are executed whenever any of the code they reference changes.


  1. I am trying to make use of this trick but there is a problem - I want the unit test to be rebuilt/relinked against the target static library before it is run. It is a chicken and egg situation. Any suggestions?

    Also your tip about using a subdirs project may not work because Creator does not correctly relink dependant targets.

  2. Doesn't PRE_TARGETDEPS work for you? I have details of how to use it in another post.