methods(TestClassSetup)
function checkTestFiles(tc)
import matlab.unittest.constraints.IsFile
tc.assumeThat(tc.TestFile,IsFile)
end
end
methods (Test)
function testInputButton(tc)
app = launchApp;
tc.addTeardown(@close,app.UIFigure);
tc.press(app.Button);
tc.verifyEqual(app.Label.Text,tc.TestFile)
end
end
end
Run the test. When the file selection dialog box appears, select input2.txt to allow
MATLAB to proceed with the test. Selecting any other file results in a test failure.
results = runtests('LaunchAppTest');
Running LaunchAppTest
.
Done LaunchAppTest
__________
Create Fully Automated Test
To test the app without manual intervention, use the mocking framework. Modify the app
to accept a file-choosing service instead of implementing it in the app (dependency
injection).
Create a FileChooser service with an Abstract method that implements the file
selection functionality.
classdef FileChooser
% Interface to choose a file
methods (Abstract)
[file,folder,status] = chooseFile(chooser,varargin)
end
end
33 Unit Testing