Results 1 to 1 of 1

Thread: Launch unit test with concole command

  1. #1
    Join Date
    Nov 2019
    Location
    Lyon, France
    Posts
    18
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Launch unit test with concole command

    Hi!

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

    TestingClass.h:
    Qt Code:
    1. #ifndef TESTINGCLASS_H
    2. #define TESTINGCLASS_H
    3.  
    4. #include <QObject>
    5. #include <QtTest/QtTest>
    6.  
    7. #include "Calculus.h"
    8.  
    9. class TestingClass : public QObject
    10. {
    11. Q_OBJECT
    12.  
    13. private slots:
    14. void testCalculus();
    15. };
    16.  
    17. #endif // TESTINGCLASS_H
    To copy to clipboard, switch view to plain text mode 

    TestingClass.cpp:
    Qt Code:
    1. #include "TestingClass.h"
    2.  
    3. void TestingClass::testCalculus()
    4. {
    5. Calculus calc;
    6. int sum = calc.addition(5, 5);
    7.  
    8. QCOMPARE(sum, 10);
    9. }
    To copy to clipboard, switch view to plain text mode 


    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:

    Qt Code:
    1. void MainWidget::on_testButton_clicked()
    2. {
    3. TestingClass test;
    4. QTest::qExec(&test);
    5. }
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. #include "MainWidget.h"
    2. #include "TestingClass.h"
    3.  
    4. #include <QApplication>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9.  
    10.  
    11. QStringList args = QCoreApplication::arguments();
    12. for (int i = 0; i < args.size(); i++)
    13. {
    14. if (args.at(i) == "runTest")
    15. {
    16. TestingClass test;
    17. QTest::qExec(&test);
    18. return 0; // So that the application is not launched
    19. }
    20. }
    21.  
    22.  
    23. MainWidget w;
    24. w.show();
    25. return a.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

    This works fine. But I'm not entirely sure that this is the correct way of doing things...
    Last edited by Thibaut; 25th November 2021 at 13:58.

Similar Threads

  1. Unit Test Tool for QT
    By mythili in forum Installation and Deployment
    Replies: 5
    Last Post: 1st May 2017, 19:07
  2. Qml unit test
    By coder1998 in forum Qt Quick
    Replies: 5
    Last Post: 10th December 2015, 22:20
  3. How do I unit test a GUI
    By jolema in forum Newbie
    Replies: 3
    Last Post: 31st August 2015, 14:47
  4. Unit Test: Test if QPushButton has a menu
    By FelixB in forum Qt Programming
    Replies: 1
    Last Post: 7th November 2012, 13:12
  5. How to unit test a Qt Gui
    By mitskits in forum Qt Programming
    Replies: 1
    Last Post: 20th January 2006, 08:36

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.