Main Content

matlab.unittest.TestSuite.fromName

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

Create Test object from single test name

Description

example

suite = matlab.unittest.TestSuite.fromName(testName) creates a test suite composed of a single Test object from the specified test name.

suite = matlab.unittest.TestSuite.fromName(testName,Name,Value) creates a test suite with additional options specified by one or more name-value arguments.

Input Arguments

testName

Name of the test, specified as a string scalar or character vector. For a given test file, the name of a test uniquely identifies the smallest runnable portion of the test content. The test name includes the package name, filename (excluding the extension), procedure name, and information about parameterization. The testName argument corresponds to the Name property of the Test object.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

ExternalParameters

External parameters, specified as an array of matlab.unittest.parameters.Parameter objects. 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.

DependsOn

Names of the source files and folders required by the test, specified as a string vector, character vector, or cell vector of character vectors. When you use this argument, the file defining the test must depend on the specified source code. Otherwise, the method returns an empty test suite.

The specified source code must represent at least one existing file with a .m, .p, .mlx, .mlapp, .mat, or .slx extension. You cannot specify a filename with an unsupported extension. If you specify a folder name, the framework expands it by extracting the paths to the supported files within the folder.

You must have MATLAB® Test™ installed to use DependsOn. For more information about selecting tests by source code dependency, see matlabtest.selectors.DependsOn (MATLAB Test).

Example: ["myFile.m" "myFolder"]

Example: ["folderA" "C:\work\folderB"]

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
__________

Tips

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

Version History

expand all