Setting up a Testable QML Project

Setting up a QT Project that makes it easy to use QML TestCase is a bit different from just using a normal QML Project and then adding tests. QML TestCase requires a TestRunner defined in C++ that will run all of the QML tests. This TestRunner also must be the entry point of its project which can cause issues if you want to manually test your Components, or just want to keep your testing seperated out from your main application logic.

I have found that an easy solution is to use the QT Subdirs Project and defining an application project, and a Test Runner Project.

Setting Up a SubDirs Project.

  1. In QT Creator create a new project
  2. Under Other Projects select SubDirs Project
  3. SubDir Project menu in QTCreator
  4. Define your Project Name
  5. Define the Kits that you wish to use.
  6. After you finish setting up the main SubDirs project it will prompt you to add a sub-project. Select yes. This will be the main application.
  7. Define the main applications project name.
  8. Define the kits.
  9. Once you have finished you will now see the application as a subproject.
  10. Now we can add in the Test Runner project.
  11. Right click on the parent Project and select Add New Subproject.
  12. Next I usually select another QML Quick application as the project type. Selecting the QML Quick Application will create a .pro file with all of our main requirements. It does however also include some extra files that aren't nessiary
  13. Continue through the rest of our dialogs to finish creating our test runner project.

You should now have two projects visible in QT Creator, and we can now make our TestRunner project to actually run tests.

Setting Up TestRunner Project

  1. First we need to add qmltestcase into the .pro file under our TestRunner Project
  2. CONFIG += c++11 qmltestcase
                        
  3. Next edit the main.cpp to contain:

    #include <QtQuickTest>
    QUICK_TEST_MAIN(example)
                                
                            
  4. From here you should be able to right click on the Test Runner Project and run it. The application output should be similar to:

    The directory '/.../ToDo_TestRunner' does not contain any test files matching 'tst_*.qml'
                                
                            

    The TestRunner is going to search its directory for any tst_*.qml files and then run the TestCase Components in those files.

  5. Since we are using two seperate subprojects we will need to tell the TestRunner where to look for the tst_*.qml files.

    #include <QtQuickTest>
    #define QUICK_TEST_SOURCE_DIR "../../QMLTesting/ToDo/" 
    QUICK_TEST_MAIN(example)
                                
                            

    If you are using a relative path to define the QUICK_TEST_SOURCE_DIR make sure to remember that the path is starting from where the TestRunner Executable is being built and run, not were the source code is stored.

  6. Now in the main application add a QML file named tst_test.qml. This will allow for our test runner to find this empty test file.