My blog
by
In order to create an Qt project with tests, I stumbled upon a very helpful tutorial and repository here.
However, I was not fully satisfied. For one, the library was missing the __declspec
attribute so the DLL would not work. Besides, I couldn’t find a way to automate my builds (so you could use build servers or add some simple CI).
In this write-up, I will cover the following things:
Here is what I came up with.
After installing QtCreator and Visual Studio, you simply create a new subdirs project. Then, you create your Library (rightclick, New Subproject), your tests (Other Project, Qt Unit Test) and your actual app (Application, Qt Widgets Application). I chose this structure to have the business logic in an interface agnostic library with separated tests, while the UI has it’s own project.
After creating the projects, I added the library to the application’s and test’s project file. To add the __declspec
attribute to the library, I created a simple samplelibrary_global.h
header, as suggested in the Qt wiki. This enables DLL export of classes like this:
When I added the Qt Test plugin, I could see the test results with alt+shift+t, alt+a
so I could do some TDD. QtTest has some quirks but works fine for my purposes.
The most difficult task was to automate the build. Qt has some windows deployment tools, but they didn’t work the way I wanted them to so I wrote a little Powershell script.
Surprisingly, I couldn’t find a lot of help on SO that was Windows specific. To build the project, Qt
vcvarsall.bat
jom.exe
For deployment, you still have to copy the Qt DLLs. I could replicate that in PS (Using MS C++ compiler, x64, release build):
Link to the finished project
tags: