PDA

View Full Version : Launch unit test with concole command



Thibaut
25th November 2021, 12:58
Hi!

I'm quite new to unit testing in Qt, but I managed to do a basic one. Here's the code:

TestingClass.h:


#ifndef TESTINGCLASS_H
#define TESTINGCLASS_H

#include <QObject>
#include <QtTest/QtTest>

#include "Calculus.h"

class TestingClass : public QObject
{
Q_OBJECT

private slots:
void testCalculus();
};

#endif // TESTINGCLASS_H


TestingClass.cpp:


#include "TestingClass.h"

void TestingClass::testCalculus()
{
Calculus calc;
int sum = calc.addition(5, 5);

QCOMPARE(sum, 10);
}



In the Calculus class, I just have a simple method that return the sum of two values, nothing more.
To run the test, I have a button in my main widget that can launch them when it's clicked, like so:



void MainWidget::on_testButton_clicked()
{
TestingClass test;
QTest::qExec(&test);
}


This work fine and as intended. When I click the button, the test are launched and I have the correct results in the application output of Qt Creator.

Now, I was wondering how I could launch those tests from a console command. I tried following this tutorial "https://doc.qt.io/qt-5/qtest-overview.html", but I find it a bit confusing.
Do someone know the steps to take to achieve that?

Added after 23 minutes:

Just to make an update.

I add this to the main.cpp:


#include "MainWidget.h"
#include "TestingClass.h"

#include <QApplication>

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


QStringList args = QCoreApplication::arguments();
for (int i = 0; i < args.size(); i++)
{
if (args.at(i) == "runTest")
{
TestingClass test;
QTest::qExec(&test);
return 0; // So that the application is not launched
}
}


MainWidget w;
w.show();
return a.exec();
}


This works fine. But I'm not entirely sure that this is the correct way of doing things...