Main Content

assertExecutionMatchesMATLAB

Class: matlabtest.compiler.TestCase
Namespace: matlabtest.compiler

Assert that deployed code artifact execution results match MATLAB results

Since R2023a

Description

assertExecutionMatchesMATLAB(testCase,executionResults) asserts that the execution results specified by executionResults for the deployed code artifact generated by MATLAB® Compiler SDK™ match the execution of the MATLAB source code in the equivalence test case testCase.

example

assertExecutionMatchesMATLAB(testCase,executionResults,diagnostic) returns diagnostic information specified by diagnostic.

example

assertExecutionMatchesMATLAB(___,Name=Value) specifies options using one or more name-value arguments in addition to the input arguments in previous syntaxes.

example

Input Arguments

expand all

Test case, specified as a matlabtest.compiler.TestCase object.

Execution results, specified as a matlabtest.compiler.results.ExecutionResults object.

Failure diagnostic information, specified as a:

Example: verifyExecutionMatchesMATLAB(testCase,executionResults,"Equivalence test failed")

Name-Value Arguments

Specify optional pairs of arguments as Name1=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: AbsTol=0.01

Absolute tolerance, specified as a numeric array. The sizes of AbsTol and expected, where expected is the output of the MATLAB execution of the function, must be the same or compatible. See Compatible Array Sizes for Basic Operations for more information about compatible arrays.

The tolerance is applied only to values of the same data type. For an absolute tolerance to be satisfied, abs(expected-actual) <= AbsTol must be true, where actual is ExecutableOutput.

Relative tolerance, specified as a numeric array. The sizes of RelTol and expected, where expected is the output of the MATLAB execution of the function, must be the same or compatible. See Compatible Array Sizes for Basic Operations for more information about compatible arrays.

The tolerance is applied only to values of the same data type. For a relative tolerance to be satisfied, abs(expected-actual) <= RelTol.*abs(expected) must be true, where actual is ExecutableOutput.

Examples

expand all

This example shows how to generate a Python® package from MATLAB source code and test for equivalence by using assertions.

Suppose that you want to generate a Python package for a function called makesquare, which generates an n-by-n matrix:

function y = makesquare(x)
y = magic(x);
end

This class definition file defines an equivalence test case that inherits from matlabtest.compiler.TestCase. The test case in the methods block defines a test case that:

  1. Builds the Python package from the makesquare function

  2. Executes the Python package with input set to 5

  3. Asserts that the execution of the Python package matches the execution of the MATLAB function makesquare with the same input

classdef tDeployment < matlabtest.compiler.TestCase
    methods(Test)
        function pythonEquivalence(testCase)
            buildResults = build(testCase,"makesquare.m", ...
                "pythonPackage");
            executionResults = execute(testCase,buildResults,{5});
            assertExecutionMatchesMATLAB(testCase,executionResults);
        end
    end
end

Run the pythonEquivalence test.

runtests("tDeployment", ...
    ProcedureName="pythonEquivalence")
Running pythonEquivalence
..
Done pythonEquivalence
__________


ans = 

  TestResult with properties:

          Name: 'tDeployment/pythonEquivalence'
        Passed: 1
        Failed: 0
    Incomplete: 0
      Duration: 93.1237
       Details: [1×1 struct]

Totals:
   1 Passed, 0 Failed, 0 Incomplete.
   93.1237 seconds testing time.

This example shows how to generate a Python package from MATLAB source code and test for equivalence by using assertions, and return a custom diagnostic result.

Suppose that you want to generate a Python package for a function called makesquare, which generates an n-by-n matrix:

function y = makesquare(x)
y = magic(x);
end

This class definition file defines an equivalence test case that inherits from matlabtest.compiler.TestCase. The test case in the methods block defines a test case that:

  1. Builds the Python package from the makesquare function

  2. Executes the Python package with input set to 5

  3. Asserts that the execution of the Python package matches the execution of the MATLAB function makesquare with the same input and returns a string if the test fails

classdef tDeployment < matlabtest.compiler.TestCase
    methods(Test)
        function pythonEquivalence(testCase)
            buildResults = build(testCase,"makesquare.m", ...
                "pythonPackage");
            executionResults = execute(testCase,buildResults,{5});
            d = "Equivalence test failed.";
            assertExecutionMatchesMATLAB(testCase,executionResults,d);
        end
    end
end

Run the pythonEquivalence test.

runtests("tDeployment", ...
    ProcedureName="pythonEquivalence")
Running pythonEquivalence
..
Done pythonEquivalence
__________


ans = 

  TestResult with properties:

          Name: 'tDeployment/pythonEquivalence'
        Passed: 1
        Failed: 0
    Incomplete: 0
      Duration: 93.1237
       Details: [1×1 struct]

Totals:
   1 Passed, 0 Failed, 0 Incomplete.
   93.1237 seconds testing time.

This example shows how to generate a Python package from MATLAB source code and test for equivalence by using assertions with tolerances.

Suppose that you want to generate a Python package for a function called makesquare, which generates an n-by-n matrix:

function y = makesquare(x)
y = magic(x);
end

This class definition file defines an equivalence test case that inherits from matlabtest.compiler.TestCase. The test case in the methods block defines a test case that:

  1. Builds the Python package from the makesquare function

  2. Executes the Python package with input set to 5

  3. Asserts that the execution of the Python package matches the execution of the MATLAB function makesquare with the same input within an absolute tolerance of 0.001

classdef tDeployment < matlabtest.compiler.TestCase
    methods(Test)
        function pythonEquivalence(testCase)
            buildResults = build(testCase,"makesquare.m", ...
                "pythonPackage");
            executionResults = execute(testCase,buildResults,{5});
            assertExecutionMatchesMATLAB(testCase,executionResults, ...
                AbsTol=0.001);
        end
    end
end

Run the pythonEquivalence test.

runtests("tDeployment", ...
    ProcedureName="pythonEquivalence")
Running pythonEquivalence
..
Done pythonEquivalence
__________


ans = 

  TestResult with properties:

          Name: 'tDeployment/pythonEquivalence'
        Passed: 1
        Failed: 0
    Incomplete: 0
      Duration: 93.1237
       Details: [1×1 struct]

Totals:
   1 Passed, 0 Failed, 0 Incomplete.
   93.1237 seconds testing time.

Limitations

  • You cannot generate deployed code artifacts or test them for equivalence in MATLAB Online™.

Version History

Introduced in R2023a