QTest GUI getting started
-
wrote on 6 Nov 2017, 11:13 last edited by
I am trying to figure out how to use QTest to test my gui application, so I set up a simple program like the one in this posting Qt test empty window. (Using Qt 5.7 on Ubuntu)
When I try to run it, I get the message tst_mytest.cpp:33: undefined reference to `MyMainWindow::MyMainWindow(QWidget)'*.
This should be really simple but I can't figure out what it is.
Thanks,
Jas//tst_mytest.cpp #include <QString> #include <QtTest> #include <QCoreApplication> #include "../MyApp/mymainwindow.h" #include "../MyApp/ui_mymainwindow.h" #include <QDebug> #include <QPushButton> class MyTest : public QObject { Q_OBJECT public: MyTest(); private Q_SLOTS: void initTestCase(); void cleanupTestCase(); void testCase1(); private: MyMainWindow* MyMainWindow_ ; }; MyTest::MyTest() { } void MyTest::initTestCase() { MyMainWindow_ = new MyMainWindow; MyMainWindow_->show(); } void MyTest::cleanupTestCase() { } void MyTest::testCase1() { QVERIFY2(true, "Failure"); } QTEST_MAIN(MyTest) #include "tst_mytest.moc"
//MyTest.pro #------------------------------------------------- # # Project created by QtCreator 2017-11-06T10:12:26 # #------------------------------------------------- QT += widgets testlib core TARGET = tst_mytest CONFIG += console CONFIG -= app_bundle TEMPLATE = app SOURCES += tst_mytest.cpp DEFINES += SRCDIR=\\\"$$PWD/\\\" INCLUDEPATH += $$PWD/../MyApp
//mymainwindow.cpp #include "mymainwindow.h" #include "ui_mymainwindow.h" MyMainWindow::MyMainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MyMainWindow) { ui->setupUi(this); } MyMainWindow::~MyMainWindow() { delete ui; } void MyMainWindow::on_pushButton_clicked() { if (ui->checkBox->isChecked()) ui->checkBox->setChecked(false); else ui->checkBox->setChecked(true); }
//mymainwindow.h #ifndef MYMAINWINDOW_H #define MYMAINWINDOW_H #include <QMainWindow> namespace Ui { class MyMainWindow; } class MyMainWindow : public QMainWindow { Q_OBJECT public: explicit MyMainWindow(QWidget *parent = 0); ~MyMainWindow(); private slots: void on_pushButton_clicked(); private: Ui::MyMainWindow *ui; }; #endif // MYMAINWINDOW_H
//MyApp.pro #ifndef MYMAINWINDOW_H #define MYMAINWINDOW_H #include <QMainWindow> namespace Ui { class MyMainWindow; } class MyMainWindow : public QMainWindow { Q_OBJECT public: explicit MyMainWindow(QWidget *parent = 0); ~MyMainWindow(); private slots: void on_pushButton_clicked(); private: Ui::MyMainWindow *ui; }; #endif // MYMAINWINDOW_H
//MySolution.pro TEMPLATE = subdirs SUBDIRS += MyApp \ MyTest
-
I am trying to figure out how to use QTest to test my gui application, so I set up a simple program like the one in this posting Qt test empty window. (Using Qt 5.7 on Ubuntu)
When I try to run it, I get the message tst_mytest.cpp:33: undefined reference to `MyMainWindow::MyMainWindow(QWidget)'*.
This should be really simple but I can't figure out what it is.
Thanks,
Jas//tst_mytest.cpp #include <QString> #include <QtTest> #include <QCoreApplication> #include "../MyApp/mymainwindow.h" #include "../MyApp/ui_mymainwindow.h" #include <QDebug> #include <QPushButton> class MyTest : public QObject { Q_OBJECT public: MyTest(); private Q_SLOTS: void initTestCase(); void cleanupTestCase(); void testCase1(); private: MyMainWindow* MyMainWindow_ ; }; MyTest::MyTest() { } void MyTest::initTestCase() { MyMainWindow_ = new MyMainWindow; MyMainWindow_->show(); } void MyTest::cleanupTestCase() { } void MyTest::testCase1() { QVERIFY2(true, "Failure"); } QTEST_MAIN(MyTest) #include "tst_mytest.moc"
//MyTest.pro #------------------------------------------------- # # Project created by QtCreator 2017-11-06T10:12:26 # #------------------------------------------------- QT += widgets testlib core TARGET = tst_mytest CONFIG += console CONFIG -= app_bundle TEMPLATE = app SOURCES += tst_mytest.cpp DEFINES += SRCDIR=\\\"$$PWD/\\\" INCLUDEPATH += $$PWD/../MyApp
//mymainwindow.cpp #include "mymainwindow.h" #include "ui_mymainwindow.h" MyMainWindow::MyMainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MyMainWindow) { ui->setupUi(this); } MyMainWindow::~MyMainWindow() { delete ui; } void MyMainWindow::on_pushButton_clicked() { if (ui->checkBox->isChecked()) ui->checkBox->setChecked(false); else ui->checkBox->setChecked(true); }
//mymainwindow.h #ifndef MYMAINWINDOW_H #define MYMAINWINDOW_H #include <QMainWindow> namespace Ui { class MyMainWindow; } class MyMainWindow : public QMainWindow { Q_OBJECT public: explicit MyMainWindow(QWidget *parent = 0); ~MyMainWindow(); private slots: void on_pushButton_clicked(); private: Ui::MyMainWindow *ui; }; #endif // MYMAINWINDOW_H
//MyApp.pro #ifndef MYMAINWINDOW_H #define MYMAINWINDOW_H #include <QMainWindow> namespace Ui { class MyMainWindow; } class MyMainWindow : public QMainWindow { Q_OBJECT public: explicit MyMainWindow(QWidget *parent = 0); ~MyMainWindow(); private slots: void on_pushButton_clicked(); private: Ui::MyMainWindow *ui; }; #endif // MYMAINWINDOW_H
//MySolution.pro TEMPLATE = subdirs SUBDIRS += MyApp \ MyTest
wrote on 6 Nov 2017, 11:22 last edited by@jastmc said in QTest GUI getting started:
MyMainWindow_ = new MyMainWindow;
I should have said the error occurs on the line on the line
MyMainWindow_ = new MyMainWindow;
-
@jastmc said in QTest GUI getting started:
MyMainWindow_ = new MyMainWindow;
I should have said the error occurs on the line on the line
MyMainWindow_ = new MyMainWindow;
wrote on 6 Nov 2017, 11:50 last edited by JonB 11 Jun 2017, 15:56[EDIT: please ignore this post, it's irrelevant and wrong. Sorry!]
@jastmc
Since the example you quote uses:mainWindow_ = new MainWindow();
why do you write
MyMainWindow_ = new MyMainWindow;
and then ask why it doesn't behave same?
-
@jastmc said in QTest GUI getting started:
MyMainWindow_ = new MyMainWindow;
undefined reference to `MyMainWindow::MyMainWindow(QWidget)'*.
It says it dont know your constructor.
I would completely delete the build folder, and run qmake + rebuild all.
If error persists, please check that MyMainWindow.cpp is actual included into the .pro file
so it gets linked to the tst_mytest project. -
Hi,
In your test, you only build the test sources itself. What about the class you want to test ?
-
[EDIT: please ignore this post, it's irrelevant and wrong. Sorry!]
@jastmc
Since the example you quote uses:mainWindow_ = new MainWindow();
why do you write
MyMainWindow_ = new MyMainWindow;
and then ask why it doesn't behave same?
wrote on 6 Nov 2017, 12:31 last edited by@JNBarchan said in QTest GUI getting started:
@jastmc
Since the example you quote uses:mainWindow_ = new MainWindow();
why do you write
MyMainWindow_ = new MyMainWindow;
and then ask why it doesn't behave same?
Whoops? For my own edification, does stinky C++ allow you to omit the
()
innew Something
and call a default constructor? parameterless constructor? complain if multiple constructors? or what?? -
@jastmc Add it to SOURCES and HEADERS in your pro file.
-
@JNBarchan said in QTest GUI getting started:
@jastmc
Since the example you quote uses:mainWindow_ = new MainWindow();
why do you write
MyMainWindow_ = new MyMainWindow;
and then ask why it doesn't behave same?
Whoops? For my own edification, does stinky C++ allow you to omit the
()
innew Something
and call a default constructor? parameterless constructor? complain if multiple constructors? or what??@JNBarchan
yes the new operator/compiler can construct an object if it can find
a default constructor (ctor). The ctor is a special function and and that is why () can be omitted even one often consider it just a function call. -
wrote on 6 Nov 2017, 15:49 last edited by
@jsulm Thanks, that was part of it.
I then had to add
INCLUDEPATH += $$PWD/../MyApp/ \ ../../build-MySolution-Desktop_Qt_5_7_0_GCC_64bit-Debug/MyApp/ \
to the MyTest.pro file because the next message was that it could not find the ui_mymainwindow.h file. So that solved it.
Regards,
Jas -
You are missing the
FORMS
variable in your .pro file.
1/11