2016-01-12 7 views
8

QtとQtクイックの新機能です。私はQt QuickのQtテストユニットテストフレームワークを検証しており、テストの実行方法を理解できません。 は、ここで私は以下の構造でSUBDIRSプロジェクトを作成して、私が持っているものです。ユニットテストQtクイック

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 

「プロジェクト」テスト対象のアプリケーションであり、私のテストケースは、「ProjectTest /リソース/ 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") 
    } 
} 

私は "プロジェクト/リソース/ qml.qrc/main.qml" 私がシミュレートしたいのid "click_button" とボタンがあります。私はテストプロジェクトを実行すると は、私はメッセージで失敗を取得する:

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 

私は私が何か間違ったことをやっていると確信しています。誰かが助けてくれますか?

+0

'main.qml'ファイルをインポートする必要はありませんか? 'tst_gui.qml'では' import 'のようになりましたProject/Resources/qml.qrc/main.qml " – Tarod

+0

main.qmlファイルをインポートするには、importステートメントを使用してエイリアスを使用する方法をいくつか試してみました。どちらもうまくいきませんでした。私はすべてのqmlコードを 'main.qml'から' tst_gui.qml'に移動しました。テストケースがclick_buttonを今見つけられるので、今はうまくいきます。 – medasumanth

+0

素晴らしい!私はあなた自身の答えを書いてそれを受け入れるべきだと思います。ハッピーコーディング! – Tarod

答えて

2

最後に、私はそれを機能させることができました。ボタンが別のQMLファイルにあるため、テストケースがボタンを検出できませんでした。私は、インポートしようとしたプロパティエイリアスを使用して、両方が動作しませんでした。 すべてを私のtst_gui.qmlにコピーしました(私のmain.qmlは空のままにしました)。

tst_gui.qml(更新):

QUICK_TEST_MAIN("tst_gui") 

おそらく、ユニットテストを書くための正しい方法を分離することである:私のmain.cppには

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) 
     } 
    } 
} 

は、私はマクロを呼び出すいますそれらは実際のコードからです。今のところ、これは私のために働く。

+0

ちょっと@medasumanth私はあなたの解決策として上記の言及を試しましたが、エラーが出ます: '/ home/Documents/Testing/Qml_testing_1'ディレクトリに 'tst _ *。qml'と一致するテストファイルがありません。このエラーに関する考えはありますか?ありがとう – 1218GG

+0

@ 1218GG qmlファイルに 'tst_'という名前を付けることを確認しましたか? – Matthias

関連する問題