如何创建 Qt 快速测试 [英] How to create a Qt-Quick Test

查看:113
本文介绍了如何创建 Qt 快速测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建一个单元测试.

但首先,我必须弄清楚要做什么.编写了一个 QtQuick2-App,现在我想用 GUI 进行单元测试.使用 GUI 进行单元测试的步骤是什么?阅读 Qt 文档后,我无法创建任何开始测试的想法.

希望有人能帮助我.

编辑:将 tst_button.qmltst_test.cpp 添加到我的项目(main.cpp现在在评论中).这是正确的方法,还是应该为测试创建一个新项目?如果是,需要什么样的项目?最后一个问题:我是否需要构建我的 MainForm 来按下按钮?

tst_button.qml

导入QtQuick 2.4导入 QtTest 1.0长方形{编号:myRec属性 var myMainForm: null测试用例{名称:按钮点击"时间:窗口显示函数 test_init(){var createMyWindow = "导入 QtQuick 2.0; MainForm{id:myForm}"var myMainForm = Qt.createQmlObject(createMyWindow,myRec)myRec.myMainForm = myMainForm}}}

tst_test.cpp

#include QUICK_TEST_MAIN(测试)

解决方案

测试和调试列出了两种方式:

<块引用>

您可以使用 Qt Test 来测试 Qt Quick 应用程序,但是当您需要访问 QML 中不可用的 C++ API 时,这通常更好.

<块引用>

我是否只是在我的项目中添加一个 *.qml 文件并用我的代码填充它?如果是,我需要做什么才能开始测试?

您首先需要将测试作为一个单独的项目,除非您打算使用 qmltestrunner(我不知道为什么 Qt 本身没有记录该工具).

Qt Quick 的运行测试部分测试文档详细说明了如何启动和运行测试.

<小时><块引用>

在将 tst_button.qml 和 tst_test.cpp 添加到我的项目后,我能够运行一些测试(main.cpp 现在在评论中).这是正确的方法,还是我应该为测试创建一个新项目?

例如,如果您的应用程序是纯 QML 并且仅打算与 qmlscene 一起运行,那么这样做就可以了.但是,如果您打算部署/交付您的应用程序,您可能需要一个可执行文件,这意味着为应用程序和测试制作单独的项目.

<块引用>

如果是,需要什么样的项目?

你可以有一个 SUBDIRS 项目,以便您的测试和应用程序本身都可以在 Qt Creator 中同时打开.像这样:

myapp.pro应用程序/主程序应用程序资源.qrc主文件测试/测试版数据/tst_stuff.qml

<块引用>

最后一个问题:例如,我是否需要构建我的 MainForm 来按下按钮?

没有..ui 功能 只是一种格式,允许 Qt Creator 强制执行某些约束以使其成为使用 Qt Quick Designer 更容易设计 Qt Quick UI.MainForm.ui.qml 因此只是一种方便.如果您在 QML 中已经有一个现有组件,您可以创建该组件的实例并对其进行测试.

I have to create a Unit-Test.

But first, I´ve to get clear what to do. There is a QtQuick2-App written and now I would like to do Unit-Tests with the GUI. What are the steps for Unit-Tests with GUI? After reading the Qt-documents, I could not create any ideas for starting with the test.

Hope somebody can help me.

Edit: I was able to run some tests, after adding tst_button.qml and tst_test.cpp to my Project (main.cpp is in comments now). Is this the right way, or should I create a new project just for the Tests? If yes, what kind of project is needed? And the last question: Do I need to build up my MainForm for pressing buttons for example?

tst_button.qml

import QtQuick 2.4
import QtTest 1.0

Rectangle{
    id: myRec
    property var myMainForm: null

    TestCase{
        name:"ButtonClick"
        when:windowShown

        function test_init(){
           var createMyWindow = "import QtQuick 2.0; MainForm{id:myForm}"
           var myMainForm = Qt.createQmlObject(createMyWindow,myRec)
            myRec.myMainForm = myMainForm
        }
      }
  }

tst_test.cpp

#include <QtQuickTest/quicktest.h>
QUICK_TEST_MAIN(test)

解决方案

Testing and Debugging lists two ways:

You can use Qt Test for testing Qt Quick applications, but that's generally better for when you need access to C++ API that isn't available in QML.

Do I just add a *.qml file to my project and fill it with my code? If yes, what do I have to do to start the test?

You'll first need to make the tests a separate project, unless you're planning on using qmltestrunner (I have no idea why that tool isn't documented by Qt itself).

The Running Tests section of Qt Quick Test's documentation details how to get a test up and running.


I was able to run some tests, after adding tst_button.qml and tst_test.cpp to my Project (main.cpp is in comments now). Is this the right way, or should I create a new project just for the Tests?

If your application is pure QML and only intended to be run with qmlscene, for example, then doing it that way is fine. However, if you intend to deploy/ship your application, you'll probably need to have an executable, which means making separate projects for the application and the tests.

If yes, what kind of project is needed?

You could have a SUBDIRS project, so that your tests and the application itself can all be opened at once in Qt Creator. Something like this:

myapp.pro
app/
    main.cpp
    app.pro
    resources.qrc
    main.qml
tests/
    tests.pro
    data/
        tst_stuff.qml

And the last question: Do I need to build up my MainForm for pressing buttons for example?

No. The .ui feature is just a format that allows Qt Creator to enforce certain constraints to make it easier to design Qt Quick UIs with Qt Quick Designer. MainForm.ui.qml is therefore just a convenience. If you already have an existing component in QML, you can create instances of that and test it.

这篇关于如何创建 Qt 快速测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆