メインコンテンツ

Generate Tests from Command History

R2026b
Since R2025a

You can generate unit tests from previously executed commands in MATLAB®. The tests execute the commands and qualify the output by using verifyEqual. Complete the tests by specifying the expected output of the commands.

Generate Tests

To generate a test from the Command History panel, right-click the command and select Generate Test. If a command spans multiple lines, select all of the lines before generating the test.

To generate a test from a conditional statement, select the commands from the conditional statement through the last line. For example, for code that includes if, elseif, or 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.

Alternatively, you can generate a test for a function using the last execution of the function in the MATLAB Command Window. Open the function in the MATLAB Editor. On the Editor tab, in the Test section, click Generate Test > Generate test from Command History statement. The Command History panel opens to the command that the test generates from. (since R2026b)

When you generate tests from functions or methods that require input arguments, the tests use the 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

You execute 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 quadraticSolver
classdef quadraticSolverTest < matlab.unittest.TestCase
    methods (Test)
        function testQuadraticSolver(testCase)
            % Specify the inputs
            a = 2;
            b = 2;
            c = -4;

            % Exercise the code
            actualOutput_r = quadraticSolver(a,b,c);

            % Specify the expected outputs
            expectedOutput_r = 

            % Verify the result
            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 testQuadraticSolver test by specifying values for the expected output, expectedOutput_r.

% This is an autogenerated sample test for quadraticSolver
classdef quadraticSolverTest < matlab.unittest.TestCase
    methods (Test)
        function testQuadraticSolver(testCase)
            % Specify the inputs
            a = 2;
            b = 2;
            c = -4;

            % Exercise the code
            actualOutput_r = quadraticSolver(a,b,c);

            % Specify the expected outputs
            expectedOutput_r = sort(roots([a b c]));

            % Verify the result
            testCase.verifyEqual(actualOutput_r, expectedOutput_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 by loading MAT files or by running scripts, if you specify variable values by passing handle objects as function inputs, or if you delete commands that specify input variables from the command history.

Run Tests

You can run the tests by using the:

For more information, see Run MATLAB Tests.

See Also

Apps

Functions

Topics