2016-01-12 20 views
8

Sono molto nuovo a Qt e Qt Quick. Sto convalidando il framework di test dell'unità Qt Test per Qt Quick e non sono in grado di capire come eseguire i test. Ecco quello che ho, ho creato un progetto SUBDIRS con la seguente struttura:Test unitari per Qt Quick

ProjectSolution 
    ProjectSolution.pro 
    Project 
     Project.pro 
     Sources/main.cpp 
     Resources/qml.qrc/main.qml 
    ProjectTest 
     ProjectTest.pro 
     Sources/main.cpp 
     Resources/qml.qrc/main.qml 
     Resources/qml.qrc/tst_gui.qml 

"Progetto" è l'applicazione da testare ed i miei casi di test sono in "ProjectTest/Resources/qml.qrc/tst_gui. QML".

tst_gui.qml:

import QtQuick 2.5 
import QtTest 1.0 

TestCase { 
    name: "UI Testcase" 
    when: windowShown 

function test_button_click() { 
    mouseClick(click_button, Qt.LeftButton, Qt.NoModifier) 
} 

function test_key_press() { 
    keyClick(Qt.Key_Left) 
    keyClick("a") 
    } 
} 

Ho un pulsante con id "click_button" in "Progetto/Resources/qml.qrc/main.qml" che voglio simulare. Quando eseguo il progetto di test, ottengo fallimento con un messaggio:

FAIL! : tst_gui::UI Testcase::test_button_click() Uncaught exception: click_button is not defined 
C:\Users\sjayaprakash\Qt Test Projects\Qt Test Validation\QtTestValidation6\QtTestValidation6Test\tst_gui.qml(9) : failure location 

Sono sicuro che sto facendo qualcosa di sbagliato. Qualcuno potrebbe aiutarmi?

+0

Non è necessario importare il file 'main.qml'? In 'tst_gui.qml' qualcosa come' import "Project/Resources/qml.qrc/main.qml" ' – Tarod

+0

Ho provato un paio di metodi diversi per importare il file main.qml, usando l'istruzione import e usando un alias. Entrambi non hanno funzionato. Ho finito per spostare tutto il codice qml da 'main.qml' a' tst_gui.qml'. Funziona bene ora, dal momento che il testcase è in grado di trovare il click_button ora. – medasumanth

+0

Ottimo! :) Penso che dovresti scrivere la tua risposta e accettarla. Buona programmazione! – Tarod

risposta

2

Infine, sono riuscito a farlo funzionare. Il test case non è stato in grado di trovare il pulsante poiché si trovava in un file QML diverso. Ho provato a importare e usare l'alias di proprietà, entrambi non hanno funzionato. Ho copiato tutto sul mio tst_gui.qml (lasciando vuoto il mio main.qml) e ora funziona bene.

tst_gui.qml (aggiornato):

import QtTest 1.0 
import QtQuick 2.5 
import QtQuick.Window 2.0 
import QtQuick.Controls 1.4 
import QtQuick.Controls.Styles 1.4 
import QtQuick.Dialogs 1.2 
import QtQml 2.2 


Rectangle { 
    id: main_window 
    visible: true 
    width: Screen.width/2 
    height: Screen.height/2 
    color: "light grey" 

    Rectangle { 

     property alias click_button: click_button 

     id: click_button 
     width: main_window.width/4 
     height: main_window.height/14 
     color: "blue" 
     x: main_window.width/2 - click_button.width/2 
     y: main_window.height/2 - main_window.height/4 
     Text { 
      id: button_text 
      text: qsTr("Click!") 
      font.pointSize: 24 
      color: "white" 
      anchors.horizontalCenter: parent.horizontalCenter 
      anchors.verticalCenter: parent.verticalCenter 
     } 

     MouseArea { 
      anchors.fill: parent 
      onClicked: { 
       //Log to console as a proof of button click simulation 
       console.log("Button was clicked!") 
      } 
     } 
    } 

    TextArea { 
     id: textarea 
     width: main_window.width/2 
     height: main_window.height/8 
     x: main_window.width/2 - textarea.width/2 
     y: (main_window.height/2 - textarea.height/2) + main_window.height/8 

     focus: true 
     selectByKeyboard: true 
     textColor: "darkblue" 
     textFormat: TextEdit.PlainText 
     wrapMode: TextEdit.WrapAtWordBoundaryOrAnywhere 

     Keys.onLeftPressed: { 
      //Log to console as a proof of key press simulation 
      console.log("Left key was pressed!") 
     } 
    } 


    TestCase { 
     name: "UI Testcase" 
     when: windowShown 

     function test_button_click() { 
      mouseClick(click_button, Qt.LeftButton, Qt.NoModifier) 
     } 

     function test_key_press() { 
      keyClick(Qt.Key_Left) 
     } 
    } 
} 

Nel mio main.cpp, sto solo chiamando la macro:

QUICK_TEST_MAIN("tst_gui") 

Probabilmente, il modo giusto di scrivere unit test è quello di separare li dal codice reale. Per ora, questo funziona per me.

+0

Ehi @medasumanth Ho provato qualunque cosa tu abbia menzionato sopra come soluzione, ma sto ricevendo un errore: la directory '/ home/Documents/Testing/Qml_testing_1' non contiene alcun file di test che corrisponda a 'tst _ *. Qml'. Qualche idea su questo errore? Grazie – 1218GG

+0

@ 1218GG, ti sei assicurato di dare un nome al tuo file qml 'tst_' qualcosa? – Matthias