Solved Qt Test
-
Hi,
I have a simple code where on click color changes as shown below. I want to write QTest for this. As i am new to Qtest i want what all steps need to refer for testing i.e. onclick color changes to yellow or not, also text color is black or not.import QtQuick 2.11
import QtQuick.Window 2.11Window
{
visible: true
width: 640
height: 480
title: qsTr("Hello World")Rectangle { id: b1 width: 100 height: 50 color: "red" anchors.centerIn: parent Text { id: name text: qsTr("Button") color: "black" font.pixelSize: 10 anchors.centerIn: parent } MouseArea { anchors.fill: parent onClicked: { b1.color = "yellow" } } }
}
-
@JasmineSethi
I suggest you to read the QtTest documentation.
https://doc.qt.io/qt-5/qml-qttest-testcase.htmlCheck out this example:
Edit:
"red" -> "#ff000"
"yellow" -> "#ffff00"
mouseClick(mouseArea) instead mouseArea.clicked()TestCase { name: "rectangleColor" when: windowShown function test_rectColor() { compare(b1.color, "#ff0000"); // check color mouseClick(mouseArea); // emit clicked signal compare(b1.color, "#ffff00"); // check color } }
-
@KillerSmath Hi, Thanks for your previous response.
I have another question.
I have a function which is part of button.qml as shown below. I want to do qtest for this. How can i ?
function setDisableState(disable)
{
properties.disableButton = disable
//properties is part of QtObject.
//i.e. property bool disableButton: falseif(disable === true) { buttonAudioPause.color = properties.disableStateModern buttonAudioPause.border.color = properties.disableStateModern mainAudioPauseButton.enabled = false properties.btnTextColor = properties.modernGray4 } else if(disable === false) { buttonAudioPause.color = properties.buttonColor buttonAudioPause.border.color = properties.buttonColor mainAudioPauseButton.enabled = true properties.btnTextColor = properties.blackColor } }
-
@JasmineSethi
You can call the function using the object identifierbuttonAudioPause.setDisableState(true)
and check if the variable has been changed using compare calls. -
I wrote 2 function set() and get()
function setDisableState(disable)
{
properties.disableButton = disableif(disable === true) { buttonAudioPause.color = "some color value" buttonAudioPause.border.color = "some color value" mainAudioPauseButton.enabled = false properties.btnTextColor = "some color value" } else if(disable === false) { buttonAudioPause.color = "some color value" buttonAudioPause.border.color ="some color value" mainAudioPauseButton.enabled = true properties.btnTextColor = "some color value" } } function getDisableState(disable) { return properties.disableButton }
My TestCase looks something like this:
I am instantiating qml file and using its id to call the set() and get() function.
RSBB_ButtonWidget_AudioPause
{
id: rsbbAudioPauseButton}
TestCase
{
name: "Disable state"
when: windowShownfunction test_disableStateForAudioPauseButton() { rsbbAudioPauseButton.setDisableState(true) compare(rsbbAudioPauseButton.getDisableState(),?,?)//?? } }
//My question is how to test more than one condition(for example buttonAudioPause.color, buttonAudioPause.border.color) in "compare" statement when setDisableState(true) and setDisableState(false) ?
-
@JasmineSethi
Compare function only compares pairs of data.
However, it may be generalize using object comparison.{ key_1: value_1, key_2: value_2 }
So, you can return a object with button states and compare with an expected result. Here is a example.
function getDisableState() { return { enabled: yourButtonID.enabled, color: yourButtonID.color } } // your code TestCase { function test_disableStateForAudioPauseButton() { yourButtonID.setDisableState(true) compare(yourButtonID.getDisableState(), {enabled: false, color: "some color value"}) } }
-
Not able to return more than one object as shown neither this nor the way u mentioned.
-
@JasmineSethi said in Qt Test:
Not able to return more than one object as shown neither this nor the way u mentioned.
I have mentioned this structure:
{ key_1: value_1, key_2: value_2 }
-
Tried the same way too. It didn't worked
-
@JasmineSethi
A explanation about why it is happening.
https://stackoverflow.com/questions/18221963/javascript-function-fails-to-return-object-when-there-is-a-line-break-between-th -
@KillerSmath
Yeah i went through this link. Then i think its not a good practice to keep the return statement for more than one line. Then wt's the best solution to do qt test for the problem i stated? Any idea? -
@JasmineSethi
You could call the change function and test each property with individual compare calls. And btw, it is better than using an object, because in the case of object, the error output will print "Object is different of Object".
but using the unique property, the value of the properties will be printed, facilitating the diagnosis. -
@KillerSmath
Hi,
I took an array and kept all the properties like :
property variant getdisablearray: [buttonAudioPause.color,
buttonAudioPause.border.color, mainAudioPauseButton.enabled, properties.btnTextColor]In get() returned this array
function getDisableState()
{
return properties.getdisablearray
}//Test case looks like below:
property variant abc:[]TestCase { name: "Disable state for audio pause" when: windowShown function test_disableStateForAudioPauseButton() { rsbbAudioPauseButton.setDisableState(true) abc = rsbbAudioPauseButton.getDisableState() compare(abc[0],"#17202a","test not pass") compare(abc[1],"#17202a","test not pass") compare(abc[2],false,"test not pass") compare(abc[3],"#666666","test not pass") } }