Solved Getting strange errors using Unit tests
-
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?
-
Well, it looks like you have a bug somewhere in the code you are testing.
-
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?
-
@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? -
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...
-
@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? -
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(); } -
@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); }
?
-
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, becausecout << "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.
-
@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? -
@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...
-
@nickqu Take a look at https://doc.qt.io/qt-5/qtest.html
There are several mousePress overloads. -
@jsulm can you make an example? I don't understand how the mousePress overloads are goingt to help me with my case.
-
@nickqu Take a look at https://doc.qt.io/qt-5/qttestlib-tutorial3-example.html