Main Content


Class: matlab.unittest.TestSuite
Package: matlab.unittest

Create Test object from name of test element



testObj = matlab.unittest.TestSuite.fromName(name) creates a scalar Test object, testObj, from the name of the test element, name.

testObj = matlab.unittest.TestSuite.fromName(name,'ExternalParameters',ExternalParameters) allows the suite to use the specified external parameters in place of corresponding parameters that are defined within a parameterized test.

Input Arguments


Name of the matlab.unittest.Test element, specified as a string array, character vector, or cell array of character vectors. For class-based tests, name contains the name of the TestCase class and the test method, as well as information about parameterization. For function-based tests, name contains the name of the main function and the local test function. For script-based tests, name contains the name of the script and the title of the test section or cell. If the section does not have a title, MATLAB® assigns one. The name argument corresponds to the Name property of the Test object.


Array of matlab.unittest.parameters.Parameter instances for use in parameterized tests. The framework uses these external parameters in place of the corresponding parameters that are defined within a parameterized test. For more information, see Use External Parameters in Parameterized Test.



To learn about attributes of methods, see Method Attributes.


expand all

Create a function to test, add5, in a file on your MATLAB path.

function res = add5(x)
% ADD5 Increment input by 5.
if ~isa(x,'numeric')
    error('add5:InputMustBeNumeric','Input must be numeric.')
res = x + 5;

Create a file, Add5Test.m, on your MATLAB path that contains the following TestCase class.

classdef Add5Test < matlab.unittest.TestCase
    properties (TestParameter)
        Type = {'double','single','int8','int32'};
    methods (Test)
        function testNonNumericInput(testCase)
        function testResultType(testCase, Type)
            actOutput = add5(cast(1,Type));
            testCase.verifyClass(actOutput, Type)

At the command prompt, create a test object for the testNonNumericInput method in the Add5Test class.

import matlab.unittest.TestSuite
testObj = TestSuite.fromName('Add5Test/testNonNumericInput');

Run the test

result = run(testObj);
Running Add5Test
Done Add5Test

Create a parameterized test for the testResultType method in the Add5Test class, and run the test.

testObj = TestSuite.fromName('Add5Test/testResultType(Type=single)');
result = run(testObj);
Running Add5Test
Done Add5Test


  • The test class, function or script described by name must be on the MATLAB path when you are creating and running the TestSuite.

See Also