Hi :)
I’m currently trying to make Unit-Testing (QTest) work in my project. I took this [gitorious.org] project as an reference on how I could struture the whole thing.
However, I’ve been trying to find a solution for a “undefined reference”-error for hours now. Anyway, let me first describe what my project looks like now:
This is my current filestructure (please note that my project is a lot bigger, but these are the essential parts):
MyApp
|
| buildInfo.pri (A)
| libraryIncludes.pri (B)
| MyApp.pro (C)
|
+---build
| +---src
| \---tests
|
+---MyApp
| | MyApp.pro (D)
| | deployment.pri
| | inputvectors.h (E)
| | inputvectors.cpp (F)
| | main.cpp
| |
\---tests
| tests.pro (G)
\ testinputvectors.cpp (H)
File A (buildInfo.pri):
ROOT_DIRECTORY = $$PWD
BUILD_DIRECTORY = $${ROOT_DIRECTORY}/build
TESTS_DIRECTORY = $${BUILD_DIRECTORY}/tests
File B (libraryIncludes.pri):
INCLUDEPATH += $$PWD/MyApp
DEPENDPATH += $$PWD/MyApp
File C (MyApp.pro):
include(buildInfo.pri)
# This variable contains the name of the template to use when generating the project.
# "subdirs"-option: Creates a Makefile for building targets in subdirectories.
# The subdirectories are specified using the SUBDIRS variable. For more information see:
# http:#qt-project.org/doc/qt-5/qmake-variable-reference.html#template
TEMPLATE = subdirs
# specify directory for target file
DESTDIR = $${BUILD_DIRECTORY}
# where to place intermediate object files
OBJECTS_DIR = $${BUILD_DIRECTORY}
# where to place intermediate moc files
MOC_DIR = $${BUILD_DIRECTORY}
SUBDIRS += \
MyApp \
tests
File D (MyApp.pro):
include(../buildInfo.pri)
TEMPLATE = app
QT += qml \
quick \
gui \
widgets \
core
# specify directory for target file
DESTDIR = $${BUILD_DIRECTORY}
# where to place intermediate object files
OBJECTS_DIR = $${BUILD_DIRECTORY}
# where to place intermediate moc files
MOC_DIR = $${BUILD_DIRECTORY}
SOURCES += main.cpp \
inputvectors.cpp \
RESOURCES += qml.qrc
# Default rules for deployment.
include(deployment.pri)
HEADERS += \
inputvectors.h
File E (inputvectors.h):
#ifndef INPUTVECTORS_H
#define INPUTVECTORS_H
#include <QList>
#include <QString>
class InputVectors
{
public:
InputVectors();
InputVectors(QList<QString> columnHeadings,
QList<QList<unsigned int> > table);
QList<QString> getHeadings();
QList<QList<unsigned int> > getValues();
private:
QList<QString> columnHeadings;
QList<QList<unsigned int> > table;
};
#endif // INPUTVECTORS_H
File F (inputvectors.cpp):
#include "inputvectors.h"
InputVectors::InputVectors(QList<QString> columnHeadings,
QList<QList<unsigned int> > table)
{
this->table = table;
this->columnHeadings = columnHeadings;
}
QList<QList<unsigned int> > InputVectors::getValues()
{
return this->table;
}
QList<QString> InputVectors::getHeadings()
{
return this->columnHeadings;
}
File G (tests.pro):
include(../buildInfo.pri)
QT += testlib \
core
CONFIG += testcase
include(../libraryIncludes.pri)
DESTDIR = $${TESTS_DIRECTORY}
OBJECTS_DIR = $${TESTS_DIRECTORY}
MOC_DIR = $${TESTS_DIRECTORY}
SOURCES += \
testinputvectors.cpp
File H (testinputvectors.cpp):,
#include <QtTest/QtTest>
#include "inputvectors.h"
class TestInputVectors: public QObject
{
Q_OBJECT
private slots:
void openFile();
};
void TestInputVectors::openFile() {
QList<QString> columnHeadings;
columnHeadings.append("test");
QList<unsigned int> tmp;
tmp.append(2);
QList<QList<unsigned int> > table;
table.append(tmp);
InputVectors test = InputVectors(columnHeadings, table);
}
QTEST_MAIN(TestInputVectors)
#include "testinputvectors.moc"
When I change the “TEMPLATE = app” to “TEMPLATE = lib” in the MyApp.pro (D) file and link it inside the libraryIncludes.pri, it all works as expected. However, when I write the “TEMPLATE = app”, I get an error message:
testinputvectors.cpp:20: undefined reference to `InputVectors::InputVectors(QList<QString>, QList<QList<unsigned int> >)’
collect2.exe: error: ld returned 1 exit status
BTW: QtCreate does not mark the #include “inputvectors.h” with a wavy underline as it would normally do if it cannot find a file.
How can I access the functions to be tested from the testing-project?
Thanks in advance,
mozzbozz
TL;DR: I have two subprojects (main and tests) on the same layer where I want to test the main “MyApp”-project with the tests placed inside the “tests”-project. However, MyApp is not “TEMPLATE = lib” but “TEMPLATE = app” and I cannot figure out how to correctly access the functions of MyApp from tests.
↧
[SOLVED] QTest an subprojects: How to link against application with "TEMPLATE = app" configuration?
↧