Introduction to GUI Qt Test???
-
Good day,
I have created a subdir project that contains first a Qt GUI project of a currency converter (see picture below) and a second project, a QtTest to test the converter.
Here is the .pro file of the subdir project:TEMPLATE = subdirs SUBDIRS += \ CurrencyConverter \ QtTest tests.subdirs = QtTest app.subdirs = CurrencyConverter QtTest.depends = CurrencyConverter
and here is the .pro file of the Qt Test project
QT += testlib gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets DEFINES += QT_DEPRECATED_WARNINGS CONFIG += qt warn_on depend_includepath testcase TEMPLATE = app INCLUDEPATH += ../CurrencyConverter SOURCES += tst_testconverter.cpp
When I add the view of the currency project in my test case, I got an undefined reference of the constructor of the View. What am doing wrong?
#include <QtTest> #include <QCoreApplication> #include "view.h" // add necessary includes here class testConverter : public QObject { Q_OBJECT public: testConverter(); ~testConverter(); private slots: void initTestCase(); void cleanupTestCase(); void test_case1(); }; testConverter::testConverter() { } testConverter::~testConverter() { } void testConverter::initTestCase() { } void testConverter::cleanupTestCase() { } void testConverter::test_case1() { View view; // the problem occurs here } QTEST_MAIN(testConverter) #include "tst_testconverter.moc"
-
Hi
But does the view constructor takes no arguments ?
Normally it takes a parent. -
@mrjj
thanks for your reply, Here is the view:#include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class View; } QT_END_NAMESPACE class Controller; class View : public QMainWindow { Q_OBJECT public: View(QWidget *parent = nullptr); ~View(); void setController(Controller *controller); public slots: void set_amount(const QString& val); void get_recieve(); private: Ui::View *ui; Controller *m_controller; friend class testConverter; };
and its cpp
View::View(QWidget *parent) : QMainWindow(parent) , ui(new Ui::View) , m_controller(nullptr) { ui->setupUi(this); ui->amount_el->setText(QString::number(0.00)); connect(ui->amount_el, SIGNAL(textChanged(const QString &)), this, SLOT(set_amount(const QString &))); connect(ui->convert_btn, SIGNAL(clicked()), this, SLOT(get_recieve())); }
The parent by default is nullptr
-
Hi,
@Bonnie is right. Your current issue is that you don't build the class along your test hence your error.
You have several way of solving this:
- make a library out of the components of your application and link against it. You app project will mainly be a main.cpp file creating the needed objects using that library as well.
- stay as you are currently and build the files needed within the test sub project.
-
Hi @SGaist
I have tried the first bullet by refactoring the code having an app as main project that just call my lib:#include "mainwindow.h" #include "currencyconverter.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { CurrencyConverter *currency = new CurrencyConverter(parent); setCentralWidget(currency); }
and my main .pro file looks like this:
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++11 # The following define makes your compiler emit warnings if you use # any Qt feature that has been marked deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 INCLUDEPATH += \ ../CurrencyConverter LIBS += \ -L../CurrencyConverter SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
When compiling, I'm still getting undefined reference to CurrencyConverter at line where currency pointer is initiaze. the first bullet seems not working for me.
for the second bullet, what you do you mean by "build the files needed within the test sub project." ho do you do that in .pro file? -
What OS are you on ?