Qt Quick的单元测试

12

我对Qt和Qt Quick非常陌生。我正在验证Qt Test单元测试框架是否适用于Qt Quick,但是我不知道如何运行测试。

这是我的项目结构:

SUBDIRS项目包含以下内容:

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
"Project" 是待测试的应用程序,我的测试用例在 "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")
    }
}

我有一个id为"click_button"的按钮,位于"Project/Resources/qml.qrc/main.qml"中,我想模拟它。执行测试项目时,我收到了失败消息:

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

我确定我做错了什么。有人可以帮忙吗?


你不需要导入你的 main.qml 文件吗?在 tst_gui.qml 中应该像这样写:import "Project/Resources/qml.qrc/main.qml" - Tarod
我尝试了几种不同的方法来导入main.qml文件,包括使用import语句和使用别名。但都没有成功。最终我将所有qml代码从main.qml移动到tst_gui.qml中。现在它可以正常工作了,因为测试用例现在能够找到click_button。 - medasumanth
太好了! :) 我认为你应该写下自己的答案并接受它。愉快编码! - Tarod
1个回答

5

最终,我成功让它运行起来了。测试用例无法找到按钮,因为它在不同的QML文件中。我尝试使用导入和属性别名,但两者都没有起作用。

我将所有内容复制到我的tst_gui.qml文件中(将main.qml留空),现在它可以正常工作。

tst_gui.qml(已更新):

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

在我的main.cpp文件中,我只是调用了宏:
QUICK_TEST_MAIN("tst_gui")

可能,编写单元测试的正确方法是将它们与实际代码分离。目前,这对我来说行得通。


嘿 @medasumanth,我尝试了你上面提到的解决方案,但是我遇到了一个错误:目录 '/home/Documents/Testing/Qml_testing_1' 中没有与 'tst_*.qml' 匹配的测试文件。对于这个错误有什么想法吗?谢谢 - 1218GG
@1218GG 你确定给你的 QML 文件命名为 tst_ 开头了吗? - Matthias

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接