(Qt6 - Advanced) 8. Qt Test

Posted by : at

Category : Qt


#include <QCoreApplication>
#include <QTest>
#include "cat.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Cat cat;
#ifdef QT_DEBUG
    QTest::qExec(&cat);
#endif
    return a.exec();
}
#ifndef CAT_H
#define CAT_H

#include <QObject>
#include <QDebug>


class Cat : public QObject
{
    Q_OBJECT
public:
    explicit Cat(QObject *parent = nullptr);
    void public_test(); //not called

signals:

public slots:

    void test(); //not called


private slots:
    void play();
    void meow();
    void sleep();
    void speak(QString value); //no param

private:
    void private_test(); //not called
};

#endif // CAT_H

WARN, ERROR

Widget::Widget(QObject *parent) : QObject(parent)
{
    age = 0;
}

void Widget::setAge(int value)
{
    age = value;
}

void Widget::testFail()
{
    QFAIL("NO REASON JUST FAIL!!!");
}

void Widget::testAge()
{
    if(!age) QFAIL("Age is not set!");
}

void Widget::testWidget()
{

    int value = 45;

    //Make sure the age is valid--------------
    QVERIFY(age > 0 && age < 100);

    //Issue warnings
    if(age > 40) QWARN("Age is over 40!");
    if(age < 21) QFAIL("Must be an adult!");

    //Make sure they are the same---------------------
    QCOMPARE(age, value);


}
void Widget::testAge_data()
{
    qInfo() << "Generating data...";

    QTest::addColumn<QString>("name");
    QTest::addColumn<int>("age");

    QTest::addRow("Invalid") << "Bob" << 190;
    QTest::addRow("Old") << "Bryan" << 44;
    QTest::addRow("Young") << "Heather" << 25;
    QTest::addRow("Under age") << "Rango" << 14;
    QTest::addRow("Retired") << "Grandma" << 90;

    qInfo() << "Data generated!";
}

void Widget::testAge()
{
    QFETCH(QString,name);
    QFETCH(int, age);

    qInfo() << "Testing age " << name << " is " << age;

    if(age < 1 || age > 100) QFAIL("Invalid Age!");
    if(age < 21) QFAIL("Must be an adult!");
    if(age > 40) QWARN("Getting Old!");
    if(age > 65) qInfo() << "This person is retired";
}

BENCHMARK CHECK

#include "widget.h"

Widget::Widget(QObject *parent) : QObject(parent)
{

}

void Widget::testFor()
{
    QVector<int> list;
    list.fill(0,100);

    //Called multiple times!
    QBENCHMARK
    {
        for (int i = 0; i < list.size();i++)
        {
            //Do Stuff
        }
    }
}

void Widget::testForEach()
{
    QVector<int> list;
    list.fill(0,100);

    //Called multiple times!
    QBENCHMARK
    {
        foreach(int value, list)
        {
            //Do Stuff
        }
    }
}

void Widget::testString()
{
    QString him = "Bryan";
    QString her = "Tammy";

    QBENCHMARK
    {
        int ret = him.compare(her);
    }
}

void Widget::testComp()
{
    QString him = "Bryan";
    QString her = "Tammy";

    QBENCHMARK
    {
        QCOMPARE(him, her);
    }
}

About Taehyung Kim

안녕하세요? 8년차 현업 C++ 개발자 김태형이라고 합니다. 😁 C/C++을 사랑하며 다양한 사람과의 협업을 즐깁니다. ☕ 꾸준한 자기개발을 미덕이라 생각하며 노력중이며, 제가 얻은 지식을 홈페이지에 정리 중입니다. 좀 더 상세한 제 이력서 혹은 Private 프로젝트 접근 권한을 원하신다면 메일주세요. 😎

Star
Useful Links