assertExecutionMatchesMATLAB
Class: matlabtest.compiler.TestCase
Namespace: matlabtest.compiler
Assert that deployed code artifact execution results match MATLAB results
Since R2023a
Syntax
Description
assertExecutionMatchesMATLAB(
asserts that the execution results specified by testCase
,executionResults
)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
.
assertExecutionMatchesMATLAB(
returns diagnostic information specified by testCase
,executionResults
,diagnostic
)diagnostic
.
assertExecutionMatchesMATLAB(___,
specifies options using one or more name-value arguments in addition to the input arguments
in previous syntaxes.Name=Value
)
Input Arguments
testCase
— Test case
matlabtest.compiler.TestCase
object
Test case, specified as a matlabtest.compiler.TestCase
object.
executionResults
— Execution results
matlabtest.compiler.results.ExecutionResults
Execution results, specified as a matlabtest.compiler.results.ExecutionResults
object.
diagnostic
— Failure diagnostic information
string scalar | character vector | function handle | matlab.unittest.diagnostics.Diagnostic
object
Failure diagnostic information, specified as a:
String scalar or character vector
Function handle
matlab.automation.diagnostics.Diagnostic
object or object of one of its subclasses
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
AbsTol
— Absolute tolerance
numeric array
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
.
RelTol
— Relative tolerance
numeric array
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
Generate and Test Python Package for Equivalence by Using Assertions
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:
Builds the Python package from the
makesquare
functionExecutes the Python package with input set to
5
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.
Generate and Test Python Package for Equivalence by Using Assertions, and Return Custom Diagnostic Result
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:
Builds the Python package from the
makesquare
functionExecutes the Python package with input set to
5
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.
Generate and Test Python Package for Equivalence by Using Assertions with Tolerances
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:
Builds the Python package from the
makesquare
functionExecutes the Python package with input set to
5
Asserts that the execution of the Python package matches the execution of the MATLAB function
makesquare
with the same input within an absolute tolerance of0.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.
Version History
Introduced in R2023a
See Also
Classes
matlabtest.compiler.TestCase
|matlabtest.compiler.results.ExecutionResults
|matlab.unittest.qualifications.Assertable
Functions
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)