Generate Equivalence Tests for MATLAB Functions
R2026bYou can create equivalence tests for the current function file in the MATLAB® Editor. The created tests build target code for the current function, execute the target code,and verify that the execution of the target code matches the execution of the MATLAB function. Complete the tests by specifying the function input values. For more information about equivalence tests, see Generate C/C++ Code and Test for Equivalence and Generate Deployed Code Artifacts and Test for Equivalence.
MATLAB Test™ uses static analysis to create equivalence tests for these targets:
C code
C++ code
Python® packages
Java® packages
.NET assemblies
Deployable archives
Deployable C++ shared libraries
Create Tests
To create an equivalence test, open a function file in MATLAB. Then, in the Editor or Live Editor tab, in the Test section, click Generate Test and, under Equivalence Tests, select your desired target language.
Tip
Selecting Generate Test > C/C++ Code Generation creates a C equivalence test. To specify the target language as C++,
set the value of the CodeGenerationArguments argument of the build
method to {"-lang:c++"}. For more information, see Specify Target Language as C++.
For example, suppose that you have a function called
quadraticSolver.
function r = quadraticSolver(a,b,c) % quadraticSolver returns solutions to the % quadratic equation a*x^2 + b*x + c = 0. r = sort([(-b + sqrt(b^2 - 4*a*c)) / (2*a); ... (-b - sqrt(b^2 - 4*a*c)) / (2*a)]); end
Generating an equivalence test for this function by selecting Generate Test > Python Package creates this test, which:
Generates a Python package from the
quadraticSolverfunctionExecutes the Python package
Verifies that the execution of the Python package matches the execution of the
quadraticSolverfunction by using theverifyExecutionMatchesMATLABmethod
% This is an autogenerated sample test for quadraticSolver classdef quadraticSolverCompilerEquivalenceTest < matlabtest.compiler.TestCase methods (Test) function testQuadraticSolverCompilerEquivalence(testCase) functionToBuild = @quadraticSolver; compilerTarget = "pythonPackage"; % Specify the inputs a = b = c = inputArgs = {a, b, c}; % Build artifact buildResults = testCase.build(functionToBuild, compilerTarget); % Execute artifact executionResults = testCase.execute(buildResults, inputArgs); % Execute MATLAB code and compare testCase.verifyExecutionMatchesMATLAB(executionResults) end end end
Complete Tests
To avoid invalid expressions in the test, specify the function input values.
For example, you can complete the
testQuadraticSolverCompilerEquivalence test by specifying values for
the function input values a, b, and
c.
% This is an autogenerated sample test for quadraticSolver classdef quadraticSolverCompilerEquivalenceTest < matlabtest.compiler.TestCase methods (Test) function testQuadraticSolverCompilerEquivalence(testCase) functionToBuild = @quadraticSolver; compilerTarget = "pythonPackage"; % Specify the inputs a = 2; b = 2; c = -4; inputArgs = {a, b, c}; % Build artifact buildResults = testCase.build(functionToBuild, compilerTarget); % Execute artifact executionResults = testCase.execute(buildResults, inputArgs); % Execute MATLAB code and compare testCase.verifyExecutionMatchesMATLAB(executionResults) end end end
Run Tests
You can run the tests by using the:
runtestsfunction at the MATLAB command lineRun Tests button on the Editor tab of the MATLAB Editor toolstrip
Run button
in the Test
BrowserRun button
in the MATLAB Test
ManagerRun button
in the Code Quality
Dashboard
For more information, see Run MATLAB Tests.