MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Write Test for App


This example shows how to write tests for an App Designer app. To interact with the app
programmatically and qualify the results, use the app testing framework and the unit
testing framework.

To explore the properties of this app prior to testing, create an instance of the app at the
command prompt.

app = PatientsDisplay;

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.BloodPressureSwitch to access the Blood
Pressure switch within the app object.

Create a test class that inherits from matlab.uitest.TestCase. To test the tab
switching functionality, create a test method test_tab. The test method chooses the
Data tab and then verifies that the selected tab has the correct title. The
TestMethodSetup method creates an app for each test and deletes it after the test is
complete.
classdef TestPatientsDisplay < matlab.uitest.TestCase
properties
App
end
methods (TestMethodSetup)
function launchApp(testCase)
testCase.App = PatientsDisplay;
testCase.addTeardown(@delete,testCase.App);
end
end
methods (Test)
function test_tab(testCase)
% Choose Data Tab
dataTab = testCase.App.Tab2;
testCase.choose(dataTab)
% Verify Data Tab is selected
testCase.verifyEqual(testCase.App.TabGroup.SelectedTab.Title,'Data')
end
end
end

Create a test_plottingOptions method that tests various plotting options. The test
method presses the Histogram radio button and verifies that the x-label changed. Then,
it changes the Bin Width slider and verifies the number of bins.
classdef TestPatientsDisplay < matlab.uitest.TestCase
properties

Write Test for App
Free download pdf