Main Content

Create Tests for MATLAB Source Code

MATLAB® Test™ enables you to interactively create tests for your scripts and functions in the Editor or Live Editor. The test file created for a script or function contains a test class to exercise that script or function. You can use this file as a starting point for writing your own tests.

This topic shows how to create a test for a simple function and then build on the generated test code.

Create Test for Function

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 {mustBeNumeric,mustBeNonzero}
    b {mustBeNumeric}
    c {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.

Selecting the Create Test option for the quadraticSolver function

This code provides the contents of the resulting test file. The generated test class:

  • Includes autogenerated inputs that satisfy the arguments block in the quadraticSolver function

  • Calls the quadraticSolver function to produce an output

  • 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)
            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

A test file created for your script or function 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.

Note

MATLAB Test attempts to generate input argument values only if your function uses an arguments block for input argument validation. If your function 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

Build on the test code generated for the quadraticSolver function by first completing the code of the generated Test method and then adding another Test method using the code insertion options on the toolstrip. For more information on how to interactively add elements to a test class, see Insert Test Code Using Editor.

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:

  1. Rename the test class as SolverTest and the Test method as solution. Then, remove the comments you no longer need from the file and save the file as SolverTest.m in your current folder.

  2. Specify the expected output of the function by calling the roots function with the autogenerated values.

  3. 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 the Insert test method button in the Test section on the Editor tab.

Test section on the Editor tab

Implement the added method by following these steps:

  1. Rename the method as nonnumericInput.

  2. Add code to the method to verify that the quadraticSolver function throws an error when it is called with inputs 1, '-3', and 2.

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 the SolverTest class interactively in the Editor or in the Test Browser app. For example, with the test class code visible in the Editor, go to the Editor tab and in the Run section, click the Run all tests in file button. In this example, both the tests pass.

Run section on the Editor tab

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.

See Also

Functions

Classes

Related Topics