Delete objects created with unit testing



  • 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_OBJECT

    public:
    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();
    }
    @


  • Lifetime Qt Champion

    Hi,

    Just delete the widget after the QCOMPARE call.

    The _data method only create test data, they do not handle their lifetime



  • 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.


  • Lifetime Qt Champion

    I didn't understood your application test question



  • main does not end after the tests, I'm being forced to abort the main loop manually.


  • Lifetime Qt Champion

    Ok I see. Why aren't you using Qt's standard test application ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.