Delete objects created with unit testing
-
wrote on 21 Jun 2014, 19:11 last edited by
I put this as a continuation on an earlier topic, but I believe fled the title. So I decided to open another topic for this.
The code below is for the unit tests for a class that returns objects created with QUiLoader.
Each test case:
@
QTest::newRow("Valid File") << "test.ui" << true; // Valid File
QTest::newRow("Inexistent File") << "inexistent.ui" << true; // Inexistent File
QTest::newRow("Invalid File") << "test_text.ui" << true; // Invalid File
QTest::newRow("Empty Ui File") << "testempty.ui" << true; // Empty File
@A new object is created but not deleted. I need to find a way to delete every object created after each test line. example:
@ QTest::newRow("Valid File") << "test.ui" << true; // Valid File
delete;
QTest::newRow("Inexistent File") << "inexistent.ui" << true; // Inexistent File
delete;
QTest::newRow("Invalid File") << "test_text.ui" << true; // Invalid File
delete;
QTest::newRow("Empty Ui File") << "testempty.ui" << true; // Empty File
delete;@Full code:
@/*
- dynamicwidgetstest.hpp
- This file is part of dynamicwidgets.so
- Copyright (C) 2014 - Matheus Saraiva
- dynamicwidgets.so is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- dynamicwidgets.so is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with dynamicwidgets.so. If not, see http://www.gnu.org/licenses/.
*/
#ifndef DYNAMICWIDGETSTEST_H
define DYNAMICWIDGETSTEST_H
#include <QString>
#include <QtTest>
#include "dynamicwidgets.hpp"class DynamicWidgetsTest : public QObject
{
Q_OBJECTpublic:
explicit DynamicWidgetsTest();
~DynamicWidgetsTest();private Q_SLOTS:
void testCreateWidget();
void testCreateWidget_data();
};
#endif
@@/*
- dynamicwidgetstest.cpp
- This file is part of dynamicwidgets.so
- Copyright (C) 2014 - Matheus Saraiva
- dynamicwidgets.so is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- dynamicwidgets.so is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with dynamicwidgets.so. If not, see http://www.gnu.org/licenses/.
*/
#include "dynamicwidgetstest.hpp"
DynamicWidgetsTest::DynamicWidgetsTest()
{
}DynamicWidgetsTest::~DynamicWidgetsTest()
{
}void DynamicWidgetsTest::testCreateWidget()
{
QFETCH(QString, file_ui);
QFETCH(bool, widget);DynamicWidgets dynawidg(file_ui); // DynamicWidgets object
QWidget *widtest = dynawidg.createWidget();
QCOMPARE(widtest->isWidgetType(), widget); // Compare DynamicWidgets::createWidget() return with QWidget type
}void DynamicWidgetsTest::testCreateWidget_data()
{
QTest::addColumn<QString>("file_ui");
QTest::addColumn<bool>("widget");QTest::newRow("Valid File") << "test.ui" << true; // Valid File
QTest::newRow("Inexistent File") << "inexistent.ui" << true; // Inexistent File
QTest::newRow("Invalid File") << "test_text.ui" << true; // Invalid File
QTest::newRow("Empty Ui File") << "testempty.ui" << true; // Empty File
}
@@#include "dynamicwidgetstest.hpp"
#include <QtWidgets/QApplication>int main(int argc, char *argv[])
{
QApplication app(argc, argv);DynamicWidgetsTest tests;
QTest::qExec(&tests, argc, argv);return app.exec();
}
@ -
Hi,
Just delete the widget after the QCOMPARE call.
The _data method only create test data, they do not handle their lifetime
-
wrote on 23 Jun 2014, 15:41 last edited by
It worked, but I'm puzzled because I had already tried before, but anyway ... Taking the hook of the topic, how do I the test application end up alone after the tests are completed? I am currently needing to manually abort the application.
-
I didn't understood your application test question
-
wrote on 23 Jun 2014, 17:53 last edited by
main does not end after the tests, I'm being forced to abort the main loop manually.
-
Ok I see. Why aren't you using Qt's standard test application ?
1/6