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. Getting strange errors using Unit tests
Forum Updated to NodeBB v4.3 + New Features

Getting strange errors using Unit tests

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 3 Posters 1.4k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi and welcome to devnet,

    One thing to exclude a Qt issue, can you successfully run the tests if the body of the methods are empty ?

    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
    • N Offline
      N Offline
      nickqu
      wrote on last edited by
      #3

      Hey SGaist,

      thank you for your help. I erased the body and I don't get any errors. All tests pass.

      ********* Start testing of Test *********
      Config: Using QtTest library 5.14.2, Qt 5.14.2 (x86_64-little_endian-llp64 shared (dynamic) release build; by GCC 7.3.0)
      PASS   : Test::initTestCase()
      PASS   : Test::testCompany()
      PASS   : Test::testAirline()
      PASS   : Test::testPrice()
      PASS   : Test::cleanupTestCase()
      Totals: 5 passed, 0 failed, 0 skipped, 0 blacklisted, 27ms
      ********* Finished testing of Test *********
      

      What am I doing wrong?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

        Well, it looks like you have a bug somewhere in the code you are testing.

        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
        1
        • N Offline
          N Offline
          nickqu
          wrote on last edited by nickqu
          #5

          I have a follow up question. I was able to fix the issue with the strange errors.

          I developed a UI in wich a user can choose a .txt file from the system and the data will be displayed in a table widget. It data is also being processed and put in vectors.

          void TravelAgency::on_actionDatei_einlesen_triggered(){
              QFile file(QFileDialog::getOpenFileName(this, tr("Open File"), "/home", tr("Text (*.txt)")));
              if (!file.exists())
                  return;
              file.open(QIODevice::ReadOnly | QIODevice::Text);
              if (!file.isOpen())
                  cerr << "error...";
          
              QTextStream stream(&file);
              QString line = stream.readLine();
              while (!line.isNull()) {
                  string sLine = line.toStdString();
          
                  fillVectors(sLine.at(0), sLine.substr(2));
          
                  line = stream.readLine();
              }
          
              cout << "vector A size: " << a.size() << endl; //return a non zero value but the getter function returns 0 in the unit test.
          
              Test test;
              QTest::qExec(&test);
          }
          

          And lets say I have a getter Method wich simply return the size of the vector which was filled and I have the following test:

          #include "test.h"
          
          Test::Test(QObject *parent) : QObject(parent){
          }
          
          void Test::testCompany(){
              TravelAgency* t = new TravelAgency();
              int sizeA= t->getSizeOfVectorA();
              QCOMPARE(sizeA,5);
          }
          

          The test fails because the getter function returns the value 0, but I am expecting a non zero value. Am I doing something wrong?

          jsulmJ 1 Reply Last reply
          0
          • N nickqu

            I have a follow up question. I was able to fix the issue with the strange errors.

            I developed a UI in wich a user can choose a .txt file from the system and the data will be displayed in a table widget. It data is also being processed and put in vectors.

            void TravelAgency::on_actionDatei_einlesen_triggered(){
                QFile file(QFileDialog::getOpenFileName(this, tr("Open File"), "/home", tr("Text (*.txt)")));
                if (!file.exists())
                    return;
                file.open(QIODevice::ReadOnly | QIODevice::Text);
                if (!file.isOpen())
                    cerr << "error...";
            
                QTextStream stream(&file);
                QString line = stream.readLine();
                while (!line.isNull()) {
                    string sLine = line.toStdString();
            
                    fillVectors(sLine.at(0), sLine.substr(2));
            
                    line = stream.readLine();
                }
            
                cout << "vector A size: " << a.size() << endl; //return a non zero value but the getter function returns 0 in the unit test.
            
                Test test;
                QTest::qExec(&test);
            }
            

            And lets say I have a getter Method wich simply return the size of the vector which was filled and I have the following test:

            #include "test.h"
            
            Test::Test(QObject *parent) : QObject(parent){
            }
            
            void Test::testCompany(){
                TravelAgency* t = new TravelAgency();
                int sizeA= t->getSizeOfVectorA();
                QCOMPARE(sizeA,5);
            }
            

            The test fails because the getter function returns the value 0, but I am expecting a non zero value. Am I doing something wrong?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @nickqu said in Getting strange errors using Unit tests:

            but I am expecting a non zero value

            Why?
            You did not fill the vector, right?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • N Offline
              N Offline
              nickqu
              wrote on last edited by
              #7

              The vector is filled correctly. It all works fine. While I was doing some debugging:

               cout << "vector A size: " << a.size() << endl;
              

              displayed the correct size value of the vector. Before I am doing

              Test test; 
              QTest::qExec(&test);
              

              But the getter simply returns 0 in the test. I don't know why...

              jsulmJ 1 Reply Last reply
              0
              • N nickqu

                The vector is filled correctly. It all works fine. While I was doing some debugging:

                 cout << "vector A size: " << a.size() << endl;
                

                displayed the correct size value of the vector. Before I am doing

                Test test; 
                QTest::qExec(&test);
                

                But the getter simply returns 0 in the test. I don't know why...

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by jsulm
                #8

                @nickqu Where do you fill the vector? You do not fill it in the constructor, right? And on_actionDatei_einlesen_triggered() is not called in your test.
                Can you show your getter?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  nickqu
                  wrote on last edited by
                  #9

                  No, I fill the vector in the fillVectors() function.

                  Is it necessary that I call on_actionDatei_einlesen_triggered() in my test? Because I am running the program and choosing a file from the .txt system. So I trigger the function which takes the .txt file processes it and fills the vector(s). And then I am exec. the test?

                  The getter function is simly:
                  ``
                  int TravelAgency::getSizeOfVectorA(){ return a.size(); }

                  jsulmJ 1 Reply Last reply
                  0
                  • N nickqu

                    No, I fill the vector in the fillVectors() function.

                    Is it necessary that I call on_actionDatei_einlesen_triggered() in my test? Because I am running the program and choosing a file from the .txt system. So I trigger the function which takes the .txt file processes it and fills the vector(s). And then I am exec. the test?

                    The getter function is simly:
                    ``
                    int TravelAgency::getSizeOfVectorA(){ return a.size(); }

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by jsulm
                    #10

                    @nickqu said in Getting strange errors using Unit tests:

                    Is it necessary that I call on_actionDatei_einlesen_triggered()

                    It is necessary that the vector gets filled somehow and in the test code you posted I don't see anything like that.
                    Where do you fill the vector in this code:

                    void Test::testCompany(){
                        TravelAgency* t = new TravelAgency();
                        int sizeA= t->getSizeOfVectorA();
                        QCOMPARE(sizeA,5);
                    }
                    

                    ?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      nickqu
                      wrote on last edited by
                      #11

                      The vectors don't get filled in the test. They get filled in te while loop in on_actionDatei_einlesen_triggered() function with the fillVectors() function.

                      The way that I was expecting it was this: I run the programm. The Programm Window opens. I choose a file. The while loop fills the vector(s) with the fillVectors() method.
                      Until now it works, because

                      cout << "vector A size: " << a.size() << endl;
                      

                      right after the while loop, displays the correct vector size.

                      Now I execute the test

                      Test test; 
                      QTest::qExec(&test);
                      

                      And the getter function returns the correct vector size in the test.

                      jsulmJ 1 Reply Last reply
                      0
                      • N nickqu

                        The vectors don't get filled in the test. They get filled in te while loop in on_actionDatei_einlesen_triggered() function with the fillVectors() function.

                        The way that I was expecting it was this: I run the programm. The Programm Window opens. I choose a file. The while loop fills the vector(s) with the fillVectors() method.
                        Until now it works, because

                        cout << "vector A size: " << a.size() << endl;
                        

                        right after the while loop, displays the correct vector size.

                        Now I execute the test

                        Test test; 
                        QTest::qExec(&test);
                        

                        And the getter function returns the correct vector size in the test.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @nickqu said in Getting strange errors using Unit tests:

                        The way that I was expecting it was this: I run the programm. The Programm Window opens. I choose a file. The while loop fills the vector(s) with the fillVectors()

                        This is not how unit tests work.
                        There should not be any interaction with users.
                        Why don't you simply call on_actionDatei_einlesen_triggered() in the test before checking the vector size?

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1
                        • N Offline
                          N Offline
                          nickqu
                          wrote on last edited by nickqu
                          #13

                          @jsulm thanks, it works now. But how can I make it, so that in the test on_actionDatei_einlesen_triggered() ist executed, when the (menu) button was clicked? Right now I get asked to choose a file as soon as the programm starts and not when the button is clicked...

                          jsulmJ 1 Reply Last reply
                          0
                          • N nickqu

                            @jsulm thanks, it works now. But how can I make it, so that in the test on_actionDatei_einlesen_triggered() ist executed, when the (menu) button was clicked? Right now I get asked to choose a file as soon as the programm starts and not when the button is clicked...

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            @nickqu Take a look at https://doc.qt.io/qt-5/qtest.html
                            There are several mousePress overloads.

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • N Offline
                              N Offline
                              nickqu
                              wrote on last edited by
                              #15

                              @jsulm can you make an example? I don't understand how the mousePress overloads are goingt to help me with my case.

                              jsulmJ 1 Reply Last reply
                              0
                              • N nickqu

                                @jsulm can you make an example? I don't understand how the mousePress overloads are goingt to help me with my case.

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                @nickqu Take a look at https://doc.qt.io/qt-5/qttestlib-tutorial3-example.html

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                1

                                • Login

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