5

I have a C++ project that doesn't use Qt. I am using Qt Creator as my IDE because it is very convenient.

I was reading about unit testing in Qt Creator here, and I think that Qt Test seems quite good. Is there a way to use Qt Test with my non-Qt C++ project?

Note that I have Qt libraries installed in my PC which I am using to program. But I don't want that the compiled program I release depends on the Qt libraries.

Also, a feature that I liked when I was using Visual Studio is that the tests are run automatically everytime I build my project. Can I do the same with Qt Creator?

By the way, if you can suggest another unit testing framework that I can use easily in Qt Creator, that's also fine.

a06e
  • 18,594
  • 33
  • 93
  • 169
  • 1
    Personally I find there is hardly any support for testing in Qt Creator and official documentation for more complex tests with Qt is missing. – Simon Jun 29 '15 at 13:14
  • You probably need to set up a Qt project for testing alone, both project sharing common testee files – UmNyobe Jun 29 '15 at 13:17
  • @Simon Do you have any other suggestion that I can use with Qt Creator? – a06e Jun 29 '15 at 13:19
  • @UmNyobe Can you give a more detail? – a06e Jun 29 '15 at 13:30
  • You probably could use Qt Test (or e.g. Boost.Test) as post-build event. – Simon Jun 29 '15 at 19:10
  • What project building system are you using? `cmake`, `qmake` (you don't have to use Qt code to use `qmake`) etc. Depending on what system you are using integration with various testing tools might vary greatly. `google test` for example is very easy to use in `cmake`. – rbaleksandar Dec 30 '16 at 08:56

3 Answers3

3

You can also unit test (non-Qt) C++ code in Qt Creator with GTest:

https://github.com/google/googletest/

using this great QtCreator plugin:

https://github.com/OneMoreGres/qtc-gtest

I switched from QTest to GTest (for testing Qt applications) for several reasons e.g. haven a nice graphical output and the ability to use a mocking framework (GMock).

jxramos
  • 7,356
  • 6
  • 57
  • 105
avb
  • 1,701
  • 5
  • 22
  • 37
  • Update: QT Creator now supports adding Google Test projects without the need for the 3rd party plugin. In the New Project wizard, select Other Project > Auto Test Project. The wizard will have a screen that asks which test suite you want to use. – Bob Liberatore Nov 07 '21 at 16:02
2

Never actually tried to unit test non qt code in QtCreator, but the Qt nature of the code don't matter. That's how I would go :

  1. Take your normal, non qt project
  2. Add a parent project of type subdir to act like a visual studio solution would.
  3. Add another project under the parent, which is a qt test project, ie

    QT  += testlib
    CONFIG += qtestlib
    
  4. Find a way to share sources between both. You can either add source files using their relative path, create a static lib, or use a pri file (project header file) for both non qt and qttestlib projects.

With this configuration, by building the top level project, you will build your normal project and run the test as well.

with subdir projects templates you get from ctrl+N are not very helpful. But the top level project file can be made as simple as

include (commonsrc.pri)

TEMPLATE = subdirs
CONFIG  += ordered

SUBDIRS += appdir
SUBDIRS += testingdir

That's what the top level of one my projects look like.

To avoid headaches match folder hierarchy with subdir hierarchy. For instance

solutiondir
     commondir
     appdir
     testingdir
     commonsrc.pri <--- allow you to write commondir\mycpp.cpp in any project
Community
  • 1
  • 1
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
0

You can try Boost's unit test framework

http://www.boost.org/doc/libs/1_55_0/libs/test/doc/html/index.html

cosmarc
  • 562
  • 3
  • 19