matlab.unittest.TestSuite.fromName

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

Create Test object from name of test element

Description

example

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

Name of the matlab.unittest.Test element, specified as a character vector or string scalar. 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.

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

ExternalParameters

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.

Attributes

Statictrue

To learn about attributes of methods, see Method Attributes.

Examples

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.')
end
res = x + 5;
end

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'};
    end
    
    methods (Test)
        function testNonNumericInput(testCase)
            testCase.verifyError(@()add5('0'),'add5:InputMustBeNumeric')
        end
        function testResultType(testCase, Type)
            actOutput = add5(cast(1,Type));
            testCase.verifyClass(actOutput, Type)
        end
        
    end
end

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
__________

See Also