import QtQuick
import QtQuick.Controls
Button {
width: 50
height: 50
onClicked: width = 100
}
另请参阅
SignalSpy
and
Qt Quick Test
.
特性文档编制
This property will be set to true once the test case has completed execution. Test cases are only executed once. The initial value is false.
另请参阅
running
and
when
.
This property defines the name of the test case for result reporting. The default value is an empty string.
TestCase {
name: "ButtonTests"
...
}
多
TestCase
types can be supplied in a test application. The application will exit once they have all completed. If a test case does not need to run (because a precondition has failed), then this property can be set to true. The default value is false.
TestCase {
when: false
optional: true
function test_not_run() {
verify(false)
}
}
另请参阅
when
and
completed
.
This property will be set to true while the test case is running. The initial value is false, and the value will become false again once the test case completes.
另请参阅
completed
and
when
.
This property should be set to true when the application wants the test cases to run. The default value is true. In the following example, a test is run when the user presses the mouse button:
Rectangle {
id: foo
width: 640; height: 480
color: "cyan"
MouseArea {
id: area
anchors.fill: parent
}
property bool bar: true
TestCase {
name: "ItemTests"
when: area.pressed
id: test1
function test_bar() {
verify(bar)
}
}
}
The test application will exit once all
TestCase
types have been triggered and have run. The
optional
property can be used to exclude a
TestCase
类型。
另请参阅
optional
and
completed
.
This property will be set to true after the QML viewing window has been displayed. Normally test cases run as soon as the test application is loaded and before a window is displayed. If the test case involves visual types and behaviors, then it may need to be delayed until after the window is shown.
Button {
id: button
onClicked: text = "Clicked"
TestCase {
name: "ClickTest"
when: windowShown
function test_click() {
button.clicked();
compare(button.text, "Clicked");
}
}
}
方法文档编制
This function is called after each test function that is executed in the
TestCase
type. The default implementation does nothing. The application can provide its own implementation to perform cleanup after each test function.
另请参阅
init
() 和
cleanupTestCase
().
This function is called after all other test functions in the
TestCase
type have completed. The default implementation does nothing. The application can provide its own implementation to perform test case cleanup.
另请参阅
initTestCase
() 和
cleanup
().
compare
(
actual
,
expected
,
message
= "")
|
Fails the current test case if
actual
is not the same as
expected
, and displays the optional
message
. Similar to
QCOMPARE(actual, expected)
in C++.
另请参阅
tryCompare
() 和
fuzzyCompare
.
对象
createTemporaryObject
(
Component
component
,
对象
parent
,
对象
properties
)
|
This function dynamically creates a QML object from the given
component
with the specified optional
parent
and
properties
. The returned object will be destroyed (if it was not already) after
cleanup
() has finished executing, meaning that objects created with this function are guaranteed to be destroyed after each test, regardless of whether or not the tests fail.
If there was an error while creating the object,
null
将被返回。
此函数调用
component.createObject
() internally.
另请参阅
管理动态创建的测试对象
.
对象
createTemporaryQmlObject
(
string
qml
,
对象
parent
,
string
filePath
)
|
This function dynamically creates a QML object from the given
qml
string with the specified
parent
. The returned object will be destroyed (if it was not already) after
cleanup
() has finished executing, meaning that objects created with this function are guaranteed to be destroyed after each test, regardless of whether or not the tests fail.
If there was an error while creating the object,
null
将被返回。
若
filePath
is specified, it will be used for error reporting for the created object.
此函数调用
Qt.createQmlObject
() internally.
另请参阅
管理动态创建的测试对象
.
expectFail
(
tag
,
message
)
|
In a data-driven test, marks the row associated with
tag
as expected to fail. When the fail occurs, display the
message
, abort the test, and mark the test as passing. Similar to
QEXPECT_FAIL(tag, message, Abort)
in C++.
If the test is not data-driven, then
tag
must be set to an empty string.
另请参阅
expectFailContinue
().
expectFailContinue
(
tag
,
message
)
|
In a data-driven test, marks the row associated with
tag
as expected to fail. When the fail occurs, display the
message
, and then continue the test. Similar to
QEXPECT_FAIL(tag, message, Continue)
in C++.
If the test is not data-driven, then
tag
must be set to an empty string.
另请参阅
expectFail
().
Fails the current test case, with the optional
message
. Similar to
QFAIL(message)
in C++.
failOnWarning
(
message
)
|
Appends a test failure to the test log for each warning that matches
message
. The test function will continue execution when a failure is added.
message
can be either a string, or a regular expression providing a pattern of messages. In the latter case, for each warning encountered, the first pattern that matches will cause a failure, and the remaining patterns will be ignored.
All patterns are cleared at the end of each test function.
For example, the following snippet will fail a test if a warning with the text "Something bad happened" is produced:
failOnWarning("Something bad happened")
The following snippet will fail a test if any warning matching the given pattern is encountered:
failOnWarning(/[0-9]+ bad things happened/)
To fail every test that triggers a given warning, pass a suitable regular expression to this function in
init
():
function init() {
failOnWarning(/.?/)
}
注意:
Despite being a JavaScript RegExp object, it will not be interpreted as such; instead, the pattern will be passed to
QRegularExpression
.
注意:
ignoreMessage() takes precedence over this function, so any warnings that match a pattern given to both
ignoreMessage()
and
failOnWarning()
将被忽略。
This method was introduced in Qt 6.3.
另请参阅
QTest::failOnWarning
() 和
warn
().
QtObject
findChild
(
parent
,
objectName
)
|
Returns the first child of
parent
with
objectName
,或
null
if no such item exists. Both visual and non-visual children are searched recursively, with visual children being searched first.
compare(findChild(item, "childObject"), expectedChildObject);
fuzzyCompare
(
actual
,
expected
,
delta
,
message
= "")
|
Fails the current test case if the difference betwen
actual
and
expected
大于
delta
, and displays the optional
message
. Similar to
qFuzzyCompare(actual, expected)
in C++ but with a required
delta
值。
This function can also be used for color comparisons if both the
actual
and
expected
values can be converted into color values. If any of the differences for RGBA channel values are greater than
delta
, the test fails.
另请参阅
tryCompare
() 和
compare
().
Returns a snapshot image object of the given
item
.
The returned image object has the following properties:
-
width Returns the width of the underlying image (since 5.10)
-
height Returns the height of the underlying image (since 5.10)
-
size Returns the size of the underlying image (since 5.10)
Additionally, the returned image object has the following methods:
-
red(x, y)
Returns the red channel value of the pixel at
x
,
y
位置
-
green(x, y)
Returns the green channel value of the pixel at
x
,
y
位置
-
blue(x, y)
Returns the blue channel value of the pixel at
x
,
y
位置
-
alpha(x, y)
Returns the alpha channel value of the pixel at
x
,
y
位置
-
pixel(x, y)
Returns the color value of the pixel at
x
,
y
位置
-
equals(image)
返回
true
if this image is identical to
image
- see
QImage::operator==
(since 5.6)
例如:
let image = grabImage(rect);
compare(image.red(10, 10), 255);
compare(image.pixel(20, 20), Qt.rgba(255, 0, 0, 255));
rect.width += 10;
let newImage = grabImage(rect);
verify(!newImage.equals(image));
-
save(path)
Saves the image to the given
path
. If the image cannot be saved, an exception will be thrown. (since 5.10)
This can be useful to perform postmortem analysis on failing tests, for example:
let image = grabImage(rect);
try {
compare(image.width, 100);
} catch (ex) {
image.save("debug.png");
throw ex;
}
ignoreWarning
(
message
)
|
Marks
message
as an ignored warning message. When it occurs, the warning will not be printed and the test passes. If the message does not occur, then the test will fail. Similar to
QTest::ignoreMessage(QtWarningMsg, message)
in C++.
从 Qt 5.12 起,
message
can be either a string, or a regular expression providing a pattern of messages to ignore.
For example, the following snippet will ignore a string warning message:
ignoreWarning("Something sort of bad happened")
And the following snippet will ignore a regular expression matching a number of possible warning messages:
ignoreWarning(new RegExp("[0-9]+ bad things happened"))
注意:
Despite being a JavaScript RegExp object, it will not be interpreted as such; instead, the pattern will be passed to
QRegularExpression
.
另请参阅
warn
().
This function is called before each test function that is executed in the
TestCase
type. The default implementation does nothing. The application can provide its own implementation to perform initialization before each test function.
另请参阅
cleanup
() 和
initTestCase
().
This function is called before any other test functions in the
TestCase
type. The default implementation does nothing. The application can provide its own implementation to perform test case initialization.
另请参阅
cleanupTestCase
() 和
init
().
bool
isPolishScheduled
(
对象
itemOrWindow
)
|
若
itemOrWindow
是
Item
,此函数返回
true
if
updatePolish
() has not been called on it since the last call to
polish
(),否则返回
false
.
Since Qt 6.5, if
itemOrWindow
是
Window
,此函数返回
true
if
updatePolish
() has not been called on any item it manages since the last call to
polish
() on those items, otherwise returns
false
.
When assigning values to properties in QML, any layouting the item must do as a result of the assignment might not take effect immediately, but can instead be postponed until the item is polished. For these cases, you can use this function to ensure that items have been polished before the execution of the test continues. For example:
verify(isPolishScheduled(item))
verify(waitForItemPolished(item))
Without the call to
isPolishScheduled()
above, the call to
waitForItemPolished()
might see that no polish was scheduled and therefore pass instantly, assuming that the item had already been polished. This function makes it obvious why an item wasn't polished and allows tests to fail early under such circumstances.
另请参阅
waitForPolish
(),
QQuickItem::polish
(),和
QQuickItem::updatePolish
().
keyClick
(
key
,
modifiers
= Qt.NoModifier,
delay
= -1)
|
Simulates clicking of
key
with optional
modifiers
on the currently focused item. If
delay
is larger than 0, the test will wait for
delay
毫秒。
The event will be sent to the
TestCase
window or, in case of multiple windows, to the current active window. See
QGuiApplication::focusWindow
() 了解更多细节。
另请参阅
keyPress
() 和
keyRelease
().
keyPress
(
key
,
modifiers
= Qt.NoModifier,
delay
= -1)
|
Simulates pressing a
key
with optional
modifiers
on the currently focused item. If
delay
is larger than 0, the test will wait for
delay
毫秒。
The event will be sent to the
TestCase
window or, in case of multiple windows, to the current active window. See
QGuiApplication::focusWindow
() 了解更多细节。
注意:
At some point you should release the key using
keyRelease
().
另请参阅
keyRelease
() 和
keyClick
().
keyRelease
(
key
,
modifiers
= Qt.NoModifier,
delay
= -1)
|
Simulates releasing a
key
with optional
modifiers
on the currently focused item. If
delay
is larger than 0, the test will wait for
delay
毫秒。
The event will be sent to the
TestCase
window or, in case of multiple windows, to the current active window. See
QGuiApplication::focusWindow
() 了解更多细节。
另请参阅
keyPress
() 和
keyClick
().
keySequence
(
keySequence
)
|
Simulates typing of
keySequence
. The key sequence can be set to one of the
标准键盘快捷键
, or it can be described with a string containing a sequence of up to four key presses.
Each event shall be sent to the
TestCase
window or, in case of multiple windows, to the current active window. See
QGuiApplication::focusWindow
() 了解更多细节。
另请参阅
keyPress
(),
keyRelease
(),
GNU Emacs 样式键序列
,和
Shortcut.sequence
.
mouseClick
(
item
,
x
= item.width / 2,
y
= item.height / 2,
button
= Qt.LeftButton,
modifiers
= Qt.NoModifier,
delay
= -1)
|
Simulates clicking a mouse
button
with optional
modifiers
on an
item
. The position of the click is defined by
x
and
y
。若
x
and
y
are not defined the position will be the center of
item
。若
delay
is specified, the test will wait for the specified amount of milliseconds before pressing and before releasing the button.
The position given by
x
and
y
is transformed from the co-ordinate system of
item
into window co-ordinates and then delivered. If
item
is obscured by another item, or a child of
item
occupies that position, then the event will be delivered to the other item instead.
另请参阅
mousePress
(),
mouseRelease
(),
mouseDoubleClickSequence
(),
mouseMove
(),
mouseDrag
(),和
mouseWheel
().
mouseDoubleClickSequence
(
item
,
x
= item.width / 2,
y
= item.height / 2,
button
= Qt.LeftButton,
modifiers
= Qt.NoModifier,
delay
= -1)
|
Simulates the full sequence of events generated by double-clicking a mouse
button
with optional
modifiers
on an
item
.
This method reproduces the sequence of mouse events generated when a user makes a double click: Press-Release-Press-DoubleClick-Release.
The position of the click is defined by
x
and
y
。若
x
and
y
are not defined the position will be the center of
item
。若
delay
is specified, the test will wait for the specified amount of milliseconds before pressing and before releasing the button.
The position given by
x
and
y
is transformed from the co-ordinate system of
item
into window co-ordinates and then delivered. If
item
is obscured by another item, or a child of
item
occupies that position, then the event will be delivered to the other item instead.
This QML method was introduced in Qt 5.5.
另请参阅
mousePress
(),
mouseRelease
(),
mouseClick
(),
mouseMove
(),
mouseDrag
(),和
mouseWheel
().
mouseDrag
(
item
,
x
,
y
,
dx
,
dy
,
button
= Qt.LeftButton,
modifiers
= Qt.NoModifier,
delay
= -1)
|
Simulates dragging the mouse on an
item
with
button
pressed and optional
modifiers
The initial drag position is defined by
x
and
y
, and drag distance is defined by
dx
and
dy
。若
delay
is specified, the test will wait for the specified amount of milliseconds before releasing the button.
The position given by
x
and
y
is transformed from the co-ordinate system of
item
into window co-ordinates and then delivered. If
item
is obscured by another item, or a child of
item
occupies that position, then the event will be delivered to the other item instead.
另请参阅
mousePress
(),
mouseClick
(),
mouseDoubleClickSequence
(),
mouseMove
(),
mouseRelease
(),和
mouseWheel
().
mouseMove
(
item
,
x
= item.width / 2,
y
= item.height / 2,
delay
= -1,
buttons
= Qt.NoButton)
|
Moves the mouse pointer to the position given by
x
and
y
在
item
, while holding
buttons
if given. Since Qt 6.0, if
x
and
y
are not defined, the position will be the center of
item
.
若
delay
(in milliseconds) is given, the test will wait before moving the mouse pointer.
The position given by
x
and
y
is transformed from the co-ordinate system of
item
into window co-ordinates and then delivered. If
item
is obscured by another item, or a child of
item
occupies that position, then the event will be delivered to the other item instead.
另请参阅
mousePress
(),
mouseRelease
(),
mouseClick
(),
mouseDoubleClickSequence
(),
mouseDrag
(),和
mouseWheel
().
mousePress
(
item
,
x
= item.width / 2,
y
= item.height / 2,
button
= Qt.LeftButton,
modifiers
= Qt.NoModifier,
delay
= -1)
|
Simulates pressing a mouse
button
with optional
modifiers
on an
item
. The position is defined by
x
and
y
。若
x
or
y
are not defined the position will be the center of
item
。若
delay
is specified, the test will wait for the specified amount of milliseconds before the press.
The position given by
x
and
y
is transformed from the co-ordinate system of
item
into window co-ordinates and then delivered. If
item
is obscured by another item, or a child of
item
occupies that position, then the event will be delivered to the other item instead.
另请参阅
mouseRelease
(),
mouseClick
(),
mouseDoubleClickSequence
(),
mouseMove
(),
mouseDrag
(),和
mouseWheel
().
mouseRelease
(
item
,
x
= item.width / 2,
y
= item.height / 2,
button
= Qt.LeftButton,
modifiers
= Qt.NoModifier,
delay
= -1)
|
Simulates releasing a mouse
button
with optional
modifiers
on an
item
. The position of the release is defined by
x
and
y
。若
x
or
y
are not defined the position will be the center of
item
。若
delay
is specified, the test will wait for the specified amount of milliseconds before releasing the button.
The position given by
x
and
y
is transformed from the co-ordinate system of
item
into window co-ordinates and then delivered. If
item
is obscured by another item, or a child of
item
occupies that position, then the event will be delivered to the other item instead.
另请参阅
mousePress
(),
mouseClick
(),
mouseDoubleClickSequence
(),
mouseMove
(),
mouseDrag
(),和
mouseWheel
().
mouseWheel
(
item
,
x
,
y
,
xDelta
,
yDelta
,
button
= Qt.LeftButton,
modifiers
= Qt.NoModifier,
delay
= -1)
|
Simulates rotating the mouse wheel on an
item
with
button
pressed and optional
modifiers
. The position of the wheel event is defined by
x
and
y
。若
delay
is specified, the test will wait for the specified amount of milliseconds before releasing the button.
The position given by
x
and
y
is transformed from the co-ordinate system of
item
into window co-ordinates and then delivered. If
item
is obscured by another item, or a child of
item
occupies that position, then the event will be delivered to the other item instead.
The
xDelta
and
yDelta
contain the wheel rotation distance in eighths of a degree. see
QWheelEvent::angleDelta
() 了解更多细节。
另请参阅
mousePress
(),
mouseClick
(),
mouseDoubleClickSequence
(),
mouseMove
(),
mouseRelease
(),
mouseDrag
(),和
QWheelEvent::angleDelta
().
Skips the current test case and prints the optional
message
. If this is a data-driven test, then only the current row is skipped. Similar to
QSKIP(message)
in C++.
Sleeps for
ms
milliseconds without processing Qt events.
另请参阅
wait
() 和
waitForRendering
().
Begins a sequence of touch events through a simulated touchscreen (
QPointingDevice
). Events are delivered to the window containing
item
.
The returned object is used to enumerate events to be delivered through a single
QTouchEvent
. Touches are delivered to the window containing the
TestCase
unless otherwise specified.
Rectangle {
width: 640; height: 480
MultiPointTouchArea {
id: area
anchors.fill: parent
property bool touched: false
onPressed: touched = true
}
TestCase {
name: "ItemTests"
when: windowShown
id: test1
function test_touch() {
let touch = touchEvent(area);
touch.press(0, area, 10, 10);
touch.commit();
verify(area.touched);
}
}
}
另请参阅
TouchEventSequence::press
(),
TouchEventSequence::move
(),
TouchEventSequence::release
(),
TouchEventSequence::stationary
(),
TouchEventSequence::commit
(),和
QInputDevice::DeviceType
.
tryCompare
(
obj
,
property
,
expected
,
timeout
= 5000,
message
= "")
|
Fails the current test case if the specified
property
on
obj
is not the same as
expected
, and displays the optional
message
. The test will be retried multiple times until the
timeout
(in milliseconds) is reached.
This function is intended for testing applications where a property changes value based on asynchronous events. Use
compare
() for testing synchronous property changes.
tryCompare(img, "status", BorderImage.Ready)
compare(img.width, 120)
compare(img.height, 120)
compare(img.horizontalTileMode, BorderImage.Stretch)
compare(img.verticalTileMode, BorderImage.Stretch)
SignalSpy::wait
() provides an alternative method to wait for a signal to be emitted.
另请参阅
compare
() 和
SignalSpy::wait
().
tryVerify
(
function
,
timeout
= 5000,
message
= "")
|
Fails the current test case if
function
does not evaluate to
true
before the specified
timeout
(in milliseconds) has elapsed. The function is evaluated multiple times until the timeout is reached. An optional
message
is displayed upon failure.
This function is intended for testing applications where a condition changes based on asynchronous events. Use
verify
() for testing synchronous condition changes, and
tryCompare
() for testing asynchronous property changes.
For example, in the code below, it's not possible to use
tryCompare
(), because the
currentItem
property might be
null
for a short period of time:
tryCompare(listView.currentItem, "text", "Hello");
Instead, we can use tryVerify() to first check that
currentItem
isn't
null
, and then use a regular compare afterwards:
tryVerify(function(){ return listView.currentItem })
compare(listView.currentItem.text, "Hello")
另请参阅
verify
(),
compare
(),
tryCompare
(),和
SignalSpy::wait
().
verify
(
条件
,
message
= "")
|
Fails the current test case if
条件
is false, and displays the optional
message
. Similar to
QVERIFY(condition)
or
QVERIFY2(condition, message)
in C++.
等待
ms
milliseconds while processing Qt events.
注意:
This methods uses a precise timer to do the actual waiting. The event you are waiting for may not. In particular, any animations as well as the
Timer
QML type can use either precise or coarse timers, depending on various factors. For a coarse timer you have to expect a drift of around 5% in relation to the precise timer used by TestCase::wait(). Qt cannot give hard guarantees on the drift, though, because the operating system usually doesn't offer hard guarantees on timers.
另请参阅
sleep
(),
waitForRendering
(),和
Qt::TimerType
.
bool
waitForPolish
(
对象
windowOrItem
,
int
timeout
= 5000)
|
若
windowOrItem
is an Item, this functions waits for
timeout
milliseconds or until
isPolishScheduled(windowOrItem)
返回
false
。返回
true
if
isPolishScheduled(windowOrItem)
返回
false
在
timeout
milliseconds, otherwise returns
false
.
若
windowOrItem
is a Window, this functions waits for
timeout
milliseconds or until
isPolishScheduled()
返回
false
for all items managed by the window. Returns
true
if
isPolishScheduled()
返回
false
for all items within
timeout
milliseconds, otherwise returns
false
.
This method was introduced in Qt 6.5.
另请参阅
isPolishScheduled
(),
QQuickItem::polish
(),和
QQuickItem::updatePolish
().
waitForRendering
(
item
,
timeout
= 5000)
|
等待
timeout
milliseconds or until the
item
is rendered by the renderer. Returns true if
item
is rendered in
timeout
milliseconds, otherwise returns false. The default
timeout
value is 5000.
另请参阅
sleep
() 和
wait
().
Prints
message
as a warning message. Similar to
qWarning(message)
in C++.
另请参阅
ignoreWarning
().