Contenuto principale

Generate Equivalence Tests for MATLAB Functions

R2026b
Since R2025a

You 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 quadraticSolver function

  • Executes the Python package

  • Verifies that the execution of the Python package matches the execution of the quadraticSolver function by using the verifyExecutionMatchesMATLAB method

% 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
You can also customize the test by generating target code with additional options or by adding tolerances. For more information, see Generate C/C++ Code and Test for Equivalence and Generate Deployed Code Artifacts and Test for Equivalence.

Run Tests

You can run the tests by using the:

For more information, see Run MATLAB Tests.

See Also

Apps

Classes

Topics