Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Console immediately closes when trying to run a unit test (Qt 5.12.2)



  • Hello all, new here, but not new to QT. I am however new to unit testing. I've been following through the unit test example, which makes things seem like they should be more straightforward than they actually are. I'm trying to use MSVC 2017 to generate and run a solution to my unit test project. However, the console only stays open for a brief instant before closing when I run my test project executable, so something is awry.

    The code I'm trying to run is:

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Includes
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    #include <QtTest/QtTest>
    //#include <QString>
    //#include <QObject>
    //#include <QUuid>
    
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Tests
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    class TestQString : public QObject
    {
        Q_OBJECT
    
    private slots:
        void toUpper();
    };
    
    void TestQString::toUpper()
    {
        QString str = "Hello";
        QCOMPARE(str.toUpper(), QString("HELLO"));
    }
    
    QTEST_MAIN(TestQString)
    #include "test_myFileName.moc"
    
    

    To generate the .pro file for the unit tests, I ran the following batch file:

    REM Build with visual studio make and qmake
    C:\"Program Files (x86)"\"Microsoft Visual Studio"\2017\Community\VC\Auxiliary\Build\vcvarsall.bat x86 && cd C:\Users\me\Documents\Projects\my-project\tests\unit_tests && c:\Qt\5.12.2\msvc2017\bin\qmake -project "QT += testlib"
    

    Which generated the following project file:

    ######################################################################
    # Automatically generated by qmake (3.1) Fri Aug 9 22:26:32 2019
    ######################################################################
    
    QT += testlib
    TEMPLATE = app
    TARGET = unit_tests
    INCLUDEPATH += .
    
    # The following define makes your compiler warn you if you use any
    # feature of Qt which has been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    # Input
    SOURCES += test_myFileName.cpp
    
    

    Then, I ran the following batch script, which generated the actual visual studio project:

    REM Build with visual studio make and qmake
    C:\"Program Files (x86)"\"Microsoft Visual Studio"\2017\Community\VC\Auxiliary\Build\vcvarsall.bat x86 && cd C:\Users\me\Documents\Projects\my-project\tests\unit_tests && c:\Qt\5.12.2\msvc2017\bin\qmake -r -tp vc
    PAUSE
    

    Then, I build this project in visual studio, which makes me sad, for two reasons. The first is the problem that I mentioned in my title. The second is that to even get this far, I needed to include the Qt5Core, Qt5Gui, Qt5Test, and Qt5Widgets dlls in the same folder as my executable. Historically, the qmake and vcvarsall.bat build approach that I've been taking hasn't made it necessary to manually include dlls for my other Qt projects, so I'm not sure why this is happening.

    Anyway, I've spent many hours just trying to get a working unit test up and running, and I'm pretty bummed out. Any suggestions, folks?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Are you using Visual Studio ?
    If so, there's an AddIn that you can use that will help you manage Qt project.



  • @sgaist Yes, I mentioned that I am using visual studio 2017. I have the add-in installed, I'm not sure how that is supposed to help me with this very specific issue.


  • Lifetime Qt Champion

    My question wasn't about the compiler but the IDE since you could be using Qt Creator to develop your unit test.

    That's what I would suggest to test, to see if you obtain there the unit test you want.