12
votes

I have an existing Qt Creator project which is not a Subdirs project. I have now reached a point where I want to add a unit test project to it. When I create the new unit test project, the "Add as a subproject to..." combo is disabled.

How can I do it? I'm thinking about creating a brand new Subdirs project and adding my existing project to it, and then adding the unit test project as well.

Is that how it should be done or is there a better way?

2
Yeah, you need to add an upper level project, which includes your current project as subdir project. This is probably done most easily from command line, I don't think Qt Creator has a helper for this (unless it's been added in a very recent version). You can use Qt Creator to create a subdir project for you, and then maybe add dummy app and test projects under it. Once you see how that works, then do it for your real project by hand. - hyde

2 Answers

11
votes

It's not a problem to change the project type to a subdir project (even for a gui app). That's the only way to add a unit test sub project to it. Steps:

  1. As mentioned, make your project a subdir project:

    TEMPLATE = subdirs

    SUBDIRS = gui logic

  2. Select File > New file or project ... > Other Project > Qt Unit Test

  3. At the last page "summary" of the wizard, select your project for "Add as a subproject to project"
  4. Your .pro file will be updated as follows:

    SUBDIRS = gui logic tests

Note: If you're manually restructuring the project hierarchy, make sure the name of the sub directory matches the name of .pro file within that sub directory.

For example:

  • sub directory name = test --> pro file name = test.pro
  • sub directory name = src --> pro file name = src.pro
0
votes

No I think you do need to create a subdir project. You can go to Qt - Adding Subprojects to Projects and read the second paragraph.