qt5 demo: add example of unit testing using QtTest integrated into waf with standard waf_unit_test Tool

This commit is contained in:
fedepell 2019-06-05 14:59:26 +02:00
parent 36898e12af
commit e6eca1eec5
5 changed files with 82 additions and 3 deletions

View File

@ -3,7 +3,24 @@
#include "foo.h"
Foo::Foo() : QWidget(NULL) {
m_button = new QPushButton("Foo Button", this);
m_button->setGeometry(QRect(QPoint(50, 60),
QSize(120, 50)));
connect(m_button, SIGNAL (released()), this, SLOT (handleButton()));
myToggle = true;
}
void Foo::handleButton() {
if (myToggle) {
m_button->setText("Button Foo");
} else {
m_button->setText("Foo Button");
}
myToggle = !myToggle;
}
int Foo::FortyTwo() {
return 42;
}
class Bar_private : public QWidget {

View File

@ -4,13 +4,20 @@
#define _FOO
#include <QWidget>
#include <QPushButton>
class Foo : public QWidget {
Q_OBJECT
signals:
void test();
private slots:
void handleButton();
public:
Foo();
int FortyTwo();
QPushButton *m_button;
public:
bool myToggle;
};
#endif

View File

@ -4,12 +4,13 @@
//#include <QString>
//#include "mainwindow.h"
#include "ui_but.h"
#include "foo.h"
int main(int argc, char **argv)
{
Q_INIT_RESOURCE(res);
QApplication app(argc, argv);
QWidget window;
Foo window;
Ui::Form ui;
ui.setupUi(&window);
window.show();

31
demos/qt5/testqt5.cpp Normal file
View File

@ -0,0 +1,31 @@
// Example of Qt5 Unit test with QtTest library
// Federico Pellegrin, 2019 (fedepell)
#include "foo.h"
#include <QtTest/QtTest>
class TestQt5Test: public QObject {
Q_OBJECT
private:
Foo myFoo;
private slots:
void testGui();
void testFunc();
};
// Test of the UI by simulating a button click and button label reading
void TestQt5Test::testGui() {
QCOMPARE(myFoo.m_button->text(), QString("Foo Button"));
QTest::mouseClick(myFoo.m_button,Qt::LeftButton,Qt::NoModifier,QPoint(5,5),0);
QCOMPARE(myFoo.m_button->text(), QString("Button Foo"));
}
// Test of a normal function
void TestQt5Test::testFunc() {
QCOMPARE(myFoo.FortyTwo(), 44); // this fails! 42 != 44
}
QTEST_MAIN(TestQt5Test)
#include "testqt5.moc"

View File

@ -1,6 +1,7 @@
#! /usr/bin/env python
# encoding: utf-8
# Thomas Nagy, 2016 (ita)
# Federico Pellegrin, 2019 (fedepell)
VERSION='0.0.1'
APPNAME='qt5_test'
@ -9,10 +10,10 @@ top = '.'
out = 'build'
def options(opt):
opt.load('compiler_cxx qt5')
opt.load('compiler_cxx qt5 waf_unit_test')
def configure(conf):
conf.load('compiler_cxx qt5')
conf.load('compiler_cxx qt5 waf_unit_test')
#conf.env.append_value('CXXFLAGS', ['-g']) # test
def build(bld):
@ -32,3 +33,25 @@ def build(bld):
langname = 'somefile', # include the .qm files from somefile.qrc
)
# Example of integration of Qt5 Unit tests using Qt5Test using waf_unit_test
bld(
features = 'qt5 cxx cxxprogram test',
use = 'QT5CORE QT5WIDGETS QT5TEST',
source = 'foo.cpp testqt5.cpp',
moc = 'foo.h',
target = 'footest',
includes = '.',
# ut_str = './${SRC} -o test-report.xml,xunitxml', # put output to a xunit xml
)
bld.add_post_fun(print_test_results) # print output of test runner to user
def print_test_results(bld):
lst = getattr(bld, 'utest_results', [])
if not lst:
return
for (f, code, out, err) in lst:
print(out.decode('utf-8'))
print(err.decode('utf-8'))