Qt_Core_AD_5_4 - 8BitsCoding/RobotMentor GitHub Wiki


#include <QtCore/QCoreApplication>

#include <qtest.h>

#include "Widget.h"

// Replace main func (but not work at VS)
// QTEST_MAIN(Widget);

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

	Widget w;
	QTest::qExec(&w);

	return a.exec();
}
#pragma once

#include <QObject>
#include <qdebug.h>
#include <qtest.h>

class Widget : public QObject
{
	Q_OBJECT

public:
	explicit Widget(QObject *parent = nullptr);
	~Widget();

signals:

public slots:

private slots:
	void testAge();
	void testAge_data();

private:
	int age;
};
#include "Widget.h"

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

Widget::~Widget()
{
}


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

	QTest::addColumn<QString>("name");
	QTest::addColumn<QString>("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" << 14;

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

void Widget::testAge_data()
{
	// Get the row data
	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!";
}

이미지

⚠️ **GitHub.com Fallback** ⚠️