I been trying to get QtQuick Test implemented into and a simple project QML/C++.
I have created a subdir project with two sub projects one project holds app (QML/C++)
I used the Qt Creator Project wizard to create these sub-projects
I have the test libraries installed and have the plugin enabled for AutoTests Libraries: qtdeclarative5-dev-tools qtdeclarative5-test-plugin
I created a QML file in my auto test sub-project called tst_MyItem.qml
this is where I want to create my unit test using javaScript and TestCase object
In Order to test my QML file in my Application subproject:
I have tried importing the QML file into the tst_MyItem.qml file (says can't find it ...)
I have right clicked on my AutoTest sub project add Existing files and added MyItem.qml file from my Application sub-project into my AutoTest Sub-project
I have Tried adding MyItem as a component into tst_MyItem.qml
I have tried creating the tst_MyItem.qml (with javaScript Unit test funitons in it) inside my Application sub-project (mainPage) and tried calling macro from AutoTest Sub projects
TestCase {
name: "Test_MyItem"
// MyItem{
// id: item
// }
TestCase {
name: "Test_MyItem"
// MyItem{
// id: item
// }
To copy to clipboard, switch view to plain text mode
I can create simple TestCase that are irrelevant to the QML I need to test and run it and have it pass or fail but not sure how to test the QML code I have written
Ex
import QtQuick 2.3
import QtTest 1.0
//import "../../../../HelloWorld/QML/MyItem.qml"
TestCase {
name: "Test_MyItem"
// MyItem{
// id: item
// }
function test_testField_text() {
var myString = "Hello World"
compare(myString, "Hello World", "not equal")
}
function test_fail() {
compare(2 + 2, 5, "2 + 2 = 5")
}
}
import QtQuick 2.3
import QtTest 1.0
//import "../../../../HelloWorld/QML/MyItem.qml"
TestCase {
name: "Test_MyItem"
// MyItem{
// id: item
// }
function test_testField_text() {
var myString = "Hello World"
compare(myString, "Hello World", "not equal")
}
function test_fail() {
compare(2 + 2, 5, "2 + 2 = 5")
}
}
To copy to clipboard, switch view to plain text mode
C++ test harnsses main() call with MACRO
#include <QtTest>
#include <QtQuickTest>
#include <QtQuick/QtQuick>
#include <QCoreApplication>
QUICK_TEST_MAIN(Test_MyItem);
#include <QtTest>
#include <QtQuickTest>
#include <QtQuick/QtQuick>
#include <QCoreApplication>
QUICK_TEST_MAIN(Test_MyItem);
To copy to clipboard, switch view to plain text mode
Here is my Project StructurefileStructure_ex.png
Here is MyItem.qml I want to write Unit tests for which is in my sub-project Application
import QtQuick 2.0
import QtQuick.Controls 1.0
import QtQuick.Dialogs 1.1
import QtQml 2.2
Item{
id: item
width: 400
height: 500
//Sends text to c++ fcn
signal submitTextField(string text)
// this function is our QML slot
function setTextField(text){
console.log("setTextField: " + text)
textField1.text = text
}
Button{
id: quitButton
width: 100
height: 20
text: "Quit"
anchors.top: parent.top
anchors.topMargin: 10
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
console.log("Quit button clicked")
}
}
}
TextField {
id: textField1
x: 31
y: 169
placeholderText: qsTr("Enter some text...")
}
Button {
x: 193
y: 167
text: qsTr("Uppercase me!")
onClicked:
// emit the submitTextField signal
submitTextField(textField1.text)
}
Button{
id:openWin_btn
text: "open window"
anchors.left: quitButton.right
anchors.top: parent.top
anchors.topMargin: 10
MouseArea {
anchors.fill: parent
onClicked:{
console.log("btn clicked")
//stackview.pop(item)
//stackview.push(mainView_comp)
//showHomeScreen();
}
}
}
// Connections{
// id: connectionForSignal
// target:
// }
StackView {
id: stackview
width: parent.width
height: parent.height
anchors.fill: parent
//initialItem: Qt.resolvedUrl("qrc:/QML/MyItem.qml")
}
// Component{
// id: mainView_comp
// MainView{
// id: mainView_page
// objectName: "mainView_page"
// }
// }
}
import QtQuick 2.0
import QtQuick.Controls 1.0
import QtQuick.Dialogs 1.1
import QtQml 2.2
Item{
id: item
width: 400
height: 500
//Sends text to c++ fcn
signal submitTextField(string text)
// this function is our QML slot
function setTextField(text){
console.log("setTextField: " + text)
textField1.text = text
}
Button{
id: quitButton
width: 100
height: 20
text: "Quit"
anchors.top: parent.top
anchors.topMargin: 10
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
console.log("Quit button clicked")
}
}
}
TextField {
id: textField1
x: 31
y: 169
placeholderText: qsTr("Enter some text...")
}
Button {
x: 193
y: 167
text: qsTr("Uppercase me!")
onClicked:
// emit the submitTextField signal
submitTextField(textField1.text)
}
Button{
id:openWin_btn
text: "open window"
anchors.left: quitButton.right
anchors.top: parent.top
anchors.topMargin: 10
MouseArea {
anchors.fill: parent
onClicked:{
console.log("btn clicked")
//stackview.pop(item)
//stackview.push(mainView_comp)
//showHomeScreen();
}
}
}
// Connections{
// id: connectionForSignal
// target:
// }
StackView {
id: stackview
width: parent.width
height: parent.height
anchors.fill: parent
//initialItem: Qt.resolvedUrl("qrc:/QML/MyItem.qml")
}
// Component{
// id: mainView_comp
// MainView{
// id: mainView_page
// objectName: "mainView_page"
// }
// }
}
To copy to clipboard, switch view to plain text mode
Bookmarks