Create Tests for MATLAB Source Code
MATLAB® Test™ enables you to interactively create tests for your scripts, functions, and classes in the Editor or Live Editor. The test file created for a source file contains a test class to exercise the specified source code. You can use the generated test code as a starting point for writing your own tests.
This example shows how to create a test for a source file and then build on the generated code.
Create Test File
With your source file open in the Editor or Live Editor, you can create a test file to test the script, function, or class in that source file. The created test file contains a test class to exercise the code in your source file. If your source file is a class definition file for a concrete class, then the test file contains code to exercise the public methods of that class, including its default or explicitly defined constructor. (since R2024b)
For example, in a file named quadraticSolver.m
in your current
folder, create the quadraticSolver
function. The function takes as
inputs the coefficients of a quadratic polynomial and returns the roots of that
polynomial. To validate its inputs, the function uses an arguments
block.
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
To create a test file that contains initial test code for the
quadraticSolver
function, right-click in the Editor and select
Create Test. (In MATLAB
Online™, you can also create a test file from the Editor or
Live Editor tab by clicking Generate
Test in the Test section.)
This code provides the contents of the resulting test file. The generated test class:
Includes autogenerated inputs that satisfy the
arguments
block in thequadraticSolver
functionCalls the
quadraticSolver
function to produce an outputUses 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) a = -7162.2732; b = -1564.7743; c = 8314.7105; % 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
Note
MATLAB
Test attempts to generate input argument values for a function or class
method only if that function or method uses an arguments
block
for input argument validation. If the function or method does not include an
arguments
block, then the test class does not include
autogenerated data to represent the input arguments.
Build on Generated Test Code
A test file created for your script, function, or class serves only as a starting
point. For example, you must at least specify the expected output in the test code
generated for the quadraticSolver
function before running the test.
To build on the generated test code for your source file, use the code insertion options
on the toolstrip to add methods and parameterization properties to your test class. You
can choose whether to add a method or property at the test level, method-setup level, or
class-setup level. For more information, see Insert Test Code Using Editor.
In this example, to finalize the test class generated for the
quadraticSolver
function, first complete the code of the
generated Test
method and then add another Test
method using the code insertion options on the toolstrip.
Complete Generated Test
Method
The generated test for the quadraticSolver
function requires
you to specify the expected output of the function. To complete the test and make it
more readable, follow these steps:
Rename the test class as
SolverTest
and theTest
method assolution
. Then, remove the comments you no longer need from the file and save the file asSolverTest.m
in your current folder.Specify the expected output of the function by calling the
roots
function with the autogenerated values.Because the
verifyEqual
method tests floating-point vectors, specify a tolerance for comparison.
classdef SolverTest < matlab.unittest.TestCase methods (Test) function solution(testCase) a = -7162.2732; b = -1564.7743; c = 8314.7105; expected_r = sort(roots([a b c])); actual_r = quadraticSolver(a,b,c); testCase.verifyEqual(actual_r,expected_r,AbsTol=eps) end end end
Add Another Test
Method
To test the quadraticSolver
function against invalid inputs,
add another Test
method to the class by clicking
in the Test section on the
Editor tab.
Implement the added method by following these steps:
Rename the method as
nonnumericInput
.Add code to the method to verify that the
quadraticSolver
function throws an error when it is called with inputs1
,'-3'
, and2
.
Save the file. This code provides the contents of the SolverTest
class.
classdef SolverTest < matlab.unittest.TestCase methods (Test) function solution(testCase) a = -7162.2732; b = -1564.7743; c = 8314.7105; expected_r = sort(roots([a b c])); actual_r = quadraticSolver(a,b,c); testCase.verifyEqual(actual_r,expected_r,AbsTol=eps) end function nonnumericInput(testCase) testCase.verifyError(@()quadraticSolver(1,'-3',2), ... "MATLAB:validators:mustBeNumeric") end end end
Run Tests in Test Class
You can run the tests in your test class interactively in the Editor or in the Test Browser
app. For example, with the SolverTest
code visible in the Editor, go to
the Editor tab and in the Run section, click
. In this example, both the tests pass.
For more information on how to run tests and customize your test run interactively, see Run Tests in Editor and Run Tests Using Test Browser.