Main Content

matlab.uitest.TestCase Class

Namespace: matlab.uitest
Superclasses: matlab.unittest.TestCase

Class for writing tests with app testing framework

Description

The matlab.uitest.TestCase class lets you write tests that use the app testing framework. Because the matlab.uitest.TestCase class derives from the matlab.unittest.TestCase class, your tests have access to the features of the unit testing framework, such as qualifications, fixtures, and plugins. For more information about app testing, see Overview of App Testing Framework.

The matlab.uitest.TestCase class is a handle class.

Class Attributes

Abstract
true

For information on class attributes, see Class Attributes.

Creation

In most cases, you are not required to create an instance of the matlab.uitest.TestCase class directly. The app testing framework automatically creates matlab.uitest.TestCase instances when running the tests.

To create a matlab.uitest.TestCase instance for interactive, command-line testing, use the forInteractiveUse static method.

Methods

expand all

Examples

collapse all

Create a discrete knob.

knob = uiknob('discrete');

A figure with a discrete knob. The knob value is 'Off'.

Create an interactive test case and choose the 'High' knob value. An animated blue dot performs the programmatic choose gesture.

tc = matlab.uitest.TestCase.forInteractiveUse;
tc.choose(knob,'High')

A figure with a discrete knob. The knob value is 'High'.

View the value of the Items property on the knob.

knob.Items
ans =

  1×4 cell array

    {'Off'}    {'Low'}    {'Medium'}    {'High'}

Choose the 'Low' knob value by index. The knob moves from 'High' to 'Low'.

tc.choose(knob,2)

A figure with a discrete knob. The knob value is 'Low'.

Create a state button.

b = uibutton('state');

Create an interactive test case and verify that the value of the state button is false.

tc = matlab.uitest.TestCase.forInteractiveUse;
tc.verifyFalse(b.Value)
Verification passed.

Press the button and verify the state changes to true. A blue dot representing the programmatic push gesture appears and then disappears on the button.

tc.press(b)
tc.verifyTrue(b.Value)
Verification passed.

Create a slider with a minimum value of -237, a maximum value of 237, and a starting value of 7.

slider = uislider("Limits",[-237 237],"Value",7);

Create an interactive test case and verify the initial value of the slider.

testCase = matlab.uitest.TestCase.forInteractiveUse;
testCase.verifyEqual(slider.Value,7)
Verification passed.

Drag the slider between two values and verify the final value. Since the framework mimics a user manipulating the component to an arbitrarily precisioned value, it is a best practice to use a tolerance to compare the actual and expected slider values.

val = 26.75;
testCase.drag(slider,-val,val)
testCase.verifyEqual(slider.Value,val,"AbsTol",0.1)
Verification passed.

Since R2023b

Perform a gesture on a component that is not in the viewable area of a figure by automatically scrolling to the component.

Create a scrollable figure with a state button that is outside the viewable area of the figure.

fig = uifigure("Scrollable","on");
b = uibutton(fig,"state","Position",[fig.Position(3)+100 100 100 22]);

Create a test case for interactive testing, and verify the initial state of the button.

tc = matlab.uitest.TestCase.forInteractiveUse;
tc.verifyFalse(b.Value)
Verification passed.

Press the button and verify that its state changes. Because the figure is scrollable, the app testing framework automatically scrolls to the button before performing the gesture.

tc.press(b)
tc.verifyTrue(b.Value)
Verification passed.

Version History

Introduced in R2018a

expand all