QTimer timout不会在单元测试中触发

时间:2018-09-21 10:59:08

标签: c++ qt unit-testing qt5 qtimer

我正在尝试为我创建的名为Counter的类创建单元测试。这样的类应每隔0.1秒减少其值“计数”,因此我在类构造函数中连接了该方法。一切似乎在原始程序中都可以正常工作,但是当我尝试运行单元测试器时,似乎没有发生连接或QTimer根本没有发送timout信号。

gitHub链接: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);
}

编辑:K,我将被要求在此处插入其他代码部分。

在前面的代码中,这是我要测试的类的构造函数,decCount()所做的仅此而已:

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

以上信号与单元测试器代码无关,它们已在完整程序中使用。

现在,据我了解,在tst_testCounter.cpp中,它是经典main的测试单元版本,我得到

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;
        }
    }
}

.cpp显然以

结尾
QTEST_MAIN(testCounter)

#include "tst_testcounter.moc"

1 个答案:

答案 0 :(得分:3)

您应该记住的第一件事是QTimer不正确,因此以秒为单位进行检查会导致问题,更好的测试可以验证以毫秒为单位的时差小于特定时间值。

另一方面,说明:

while (counter-> count> 0) {
    std::cout << counter-> count << std::endl;
}

您不让事件循环起作用,因此QTimer或信号可以起作用。

您应该使用QCoreApplication::processEvents()

while(counter.count>0){
    QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
}

还建议在该方法中定义测试的元素,并且该测试不在构造函数中而是在插槽中给出。

考虑到上述情况,并进行了其他改进,可获得以下内容:

counter.h

#ifndef COUNTER_H
#define COUNTER_H

#include <QObject>
class QTimer;
class QLineEdit;

class Counter : public QObject
{
    Q_OBJECT
public:
    explicit Counter(QLineEdit *setterSec, QLineEdit *setterMin, QObject *parent = nullptr);
    QTimer *timer;
    QLineEdit *setterSec;
    QLineEdit *setterMin;
    int count;
signals:
    void showCountSec(const QString &count);
    void showCountMin(int count);

public slots:
    void decCount();
    void start();
    void stop();
    void set();
};

#endif // COUNTER_H

counter.cpp

#include "counter.h"
#include <QTimer>
#include <QIntValidator>
#include <QLineEdit>

Counter::Counter(QLineEdit *editSec, QLineEdit *editMin, QObject *parent) :
    QObject(parent),
    timer(new QTimer(this)),
    setterSec(editSec),
    setterMin(editMin),
    count(0)
{
    setterSec->setValidator(new QIntValidator(0, 59, this));
    setterMin->setValidator(new QIntValidator(0, 59, this));
    connect(timer, &QTimer::timeout, this, &Counter::decCount);
}

void Counter::start(){
    timer->start(100);
}

void Counter::stop(){
    timer->stop();
}

void Counter::set(){
    timer->stop();
    count = setterSec->text().toInt()*10 + setterMin->text().toInt()*600;
    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);
}

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

tst_counter.h

#include <QtTest>
#include "counter.h"
#include <QLineEdit>
#include <iostream>
#include <cmath>
#include <QDebug>

// add necessary includes here

class tst_Counter : public QObject
{
    Q_OBJECT
public:
    tst_Counter();
    ~tst_Counter();

private slots:
    void initTestCD();
};

tst_Counter::tst_Counter() : QObject ()
{
}
tst_Counter::~tst_Counter()
{

}

void tst_Counter::initTestCD(){
    QLineEdit editSec;
    QLineEdit editMin;
    Counter counter(&editSec, &editMin);
    const int epsilon = 50; // ms

    for(int 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
        qDebug() << "Starting the Timer at " << (counter.count)/10;

        auto start= std::chrono::system_clock::now();
        counter.start(); //{timer->start();}
        while(counter.count>0){
            QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
        }
        auto end= std::chrono::system_clock::now();
        counter.stop();
        auto check_ms= std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
        int checkInt = check_ms.count();
        int delta_s = randsec + randmin*60;
        int delta = std::abs(checkInt - delta_s);
        QVERIFY(delta < epsilon);

        qDebug() << "Sec counted: " << counter.count/10;
        qDebug() << "msec passed: " << checkInt;
    }
}

QTEST_MAIN(tst_Counter)

#include "tst_counter.moc"

输出:

********* Start testing of tst_Counter *********
Config: Using QtTest library 5.11.2, Qt 5.11.2 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 8.2.1 20180831)
PASS   : tst_Counter::initTestCase()
QDEBUG : tst_Counter::initTestCD() Starting the Timer at  3
QDEBUG : tst_Counter::initTestCD() Sec counted:  0
QDEBUG : tst_Counter::initTestCD() msec passed:  3033
QDEBUG : tst_Counter::initTestCD() Starting the Timer at  3
QDEBUG : tst_Counter::initTestCD() Sec counted:  0
QDEBUG : tst_Counter::initTestCD() msec passed:  2999
QDEBUG : tst_Counter::initTestCD() Starting the Timer at  3
QDEBUG : tst_Counter::initTestCD() Sec counted:  0
QDEBUG : tst_Counter::initTestCD() msec passed:  3000
QDEBUG : tst_Counter::initTestCD() Starting the Timer at  3
QDEBUG : tst_Counter::initTestCD() Sec counted:  0
QDEBUG : tst_Counter::initTestCD() msec passed:  2999
QDEBUG : tst_Counter::initTestCD() Starting the Timer at  3
QDEBUG : tst_Counter::initTestCD() Sec counted:  0
QDEBUG : tst_Counter::initTestCD() msec passed:  2999
QDEBUG : tst_Counter::initTestCD() Starting the Timer at  3
QDEBUG : tst_Counter::initTestCD() Sec counted:  0
QDEBUG : tst_Counter::initTestCD() msec passed:  3000
QDEBUG : tst_Counter::initTestCD() Starting the Timer at  3
QDEBUG : tst_Counter::initTestCD() Sec counted:  0
QDEBUG : tst_Counter::initTestCD() msec passed:  2999
QDEBUG : tst_Counter::initTestCD() Starting the Timer at  3
QDEBUG : tst_Counter::initTestCD() Sec counted:  0
QDEBUG : tst_Counter::initTestCD() msec passed:  2999
QDEBUG : tst_Counter::initTestCD() Starting the Timer at  3
QDEBUG : tst_Counter::initTestCD() Sec counted:  0
QDEBUG : tst_Counter::initTestCD() msec passed:  2999
QDEBUG : tst_Counter::initTestCD() Starting the Timer at  3
QDEBUG : tst_Counter::initTestCD() Sec counted:  0
QDEBUG : tst_Counter::initTestCD() msec passed:  2999
QDEBUG : tst_Counter::initTestCD() Starting the Timer at  3
QDEBUG : tst_Counter::initTestCD() Sec counted:  0
QDEBUG : tst_Counter::initTestCD() msec passed:  2999
PASS   : tst_Counter::initTestCD()
PASS   : tst_Counter::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted, 33078ms
********* Finished testing of tst_Counter *********

在下面的link中,您可以找到完整的示例。