Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Delete objects created with unit testing
Forum Updated to NodeBB v4.3 + New Features

Delete objects created with unit testing

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Exotic_Devel
    wrote on 21 Jun 2014, 19:11 last edited by
    #1

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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 21 Jun 2014, 20:33 last edited by
      #2

      Hi,

      Just delete the widget after the QCOMPARE call.

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Exotic_Devel
        wrote on 23 Jun 2014, 15:41 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 23 Jun 2014, 17:02 last edited by
          #4

          I didn't understood your application test question

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • E Offline
            E Offline
            Exotic_Devel
            wrote on 23 Jun 2014, 17:53 last edited by
            #5

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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 23 Jun 2014, 21:18 last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              5/6

              23 Jun 2014, 17:53

              • Login

              • Login or register to search.
              5 out of 6
              • First post
                5/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved