Main Content

Overview of App Testing Framework

Use the MATLAB® app testing framework to test App Designer apps or apps built programmatically using the uifigure function. The app testing framework lets you author a test class that programmatically performs a gesture on a UI component, such as pressing a button or dragging a slider, and verifies the behavior of the app.

App Testing

Test Creation – Class-based tests can use the app testing framework by subclassing matlab.uitest.TestCase. Because matlab.uitest.TestCase is a subclass of matlab.unittest.TestCase, your test has access to the features of the unit testing framework, such as qualifications, fixtures, and plugins. To experiment with the app testing framework at the command prompt, create a test case instance using matlab.uitest.TestCase.forInteractiveUse.

Test Content – Typically, a test of an app programmatically interacts with app components using a gesture method of matlab.uitest.TestCase, such as press or type, and then performs a qualification on the result. For example, a test might press one check box and verify that the other check boxes are disabled. Or it might type a number into a text box and verify that the app computes the expected result. These types of tests require understanding of the properties of the app being tested. To verify a button press, you must know where in the app object MATLAB stores the status of a button. To verify the result of a computation, you must know how to access the result within the app.

Test Cleanup – It is a best practice to include a teardown action to delete the app after the test. Typically, the test method adds this action using the addTeardown method of matlab.unittest.TestCase.

App Locking – When an app test creates a figure, the framework locks the figure immediately to prevent external interactions with the components. The app testing framework does not lock UI components if you create an instance of matlab.uitest.TestCase.forInteractiveUse for experimentation at the command prompt.

To unlock a figure for debugging purposes, use the matlab.uitest.unlock function.

Dialog Box Interaction – Some apps display modal dialog boxes, preventing interaction with other app components. To access the figure behind a modal dialog box, you must first select an option in the dialog box or dismiss the dialog box. To programmatically interact with a dialog box in the figure window, use the chooseDialog or dismissDialog method.

Gesture Support of UI Components

The gesture methods of matlab.uitest.TestCase support various UI components.

ComponentTypical Creation Functionmatlab.uitest.TestCase Gesture Method
presschoosedragscrolltypehoverchooseContextMenu
Axesaxes  
Buttonuibutton     
Button Groupuibuttongroup      
Check Boxuicheckbox    
Date Pickeruidatepicker     
Discrete Knobuiknob     
Drop Downuidropdown    
Edit Field (Numeric, Text)uieditfield     
Hyperlinkuihyperlink     
Imageuiimage     
Knobuiknob    
Labeluilabel      
List Boxuilistbox     
Menuuimenu      
Paneluipanel    
Polar Axespolaraxes    
Push Tooluipushtool      
Radio Buttonuiradiobutton    
Slideruislider    
Spinneruispinner    
State Buttonuibutton    
Switch (Rocker, Slider, Toggle)uiswitch    
Tabuitab      
Tab Groupuitabgroup      
Tableuitable    
Text Areauitextarea     
Toggle Buttonuitogglebutton    
Toggle Tooluitoggletool     
Tree Nodeuitreenode     
UI Axesuiaxes  
UI Figureuifigure   

Example: Write a Test for an App

This example shows how to write a test for an app in your current folder. The app provides options to change the sample size and colormap of a plot. To programmatically interact with the app and qualify the results, use the app testing framework and the unit testing framework.

To explore the properties of the app prior to testing, create an instance of the app. This step is not necessary for the tests, but it is helpful to explore the properties used by the tests. For example, use app.UpdatePlotButton to access the Update Plot button within the 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":[]}

In a file named ConfigurePlotAppExampleTest.m in your current folder, create a test class that derives from matlab.uitest.TestCase. Add two Test methods to the ConfigurePlotAppExampleTest class. In each method, create an instance of the app before testing and delete it after the test is complete:

  • testSampleSize method — Modify the sample size, update the plot, and verify that the plot uses the specified sample size.

  • testColormap method — Select a colormap, update the plot, and verify that the plot uses the specified colormap.

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

Run the tests. In this example, both of the tests pass.

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.

See Also

Related Topics