Main Content

App 测试框架概述

使用 MATLAB® App 测试框架来测试通过 App 设计工具创建的 App,或测试使用 uifigure 函数以编程方式构建的 App。您可以使用 App 测试框架来编写测试类,这些测试类以编程方式对 UI 组件执行手势(如按下按钮或拖动滑块)操作并验证 App 的行为。

App 测试

测试创建 - 基于类的测试可以通过子类化 matlab.uitest.TestCase 来使用 App 测试框架。matlab.uitest.TestCasematlab.unittest.TestCase 的一个子类,因此您的测试可以使用单元测试框架的功能,例如鉴定、脚手架和插件。要在命令提示符下试用 App 测试框架,请使用 matlab.uitest.TestCase.forInteractiveUse 创建一个测试用例实例。

测试内容 - 通常,App 的测试以编程方式使用 matlab.uitest.TestCase 的某一手势方法(如 presstype)与 App 组件进行交互,然后对结果执行鉴定。例如,测试可能会按下一个复选框并验证其他复选框已禁用。或者,它可能会在文本框中键入数字,并验证 App 是否计算出预期的结果。这些类型的测试要求您对被测 App 的属性有一定了解。要验证按钮按下操作,您必须知道 MATLAB 将按钮的状态存储在 App 对象中的哪个位置。要验证计算结果,您必须知道如何在 App 中访问结果。

测试清理 - 最好包含用于在测试后删除 App 的拆解操作。通常,测试方法使用 matlab.unittest.TestCaseaddTeardown 方法添加此操作。

App 锁定 - 当 App 测试创建图窗时,框架会立即锁定图窗以防止与组件的外部交互。如果您在命令提示符下创建用于试验的 matlab.uitest.TestCase.forInteractiveUse 实例,则 App 测试框架不会锁定 UI 组件。

要解锁图窗以进行调试,请使用 matlab.uitest.unlock 函数。

忽略警报 - 在某些情况下,App 会显示模态警报对话框,这使得与 App 组件进行交互变得无法进行。访问对话框后面的图窗可能需要您关闭对话框。要以编程方式关闭图窗窗口中的警报对话框,请使用 dismissAlertDialog 方法。

UI 组件的手势支持

matlab.uitest.TestCase 的手势方法支持各种 UI 组件。

组件典型的创建函数matlab.uitest.TestCase 手势方法
presschoosedragscrolltypehoverchooseContextMenu
坐标区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":[]}

在当前文件夹中名为 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.

另请参阅

相关主题