Generate Tests for MATLAB Source Code
You can use MATLAB® Test™ to generate tests that exercise your MATLAB source code. You can also generate tests that generate target code from a MATLAB function and test it for equivalence. This table summarizes the different methods.
Task | Benefits | Create Tests From | Specifying Input and Expected Output Values |
---|---|---|---|
Generate basic unit test for current file |
| Currently open script, function, or class file in the MATLAB Editor |
|
Generate unit test for the current file using MATLAB Copilot |
| Currently open script, function, or class file in the MATLAB Editor |
|
Generate unit test from commands that you executed in the MATLAB Command Window |
| Commands in the Command History panel |
|
Generate basic equivalence test for the current file |
| Currently open function file in the MATLAB Editor |
|
Generate Basic Unit Tests for Current File
You can generate basic unit tests for the current source code file. The generated unit
tests execute the source code and test the output by using the verifyEqual
method. Complete the tests by specifying the expected output
of the source code in the test, and if necessary, by
specifying the input values. For more information about building on the generated code,
see Create Basic Tests for MATLAB Source Code.
Generate Tests
To generate a test, open a source code file in MATLAB. Then, in the Editor or Live Editor tab, in the Test section, click Generate Test > Generate Test by using static analysis, or click Generate Test. The generated test opens.
Note
Alternatively, you can generate the tests by right-clicking in the Editor and selecting Create Test (since R2024a).
For example, suppose that you have a function called
quadraticSolver
. The function takes as inputs the
coefficients of a quadratic polynomial and returns the roots of that
polynomial.
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
The generated test for this function calls the
quadraticSolver
function to produce an output and uses the
verifyEqual
qualification method to
test the produced output.
% This is an autogenerated sample test for file quadraticSolver.m classdef testquadraticSolver < matlab.unittest.TestCase methods (Test) function test_quadraticSolver(testCase) % Specify the input(s) of quadraticSolver a = b = c = % Specify the expected output(s) of quadraticSolver expected_r = % Exercise the function quadraticSolver actual_r = quadraticSolver(a, b, c); testCase.verifyEqual(actual_r, expected_r); end end end
Complete Tests
To avoid invalid expressions in the test, specify the function or method input values and the expected output of the source code file.
Note
If the function or methods use an arguments
block for
input argument validation, MATLAB
Test attempts to generate input argument values for a function or
class method. For more information, see Create Basic Tests for MATLAB Source Code.
For example, you can complete the test_quadraticSolver
test by
specifying values for the inputs a
, b
, and
c
and by specifying values for the expected output,
expected_r
.
% This is an autogenerated sample test for file quadraticSolver.m classdef testquadraticSolver < matlab.unittest.TestCase methods (Test) function test_quadraticSolver(testCase) % Specify the input(s) of quadraticSolver a = 2; b = 2; c = -4; % Specify the expected output(s) of quadraticSolver expected_r = sort(roots([a b c])); % Exercise the function quadraticSolver actual_r = quadraticSolver(a, b, c); testCase.verifyEqual(actual_r, expected_r); end end end
Generate Unit Tests for Current File Using MATLAB Copilot
If you have a MATLAB Copilot license, you can generate unit tests for a source code file using MATLAB Copilot. The generated tests execute the source code and test the output by using qualification methods. For more information about qualification methods, see Table of Verifications, Assertions, and Other Qualifications. You do not need to specify input or output values to run the tests.
Tip
MATLAB Copilot attempts to generate test code that qualifies the source code. However, the generated tests might contain errors or incorrect code. Validate the generated tests before use, and ensure that the tests exercise the functional behavior that you want to test. You can use MATLAB Copilot to explain the generated tests by selecting the test code in the Editor and, on the Editor tab, selecting Copilot > Explain Code.
To generate a test, open a source code file in MATLAB. Then, on the Editor or Live Editor tab, in the Test section, select Generate Test > Generate test for the current file using Copilot. The test file that opens can contain one or more tests. You do not need to specify input or expected output values in the test.
For example, suppose that you have a function named
quadraticSolver
.
function r = quadraticSolver(a,b,c) % quadraticSolver returns solutions to the % quadratic equation a*x^2 + b*x + c = 0. arguments a (1,1) {mustBeNumeric,mustBeNonzero} b (1,1) {mustBeNumeric} c (1,1) {mustBeNumeric} end r = sort([(-b + sqrt(b^2 - 4*a*c)) / (2*a); ... (-b - sqrt(b^2 - 4*a*c)) / (2*a)]); end
Then, generate tests for the function using MATLAB Copilot. Save the test file.
% This test file was generated by Copilot. Validate the generated output before use. classdef TestQuadraticSolver < matlab.unittest.TestCase methods(Test) function testRealRoots(testCase) % Test case with real roots a = 1; b = -3; c = 2; % (x-1)(x-2) = 0 expected = [1; 2]; actual = quadraticSolver(a,b,c); testCase.verifyEqual(actual,expected,"AbsTol",1e-10); end function testComplexRoots(testCase) % Test case with complex roots a = 1; b = 2; c = 5; % x^2 + 2x + 5 = 0 expected = [-1 + 2i; -1 - 2i]; actual = quadraticSolver(a,b,c); testCase.verifyEqual(actual,expected,"AbsTol",1e-10); end function testDoubleRoot(testCase) % Test case with a double root a = 1; b = 2; c = 1; % (x+1)^2 = 0 expected = [-1; -1]; actual = quadraticSolver(a,b,c); testCase.verifyEqual(actual,expected,"AbsTol",1e-10); end function testInvalidInput(testCase) % Test case for invalid input (non-numeric) testCase.verifyError(@() quadraticSolver("a",2,1), ... "MATLAB:validators:mustBeNumeric"); end function testZeroCoefficientA(testCase) % Test case for zero coefficient a testCase.verifyError(@() quadraticSolver(0,2,1), ... "MATLAB:validators:mustBeNonzero"); end end end
To validate the generated tests and test the source code, run the tests and review the results.
For example, when you run the tests in the TestQuadraticSolver
file, the testComplexRoots
test fails. The test values in the failure
diagnostic indicate that the expected values do not match the order of the actual
values. To fix this test failure, replace expected = [-1 + 2i; -1 -
2i];
with expected = sort([-1 + 2i; -1 - 2i]);
. Save
the test file and run the tests again. The tests pass.
Generate Unit Tests from Command History
You can generate unit tests from commands that you executed in the MATLAB Command Window. The generated tests execute the commands that you generate
the tests from and test the output by using the verifyEqual
method. Complete the tests by specifying the expected output
of the source code.
Generate Tests
To generate a test from commands that you executed in the MATLAB Command Window, use the Command History panel.
Right-click a command and select Generate Test. If the
Command History icon is not in the sidebar, click the Open more panels
button
and select Command
History.
To generate tests from commands that use ellipses to span multiple lines, select each of the lines, then generate the test.
To generate tests from conditional statements, select the first line of the
conditional statement through the last line. For example, for code that includes
if, elseif, else
statements, select the entire
if...end
block. Alternatively, you can select the statements
that execute if the if
, elseif
, or
else
statements are true.
When you generate tests from functions or methods that require input arguments,
the tests use inputs that appear in the command history. 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
Suppose that you executed these commands.
>> a = 2; >> b = 2; >> c = -4; >> r = quadraticSolver(a,b,c);
Generating a test from the execution of the quadraticSolver
function creates this
test.
% This is an autogenerated sample test for file quadraticSolver.m classdef testquadraticSolver < matlab.unittest.TestCase methods (Test) function test_quadraticSolver(testCase) % Specify the input(s) of quadraticSolver a = 2; b = 2; c = -4; % Exercise the function quadraticSolver.m actualOutput_r = quadraticSolver(a,b,c); % Specify the expected output(s) of quadraticSolver expectedOutput_r = testCase.verifyEqual(actualOutput_r, expectedOutput_r); end end end
Complete Tests
To avoid invalid expressions in the test, specify the expected output of the source code.
For example, you can complete the test_quadraticSolver
test by
specifying values for the expected output, expected_r
.
% This is an autogenerated sample test for file quadraticSolver.m classdef testquadraticSolver < matlab.unittest.TestCase methods (Test) function test_quadraticSolver(testCase) % Specify the input(s) of quadraticSolver a = 2; b = 2; c = -4; % Specify the expected output(s) of quadraticSolver expected_r = sort(roots([a b c])); % Exercise the function quadraticSolver actual_r = quadraticSolver(a, b, c); testCase.verifyEqual(actual_r, expected_r); end end end
Note
If the command history does not contain the commands that specify the input argument values, you must specify them in the test. This might occur if you specify input variables at the MATLAB Command Window by loading MAT files or by running scripts, specifying variable values by passing handle objects as function inputs, or if you delete commands that specify input variables from the command history.
Generate Equivalence Tests for Current Function File
You can generate equivalence tests for the current function file by using static analysis. The generated 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.
You can generate equivalence tests that generate code for these targets:
C code
C++ code
Python packages
Java packages
.NET assemblies
Deployable archives
Deployable C++ shared libraries
Generate Tests
To generate 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
functionExecutes the Python package
Verifies that the execution of the Python package matches the execution of the
quadraticSolver
function by using theverifyExecutionMatchesMATLAB
method
% This is an autogenerated sample test for file quadraticSolver.m classdef testquadraticSolverCompilerEquivalence < matlabtest.compiler.TestCase methods (Test) function test_quadraticSolverCompilerEquivalence(testCase) functionToBuild = "quadraticSolver"; compilerTarget = "pythonPackage"; % Specify the inputs a = b = c = inputArgs = {a, b, c}; % Build artifact functionPath = which(functionToBuild); buildResults = testCase.build(functionPath, 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
test_quadraticSolverCompilerEquivalence
test by specifying
values for the function input values a
, b
, and
c
.
% This is an autogenerated sample test for file quadraticSolver.m classdef testquadraticSolverCompilerEquivalence < matlabtest.compiler.TestCase methods (Test) function test_quadraticSolverCompilerEquivalence(testCase) functionToBuild = "quadraticSolver"; compilerTarget = "pythonPackage"; % Specify the inputs a = 2; b = 2; c = -4; inputArgs = {a, b, c}; % Build artifact functionPath = which(functionToBuild); buildResults = testCase.build(functionPath, compilerTarget); % Execute artifact executionResults = testCase.execute(buildResults, inputArgs); % Execute MATLAB code and compare testCase.verifyExecutionMatchesMATLAB(executionResults) end end end
Run Tests and Verify Results
You can run the generated tests by using any of these options:
runtests
function at the MATLAB command lineRun Tests button in the MATLAB Editor toolstrip on the Editor tab
Run button
in the Test Browser
Run button
in the MATLAB Test Manager
Run button
in the Code Quality Dashboard
For more information, see Run MATLAB Tests.