ImaginativeThinking.ca


A developers blog

How to Run your UnitTests as A Post Build Step in QtCreator

By: Brad

In my previous post What the Heck is a Unit Test I said that there is no excuse for not running your unit tests before each commit, even if your not using Visual Studio which has a brilliant built in test runner, because you can simply add them as a post build step.

After saying that I realized that every Qt project I’ve worked on was a Windows application and as such we used Visual Studio as the IDE; I hadn’t actually ever setup a post build step in QtCreator before. Not wanting to sound like a jerk telling you all to do something I don’t know how to do I set forth to figure it out.

In this post I’ll walk you though how to setup a post build step in QtCreator.

Download the QtQuick Sample Application and open it in QtCreator. On the left hand side of the QtCreator GUI notice that there is a tool bar strip running along it. Click on the button in the tool bar called Projects.

QtCreator_PostBuildStep_1

This opens the project settings interface and wouldn’t you know it one of the project settings is Build Steps. Clicking on the Add Build Step button will expand a drop down menu where you can select Custom Process Step.

QtCreator_PostBuildStep_2

In the fields that show up when you click on the Custom Process Steps you can enter the command you want the step to run; this could be a command to run a batch/bash/ant script if the execution of your unit tests requires multiple commands or you can have it call your test running application directly.

QtCreator_PostBuildStep_3

At this point if you compile your application you’ll notice in the Compile Output window after your code is compiled, the console messages of your test runner application will be displayed. If any of your unit tests fail the Custom Process Step will fail giving you immediate feed back that the change you just made caused an unexpected change to the applications behaviour.

So there you have it, its not as pretty as Visual Studio but it gets the job done. Now you have no excuse to run your unit tests before you commit because they are run each time you compile your code automatically.

Oh but wait there is a problem.

As it turns out this Custom Process Step we just created is NOT saved in the *.pro file like one use to Visual Studio might expect but rather they are saved in the *.pro.user file. Why is that a problem? Well you work on a team with many developers working on the same application typically the *.pro file is checked into a source repository and shared among the team however the *.pro.user file is specific to your development environment and generally not checked in/shared with the other developers (user files typically contain hard coded paths that are unique to your development box).

But I want to share this benefit with the entire team of developers

So when I ran into this the last thing I wanted to do was pin a document to the wall telling every developer who ever works on this project past, present, or future to remember to manually configure the same Custom Process Step and I couldn’t fathom that the Qt guys didn’t think of this when coming up with QtCreator. So I did a little digging and sure enough they didn’t let me down; I just wasn’t coming from the same point of view as they were (I’m to use to Visual Studio’s paradigms I guess).

In QtCreator it is possible to Share user settings.

You do this simply by taking your *.pro.user file, which is actually just plan text file containing XML, stripping out anything you don’t want to share (like settings that are set to hard coded paths only useful to you) and saving it as a *.pro.shared file. If you check that in with your *.pro file both your project settings and the Custom Process Step will be shared among your entire team.

How that works is that when QtCreator opens a project it first looks for a *.pro.shared file in the same directory as the *.pro file it just opened. If one exists QtCreator will apply those settings found in the *.pro.shared file; it then looks for the *.pro.user file for additional settings. By looking at the files in this order users can override shared settings by explicitly setting them in their *.pro.user file.

And there you go we’ve now added a post build step in QtCreator for our project that will run our test runner application after each compilation and shared that setting automatically with the entire team.

You can download a version of the QtQuick Sample Application which has a shared custom post build step which runs its unit tests via this link.

Thank you I hope you have enjoyed and found this tutorial helpful. Feel free to leave any comments or questions you might have below and I’ll try to answer them as time permits.

Until next time think imaginatively and design creatively

Brad

My interest in computer programming started back in high school and Software Development has remained a hobby of mine ever since. I graduated as a Computer Engineering Technologist and have been working as a Software Developer for many years. I believe that software is crafted; understanding that how it is done is as important as getting it done. I enjoy the aesthetics in crafting elegant solutions to complex problems and revel in the knowledge that my code is maintainable and thus, will have longevity. I hold the designation Certified Technician (C.Tech.) with the Ontario Association of Computer Engineering Technicians and Technologists (OACETT), have been certified as a Professional Scrum Master level 1 (PSM I) and as a Professional Scrum Developer level 1 (PSD I) by Scrum.org as well as designated as an Officially Certified Qt Developer by the Qt Company. For more on my story check out the about page here

Feel free to write a reply or comment.