Write Test That Uses App Testing and Mocking
Frameworks
This example shows how to write a test that uses the app testing framework and the
mocking framework. The app contains a file selection dialog box and a label indicating the
selected file. To test the app programmatically, use a mock object to define the behavior of
the file selector.
Create App
Create the launchApp app in your current working folder. The app allows a user to select
an input file and displays the name of the file in the app. The file selection dialog box is a
blocking modal dialog box that waits for user input.
function app = launchApp
f = uifigure;
button = uibutton(f,'Text','Input file');
button.ButtonPushedFcn = @(src,evt)pickFile;
label = uilabel(f,'Text','No file selected');
label.Position(1) = button.Position(1) + button.Position(3) + 25;
label.Position(3) = 200;
% Add components to an App struct for output
app.UIFigure = f;
app.Button = button;
app.Label = label;
function file = pickFile()
[file,folder,status] = uigetfile('*.*');
if status
label.Text = file;
end
end
end
To explore the properties of this app prior to testing, create an instance of the app at the
command prompt. This step is not necessary for the tests, but it is helpful to explore the
properties used by the app tests. For example, use app.Button to access the Input file
button within the app object.
app = launchApp;
33 Unit Testing