PDA

View Full Version : Take QtTestLib into use in a Qt project



phenoboy
20th March 2013, 09:34
What is the best and easiest way to integrate unit test library, QtTestLib, into existing Qt project with about 50 classes?

- Can I create for example 'test' folder under main project and put all test classes there including test application project file? How about including classes from existing project
- Do I have to copy from existing qt project file all the INCLUDES, LIBS, various CONFIGS into test application project file ?

Are there any example (large) projects where it is used?

anda_skoa
20th March 2013, 20:19
Creating a test subdirectory is a very common approach.
Basicalyl rename your current main .pro file create a new one using template subdirs and putting the previous file as well as tests (name of the test subdirectory) into its SUBDIRS variable,

Or you restructure the project to have two (or more) subdirectories, one building the application.

For compiling, one option is to compile all needed classes into the test by adding them to the test's .pro file.
One other, usually more convenient, option is to put all classes into a static library and linking the main application and each test application against it.

If you need to share settings between the main application build and the test builds, put those into a .pri file (same syntax as .pro, basically a .pro include) and include it in each .pro file.

Cheers,
_

phenoboy
21st March 2013, 09:16
Creating a test subdirectory is a very common approach.
Basicalyl rename your current main .pro file create a new one using template subdirs and putting the previous file as well as tests (name of the test subdirectory) into its SUBDIRS variable,


Ok, this is the part I do not fully understand. I created new Qt Unit Test project with Qt Creator under 'test' directory.


QT += testlib

TARGET = tst_unittitestitest
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

INCLUDEPATH += ../


SOURCES += tst_unittitestitest.cpp
DEFINES += SRCDIR=../

Should I add all the LIBS and INCLUDEPATH's from main application to this test.pro file. Is there easier way just to include all that is in main .pro file ?
It is very tedious to maintain two .pro files and have basically two copys of INCLUDEPATH's in two .pro files.

I know Qt is smarter than I think. So please help :)

anda_skoa
22nd March 2013, 16:52
Assuming ../ in your INCLUDEPATH refers to the top level directory of your project, then you can create a file like this (e.g. common.pri) in the top level directory



INCLUDEPATH += $$dirname(_FILE_)
LIBS #whatever you need


The all .pro files can simple include it. For example in your test



include(../common.pri)


Cheers,
_

phenoboy
13th September 2013, 10:33
Really having a problem here...

I'm still having a problem setting up a project with qttestlib. I get unresolved external symbols:



myclass.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall MyClass::metaObject(void)const " (?metaObject@MyClass@@UBEPBUQMetaObject@@XZ)

myclass.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall MyClass::qt_metacast(char const *)" (?qt_metacast@MyClass@@UAEPAXPBD@Z)

myclass.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall MyClass::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@MyClass@@UAEHW4Call@QMetaObject@@HPA PAX@Z)
debug\tst_testtest.exe : fatal error LNK1120: 3 unresolved externals


Here's my current project zipped.

http://phenoboy.kapsi.fi/App.zip


Thanks!

anda_skoa
13th September 2013, 13:50
your tests.pro is missing the HEADERS line for the MyClass header

Cheers,
_

phenoboy
13th September 2013, 14:24
your tests.pro is missing the HEADERS line for the MyClass header

Cheers,
_

yes, it works thanks. Now I have to move SOURCES and HEADERS to own .pri file because I don't like to copy stuff to two places each time I add a new class in main project.