I'm trying to create a unit testing for a class i made named Counter. Such class should decrease its value "count" every 0.1 second, so I connected the method in the class constructor. Everything seems to work just fine in the original program, but when I try to run the unit tester it seems as the connection didnt happen or the QTimer doesnt send the timout signal at all.
gitHub link: https://github.com/allocro/TimerLFdP/tree/master/testCounter
Counter::Counter(QLineEdit *editSec, QLineEdit *editMin, QObject *parent) :
QObject(parent)
{
timer= new QTimer(this);
[...]
connect(timer, &QTimer::timeout, this, &Counter::decCount);
}
EDIT: K, I'm gonna try to fit other code part in here as I'm being asked to do so.
In the precedend code, here is the constructor of the class I want to test, what decCount() doesis nothing else than that:
void Counter::decCount(){
if (count>0)
count--;
else
timer->stop();
int countsec = count % 600;
int countmin = (count - countsec)/600;
QString secOut = QString::number(countsec/10) + "."
+QString::number(countsec%10);
emit showCountMin(countmin);
emit showCountSec(secOut);
}
The above signals are irrelevant to the unit tester code, they were used in the complete program.
Now, in the tst_testCounter.cpp, that as far as I understood, is the test unit version of the classic main, and I got
testCounter::testCounter() : QObject ()
{ int i;
editSec= new QLineEdit();
editMin= new QLineEdit();
counter= new Counter(editSec, editMin);
for(i=0; i<=10; i++){
int randsec= rand() %60;
int randmin= rand() %60;
randsec=3;
randmin=0; //To test the tester faster
QString randsecString= QString::number(randsec);
QString randminString= QString::number(randmin);
editSec->setText(randsecString);
editMin->setText(randminString);
counter->set();//those are to input, they work fine, I checked
std::cout << "Starting the Timer at " << (counter->count)/10 <<
"sec" <<std::endl;
counter->start(); //{timer->start();}
while(counter->count>0){
std::cout <<counter->count <<std::endl;
}
auto end= std::chrono::system_clock::now();
counter->stop();
auto check= std::chrono::duration_cast<std::chrono::seconds>(end -
start);
int checkInt= check.count();
if(checkInt==randsec+(randmin*60)){
std::cout << "Timer matches" <<std::endl;
}else{
std::cout << "Timer doesnt match" <<std::endl;
std::cout << "Sec counted: " << (counter->count)/10 <<std::endl;
std::cout << "Sec passed: " << checkInt <<std::endl;
break;
}
}
}
And obviously the .cpp ends in
QTEST_MAIN(testCounter)
#include "tst_testcounter.moc"