How to write the qmake file for a test case? [duplicate]

一个人想着一个人 提交于 2019-12-13 12:49:42

问题


I just don't understand what is the overall layout of a Qt project with a program and a test...

The project of QTest tutorial only have the test program, but my project already have another program. If I add the test case, it claims "multiple definition of main()", as QTEST_MAIN is actually another main().

In addition, I got "undefined reference to vtable" on my test class, and don't know why..

I'm using Qt 5.2.1

This is my project file:

#-------------------------------------------------
#
# Project created by QtCreator 2014-06-06T13:42:19
#
#-------------------------------------------------

QT       += core gui testlib
CONFIG += testcase

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = GutMiner
TEMPLATE = app
LIBS += -lquazip


SOURCES += main.cpp\
        mainwindow.cpp \
    dataform.cpp \
    datavec.cpp \
    distance.cpp \
    linereader.cpp \
    diseasepackage.cpp \
    error.cpp \
    newpagedialog.cpp \
    resultpage.cpp \
    test.cpp

HEADERS  += mainwindow.h \
    dataform.h \
    distance.h \
    datavec.h \
    linereader.h \
    diseasepackage.h \
    error.h \
    newpagedialog.h \
    resultpage.h

FORMS    += mainwindow.ui \
    dataform.ui \
    newpagedialog.ui

and this is my test source file:

#include <QObject>
#include <QTest>

#include "distance.h"
#include "diseasepackage.h"

class TestDistance: public QObject
{
    Q_OBJECT
public:
    virtual ~TestDistance();
private slots:
    void jensen_shannon();
};

TestDistance::~TestDistance() {}

void TestDistance::jensen_shannon()
{
    DiseasePackage pkg("CRC.zip");
}

QTEST_MAIN(TestDistance);

回答1:


One way to make this work properly (the one I use with Qt 4.8) is to have a separate .pro file for the test program.

The main program's .pro file does NOT include the test code.

QT       += core gui qt3support xml script

TARGET = simui
TEMPLATE = app

SOURCES += main.cpp\
    <lots of other source files

HEADERS  += \
    < header files>

FORMS    += \
    < form files > 

The test program's .pro file does NOT include main.cpp. The test programs's .pro file does include the test library:

QT      += core gui qt3support xml script
CONFIG  += qtestlib

TARGET = testsimui
TEMPLATE = app

SOURCES += \
    < all the sources form the main program (except main.cpp!) > 
    < all the test code sources. >

HEADERS += \
    < all the headers from the main program >
    < all the test code headers> 

FORMS    += \
    < all the forms from the main program > 

This isn't necessarily the best setup, but it does work nicely, though you end up having to add each source file to both .pro file.

As to your vtable problem, I don't think there's enough info in what you've given to make anything out of that. What class is having the problem? Also, I'm curious why you have an empty destructor on your test class.



来源:https://stackoverflow.com/questions/24153895/how-to-write-the-qmake-file-for-a-test-case

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!