简体   繁体   中英

Unit testing (non-Qt) C++ code in Qt Creator?

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.

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

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 eg haven a nice graphical output and the ability to use a mocking framework (GMock).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM