Important: Please read the Qt Code of Conduct -

Help with QTest Framework

  • I've started writing unit tests with QTest. When I used the templated class in a single cpp file to write a basic test it worked as expected. But then I tried to move the class definitions over to a header file - and everything broke?! For instance: it suddenly started saying that there was no rule for the .moc file, among other randomness.

    Since Qt put it all in one file I have to ask if there is a technical reason for this? Or are there any examples of splitting a unit test up into a header and cpp file without error? I have a lot of unit tests that I want to write - and I really don't want to shove it all into one file and make a big mess of it.

    Also, I'd like to have multiple unit test classes in my project. Is there any easy way to register many unit tests for execution? Or do you just have to write your own main function and execute 'em manually?


  • @iam1me

    Hi and welcome to devnet forum

    My guess is you are using Qt creator.

    From your description it looks like the update of makefile from qmake is missing respectively you also to ensure that the .pro file knows about all dependencies. Typical issue is that one forgets to add new files in this case with the HEADERS statement

    Also it is sometimes good to rerun qmake and start a rebuild from creator.

  • Hey @koahnig

    Thanks for your input. After reading the QTest doc a little closer, I found that my problem was due to using the macros that add in a main function to run the test for you. It is written in such a manner that everything needs to be in one file. However, if you write your own little main function to process the tests then there is no problem splitting up the class definition and implementation across 2+ files.

    I am indeed using the Qt creator currently. I'm just getting started with Qt, any recommendations here?


  • @iam1me

    No, I think you are on the right by consulting also the docs.
    Some people tend to ask always questions instead of looking for themselves.

    You can create your test setup from inside of Qt creator. Whatever is there shall stay there.
    I thought that you were shifting else outside of the main part. It was not clear from your post to me.

    Glad you have found your issue.

Log in to reply