matlab.unittest.qualifications.Verifiable.verifyEqualsBaseline - Verify value is equal to baseline data - MATLAB (original) (raw)

Verify value is equal to baseline data

Since R2024b

Syntax

Description

verifyEqualsBaseline([testCase](#mw%5Fe9485355-206b-4904-8cb8-59d936ed9b82),[actual](#mw%5F6367a687-a8d8-4334-ab09-b504ed61741c),[baseline](#mw%5F61bb9bb7-6488-4ccc-b9a4-1742d7d5a533)) verifies that actual is strictly equal to the baseline data represented by baseline. The method compares values in the same way as the verifyEqual method.

You can use the verifyEqualsBaseline method within a test class that specifies baseline parameters. If the qualification fails, the testing framework provides you with options to either create new baseline data or update the existing baseline data using the actual value. For more information about baseline testing, see Create Baseline Tests for MATLAB Code.

example

verifyEqualsBaseline([testCase](#mw%5Fe9485355-206b-4904-8cb8-59d936ed9b82),[actual](#mw%5F6367a687-a8d8-4334-ab09-b504ed61741c),[baseline](#mw%5F61bb9bb7-6488-4ccc-b9a4-1742d7d5a533),[diagnostic](#mw%5F03604d83-fc9f-44b4-a8da-3c625317ba74)) associates the diagnostic information in diagnostic with the qualification.

verifyEqualsBaseline(___,[Name=Value](#namevaluepairarguments)) specifies options using one or more name-value arguments in addition to other input argument combinations in previous syntaxes. For example,verifyEqualsBaseline(testCase,actual,baseline,RelTol=0.01) verifies that the difference between the corresponding elements of the actual and baseline numeric arrays is within 1%.

Input Arguments

expand all

Value to test, specified as a value of any data type. To create or update the baseline data, the testing framework must be able to save and load actual. For more information about the save and load operations, see Default Save and Load Process for Objects.

Diagnostic information to display when the qualification passes or fails, specified as a string array, character array, function handle, or array of matlab.automation.diagnostics.Diagnostic objects.

Depending on the test runner configuration, the testing framework can display diagnostics when the qualification passes or fails. By default, the framework displays diagnostics only when the qualification fails. You can override the default behavior by customizing the test runner. For example, use a DiagnosticsOutputPlugin instance to display both failing and passing event diagnostics.

Example: "My Custom Diagnostic"

Example: @dir

Name-Value Arguments

expand all

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: verifyEqualsBaseline(testCase,actual,baseline,RelTol=0.01) verifies that the difference between the corresponding elements of the actual and baseline numeric arrays is within 1%.

Absolute tolerance when comparing numeric arrays in a baseline test, specified as a nonnegative real scalar. For an absolute tolerance to be satisfied by an actual array A and baseline array B, abs(B-A) <= AbsTol must be true.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Relative tolerance when comparing numeric arrays in a baseline test, specified as a nonnegative real scalar. For a relative tolerance to be satisfied by an actual arrayA and baseline array B, abs(B-A) <= RelTol.*abs(B) must be true.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Examples

expand all

Create a baseline test to test a function whose output is not straightforward to verify through automated testing. Run the test to create baseline data in a MAT file from the "known good" function output. Run the test a second time to test the function output against the created baseline data.

First, manually verify that your function behaves as expected. For example, you can inspect the function output in the Command Window. For illustrative purposes, this example uses the magic function. In practice, you work with user-defined code.

M =

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

To create a baseline test for your function, you must define a baseline and then associate it with a test using a test class. Create the ExampleTest test class in a file named ExampleTest.m in your current folder. Then, complete the test class code by following these steps:

  1. In a properties block with theTestParameter attribute, add thebaseline property to define a baseline.
  2. Set the baseline property using thematlabtest.parameters.matfileBaseline function. The function creates a baseline parameter that defines the data in thetestdata.mat file as baseline data.
  3. In a methods block with the Test attribute, add the parameterized baselineTest method that accepts baseline as an input.
  4. Implement the baselineTest method by specifying the actual value and then using the verifyEqualsBaseline method to test the actual value against the baseline data.

classdef ExampleTest < matlab.unittest.TestCase properties (TestParameter)
baseline = matlabtest.parameters.matfileBaseline("testdata.mat") end

methods (Test)
    function baselineTest(testCase,baseline)
        actual = magic(5);
        testCase.verifyEqualsBaseline(actual,baseline)
    end
end

end

Run the test. The test fails because the testdata.mat file does not yet exist and there is no baseline data to compare the actual value against. However, the test diagnostic includes a hyperlink that enables you to create baseline data from the actual value that the testing framework records.

result = runtests("ExampleTest");

Running ExampleTest

================================================================================ Verification failed in ExampleTest/baselineTest(baseline=testdata.mat(data)). --------------------- Framework Diagnostic: --------------------- verifyEqualsBaseline failed. --> An error occurred when loading baseline data: MATLAB:MatFile:NoFile Cannot access 'data' because 'C:\work\testdata.mat' does not exist. --> Create baseline from recorded test data ------------------ Stack Information: ------------------ ... In C:\work\ExampleTest.m (ExampleTest.baselineTest) at 9

. Done ExampleTest


Failure Summary:

 Name                                                 Failed  Incomplete  Reason(s)
==================================================================================================
 ExampleTest/baselineTest(baseline=testdata.mat(data))    X                 Failed by verification.

Because you already inspected the actual value before creating the baseline test, approve it as baseline data by clicking Create baseline from recorded test data. The framework creates the testdata.mat file including the actual value as baseline data and displays a dialog box to confirm the operation. Now that baseline data exists for your test, you can verify that your function continues to produce the same output by running the baseline test. If the function output changes, for instance, due to code refactoring, the baseline test signals the change through a qualification failure. The test passes only if the function output remains the same as the baseline data in the MAT file.

Run the test again to test the function output against the baseline data. In this case, the test passes because the function has not changed since baseline data creation.

result = runtests("ExampleTest");

Running ExampleTest . Done ExampleTest


Tips

Version History

Introduced in R2024b