matlabtest.baselines.MATFileBaseline.load - Load baseline data into workspace - MATLAB (original) (raw)
Main Content
Class: matlabtest.baselines.MATFileBaseline
Namespace: matlabtest.baselines
Load baseline data into workspace
Since R2024b
Syntax
Description
data = load([baseline](#mw%5F8ef5bd07-e728-4221-8ee3-8c2fd27fcdf4))
loads the baseline data from the MAT file specified by baseline
into the MATLABĀ® workspace. The method returns the baseline data as a value of any data type.
You can inspect the loaded data to debug test failures. For more information about baseline tests, see Create Baseline Tests for MATLAB Code.
Input Arguments
Representation of the baseline data, specified as amatlabtest.baselines.MATFileBaseline
object.
Examples
Use the load
method to load baseline data when running a baseline test in debug mode. For more information on debugging MATLAB files, see Debug MATLAB Code Files.
In your current folder, create a MAT file named testdata.mat
that contains the expected output of the magic
function when called with5
as its input.
M = magic(5); save("testdata.mat","M")
In a file named ExampleTest.m
in your current folder, create theExampleTest
test class with a baseline test that compares a value against the baseline data in testdata.mat
. If you run this test class, it fails due to an intentional error.
classdef ExampleTest < matlab.unittest.TestCase
properties (TestParameter)
baseline = matlabtest.parameters.matfileBaseline("testdata.mat")
end
methods (Test)
function baselineTest(testCase,baseline)
actual = magic(6); % Intentional error
testCase.verifyEqualsBaseline(actual,baseline)
end
end
end
In the Editor, add a breakpoint at the line that includes the verification. Then, run the test, for instance, by executing runtests("ExampleTest")
in the Command Window. MATLAB enters debug mode and enables you to inspect the variables in the test.
Compare the actual value against the baseline data. To access the baseline data from the baseline
variable in the test, use the load
method. The comparison indicates a discrepancy between the actual and baseline matrix orders.
K>> actual
actual =
35 1 6 26 19 24
3 32 7 21 23 25
31 9 2 22 27 20
8 28 33 17 10 15
30 5 34 12 14 16
4 36 29 13 18 11
K>> data = load(baseline)
data =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
If you run the verification in the Command Window with a valid actual value, the test passes.
K>> actual = magic(5); K>> testCase.verifyEqualsBaseline(actual,baseline)
Exit debug mode and clear the breakpoint. Update the actual value in the test file, and save the file. If you run the test class, the baseline test passes.
result = runtests("ExampleTest");
Running ExampleTest . Done ExampleTest
Version History
Introduced in R2024b