Solved how to test a function calculator( QWidget *parent)
-
This is the code:
Button.h:class Button : public QToolButton { Q_OBJECT public: explicit Button(const QString &text, QWidget *parent = 0);
calculator.h:
class Calculator : public QWidget { Q_OBJECT public: Calculator(QWidget *parent = 0);
calculator.cpp:
Calculator::Calculator(QWidget *parent) : QWidget(parent) {}
Button.cpp:
Button::Button(const QString &text, QWidget *parent) : QToolButton(parent)
Main.cpp:
int main(int argc, char *argv[]) { QApplication app(argc, argv); Calculator calc; calc.show(); return app.exec(); }
How can I test this calculator function
-
@shravan_121 Did you add the calculator.cpp to your pro file?
-
Test.pro:
QT += testlib
QT += gui
QT +=widgetsCONFIG += qt console warn_on depend_includepath testcase
CONFIG -= app_bundleTEMPLATE = app
INCLUDEPATH = C:/Qt/Qt5.12.6/Examples/Qt-5.12.6/widgets/widgets/calculator
DEPENDPATH = C:/Qt/Qt5.12.6/Examples/Qt-5.12.6/widgets/widgets/calculatorSOURCES += tst_test1.cpp \
calculator.pro:
QT += widgetsHEADERS = button.h
calculator.h
SOURCES = button.cpp
calculator.cpp
main.cppinstall
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/widgets/calculator
INSTALLS += target -
@shravan_121 What are your includes for those classes ?
-
button.cpp:
#include <QtWidgets>
#include "button.h"calculator.cpp:
#include <QtWidgets>
#include <cmath>
#include "button.h"
#include "calculator.h"main.cpp:
#include <QApplication>
#include "calculator.h" -
Test.cpp:
#include<QTest>
#include<calculator.h>
#include<button.h>
#include<QtWidgets>
#include<QDebug>class test1 : public QObject
{
Q_OBJECTpublic:
test1();
~test1();
private slots:
void test_case1();
void test_case2();
private:
Calculator *mcalculator;};
-
-
After including calculator.cpp i am getting error:QWidget: Must construct a Qapplication before a QWidget. And Thank You all of you for helping me.
-
@shravan_121
So you need to obey the message, exactly as it says. You need to construct yourQApplication
before you can accessQWidget
s. What else to say? -
@JonB Actually I don't know how this works and as i am new to qt I don't know how to construct QApplication
-
@shravan_121
Follow any example anywhere of how to create a basic Qt (widgets) application. Reading the docs/examples is a good place to start. Without knowing that you won't get anywhere. -
I have the application created but for testing that application I am facing this error.In that actual application Application is constructed in the main function.
-
Please read the QtTest module tutorials they show how to write unit tests.
-
I have gone through the document about unit testing.Now I have some idea on inbuilt qttest functions.But I need to clear the error of QApplication construction in QtTest application.Can Any one help me solving this.
-
Did you add the QTEST_MAIN related lines as described at the bottom of the GUI related tutorials ?
-
@SGaist I tried It but it shows some error I don't know how can you give me a sample of it
how to add QApplication related lines because i am unclear about the concept. -
@shravan_121 said in how to test a function calculator( QWidget *parent):
but it shows some error
Please post the error
-
@jsulm
Test.Cpp:
#include <QtTest/QtTest>
#include<QTest>
#include<calculator.h>
#include<button.h>
#include<QtWidgets>
#include<QDebug>
#include<QApplication>
class test1 : public QObject
{
Q_OBJECTpublic:
test1();
~test1();private slots:
void test_case1();
void test_case2();private:
Calculator *mcalculator;
};
test1::test1()
{
mcalculator = new Calculator(); //ERROR
}
-
#include <QtTest/QtTest>
#include<QTest>
#include<calculator.h>
#include<button.h>
#include<QtWidgets>
#include<QDebug>
#include<QApplication>
class test1 : public QObject
{
Q_OBJECTpublic:
test1();
~test1();private slots:
void test_case1();
void test_case2();private:
Calculator *mcalculator;
};
test1::test1()
{
mcalculator = new Calculator();//error
}
-
QTEST_APPLESS_MAIN(test1)
#include "tst_test1.moc"