App 测试框架概述
使用 MATLAB® App 测试框架来测试通过 App 设计工具创建的 App,或测试使用 uifigure 函数以编程方式构建的 App。您可以使用 App 测试框架来编写测试类,这些测试类以编程方式对 UI 组件执行手势(如按下按钮或拖动滑块)操作并验证 App 的行为。
App 测试
测试创建 - 基于类的测试可以通过子类化 matlab.uitest.TestCase 来使用 App 测试框架。matlab.uitest.TestCase 是 matlab.unittest.TestCase 的一个子类,因此您的测试可以使用单元测试框架的功能,例如鉴定、脚手架和插件。要在命令提示符下试用 App 测试框架,请使用 matlab.uitest.TestCase.forInteractiveUse 创建一个测试用例实例。
测试内容 - 通常,App 的测试以编程方式使用 matlab.uitest.TestCase 的某一手势方法(如 press 或 type)与 App 组件进行交互,然后对结果执行鉴定。例如,测试可能会按下一个复选框并验证其他复选框已禁用。或者,它可能会在文本框中键入数字,并验证 App 是否计算出预期的结果。这些类型的测试要求您对被测 App 的属性有一定了解。要验证按钮按下操作,您必须知道 MATLAB 将按钮的状态存储在 App 对象中的哪个位置。要验证计算结果,您必须知道如何在 App 中访问结果。
测试清理 - 最好包含用于在测试后删除 App 的拆解操作。通常,测试方法使用 matlab.unittest.TestCase 的 addTeardown 方法添加此操作。
App 锁定 - 当 App 测试创建图窗时,框架会立即锁定图窗以防止与组件的外部交互。如果您在命令提示符下创建用于试验的 matlab.uitest.TestCase.forInteractiveUse 实例,则 App 测试框架不会锁定 UI 组件。
要解锁图窗以进行调试,请使用 matlab.uitest.unlock 函数。
对话框交互 - 一些 App 显示模态对话框,从而阻止与其他 App 组件的交互。要访问模态对话框后面的图窗,您必须首先在对话框中选择选项或关闭对话框。要以编程方式与图窗窗口中的对话框进行交互,请使用 chooseDialog 或 dismissDialog 方法。
UI 组件的手势支持
matlab.uitest.TestCase 的手势方法支持各种 UI 组件。
| 组件 | 典型的创建函数 | matlab.uitest.TestCase 手势方法 | ||||||
press | choose | drag | scroll | type | hover | chooseContextMenu | ||
| 坐标区 | axes | ∍ | ∍ | ∍ | ∍ | ∍ | ||
| 按钮 | uibutton | ∍ | ∍ | |||||
| 按钮组 | uibuttongroup | ∍ | ||||||
| 复选框 | uicheckbox | ∍ | ∍ | ∍ | ||||
| 日期选择器 | uidatepicker | ∍ | ∍ | |||||
| 分档旋钮 | uiknob | ∍ | ∍ | |||||
| 下拉列表 | uidropdown | ∍ | ∍ | ∍ | ||||
| 编辑字段(数值、文本) | uieditfield | ∍ | ∍ | |||||
| 超链接 | uihyperlink | ∍ | ∍ | |||||
| 图像 | uiimage | ∍ | ∍ | |||||
| 旋钮 | uiknob | ∍ | ∍ | ∍ | ||||
| 标签 | uilabel | ∍ | ||||||
| 列表框 | uilistbox | ∍ | ∍ | |||||
| 菜单 | uimenu | ∍ | ||||||
| 面板 | uipanel | ∍ | ∍ | ∍ | ||||
| 极坐标区 | polaraxes | ∍ | ∍ | ∍ | ||||
| 按钮工具 | uipushtool | ∍ | ||||||
| 单选按钮 | uiradiobutton | ∍ | ∍ | ∍ | ||||
| 滑块 | uislider | ∍ | ∍ | ∍ | ||||
| 微调器 | uispinner | ∍ | ∍ | ∍ | ||||
| 状态按钮 | uibutton | ∍ | ∍ | ∍ | ||||
| 开关(跷板、滑块、拨动) | uiswitch | ∍ | ∍ | ∍ | ||||
| 制表符 | uitab | ∍ | ||||||
| 选项卡组 | uitabgroup | ∍ | ||||||
| 表 | uitable | ∍ | ∍ | ∍ | ||||
| 文本区域 | uitextarea | ∍ | ∍ | |||||
| 切换按钮 | uitogglebutton | ∍ | ∍ | ∍ | ||||
| 切换工具 | uitoggletool | ∍ | ∍ | |||||
| 树节点 | uitreenode | ∍ | ∍ | |||||
| UI 坐标区 | uiaxes | ∍ | ∍ | ∍ | ∍ | ∍ | ||
| UI 图窗 | uifigure | ∍ | ∍ | ∍ | ∍ | |||
示例:为 App 编写测试
此示例说明如何在您当前文件夹中为 App 写测试。该 App 提供用于更改绘图的样本大小和颜色图的选项。要以编程方式与 App 交互并验证结果,请使用单元测试框架和 App 测试框架。
要在测试之前了解该 App 的属性,请创建该 App 的实例。此步骤对于测试不是必需的,但是了解测试使用的属性会很有帮助。例如,使用 app.UpdatePlotButton 访问 App 对象内的更新绘图按钮。
app = ConfigurePlotAppExample;
![{"String":"Figure Configure Plot contains an axes object and other objects of type uilabel, uidropdown, uibutton, uinumericeditfield. The axes object contains an object of type surface.","Tex":[],"LaTex":[]}](../../examples/matlab/win64/WriteATestForAnAppExample_01.png)
在当前文件夹中名为 ConfigurePlotAppExampleTest.m 的文件中,创建一个从 matlab.uitest.TestCase 派生的测试类。将两个 Test 方法添加到 ConfigurePlotAppExampleTest 类。在每种方法中,在测试前创建一个 App 实例,并在测试完成后删除它:
testSampleSize方法 - 修改样本大小、更新绘图并验证绘图使用指定的样本大小。testColormap方法 - 选择一个颜色图、更新绘图并验证该绘图使用了指定的颜色图。
classdef ConfigurePlotAppExampleTest < matlab.uitest.TestCase methods (Test) function testSampleSize(testCase) app = ConfigurePlotAppExample; testCase.addTeardown(@delete,app) testCase.type(app.SampleSizeEditField,12) testCase.press(app.UpdatePlotButton) ax = app.UIAxes; surfaceObj = ax.Children; testCase.verifySize(surfaceObj.ZData,[12 12]) end function testColormap(testCase) app = ConfigurePlotAppExample; testCase.addTeardown(@delete,app) testCase.choose(app.ColormapDropDown,"Winter") testCase.press(app.UpdatePlotButton) expectedMap = winter; ax = app.UIAxes; testCase.verifyEqual(ax.Colormap,expectedMap) end end end
运行测试。在此示例中,两个测试都通过。
results = runtests("ConfigurePlotAppExampleTest")Running ConfigurePlotAppExampleTest
.
. Done ConfigurePlotAppExampleTest __________
results =
1×2 TestResult array with properties:
Name
Passed
Failed
Incomplete
Duration
Details
Totals:
2 Passed, 0 Failed, 0 Incomplete.
11.9555 seconds testing time.