matlab.unittest.TestSuite.fromMethod

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

Create TestSuite array from single test method

Description

example

suite = matlab.unittest.TestSuite.fromMethod(testClass,testMethod) creates a TestSuite array from the test class described by testClass and the test method described by testMethod and returns it in suite.

example

suite = matlab.unittest.TestSuite.fromMethod(testClass,testMethod,s) creates a TestSuite array from all of the Test methods contained in testMethod that satisfy the conditions specified by the selector, s.

example

suite = matlab.unittest.TestSuite.fromMethod(___,Name,Value) creates a TestSuite array with additional options specified by one or more Name,Value pair arguments. You can use this syntax with any of the input arguments of the previous syntaxes.

Input Arguments

testClass

Class describing the test methods, specified as a meta.class instance which must derive from matlab.unittest.TestCase.

testMethod

Test method, specified by either the meta.method instance or the name as a character vector or string scalar. The method must be defined with a Test method attribute.

s

Selector, specified as an instance of a class from the matlab.unittest.selector package.

Name-Value Pair Arguments

'BaseFolder'

Name of the parent of the top-level package folder that contains the file defining the test class or function, specified as a string array, character vector, or cell array of character vectors. This argument filters TestSuite array elements. For the testing framework to include a test in the suite, the Test element must be contained in one of the base folders specified by BaseFolder. If none of the Test elements matches a base folder, an empty test suite is returned. Use the wildcard character * to match any number of characters. Use the question mark character ? to match a single character.

'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.

'Name'

Name of the suite element, specified as a string array, character vector, or cell array of character vectors. This argument filters TestSuite array elements. For the testing framework to include a test in the suite, the Name property of the Test element must match one of the names specified by Name. If none of the Test elements has a matching name, an empty test suite is returned. Use the wildcard character * to match any number of characters. Use the question mark character ? to match a single character.

'ParameterProperty'

Name of a test class property that defines a parameter used by the test suite element, specified as a string array, character vector, or cell array of character vectors. This argument filters TestSuite array elements. For the testing framework to include a test in the suite, the Parameterization property of the Test element must contain at least one of the property names specified by ParameterProperty. If none of the Test elements has a matching property name, an empty test suite is returned. Use the wildcard character * to match any number of characters. Use the question mark character ? to match to a single character.

'ParameterName'

Name of a parameter used by the test suite element, specified as a string array, character vector, or cell array of character vectors. MATLAB® generates parameter names based on the test class property that defines the parameters:

  • If the property value is a cell array of character vectors, MATLAB generates parameter names from the values in the cell array. Otherwise, MATLAB specifies parameter names as value1, value2, …, valueN.

  • If the property value is a structure, MATLAB generates parameter names from the structure fields.

The ParameterName argument filters TestSuite array elements. For the testing framework to include a test in the suite, the Parameterization property of the Test element must contain at least one of the parameter names specified by ParameterName. If none of the Test elements has a matching parameter name, an empty test suite is returned. Use the wildcard character * to match any number of characters. Use the question mark character ? to match a single character.

'ProcedureName'

Name of the test procedure, specified as a string array, character vector, or cell array of character vectors. This argument filters TestSuite array elements. For the testing framework to include a test in the suite, the ProcedureName property of the Test element must match one of the procedure names specified by ProcedureName. If none of the Test elements has a matching procedure name, an empty test suite is returned. Use the wildcard character * to match any number of characters. Use the question mark character ? to match a single character.

In a class-based test, the ProcedureName is the name of the test method. In a function-based test, it is the name of the local function that contains the test. In a script-based test, it is a name generated from the test section title. Unlike Name, the name of the test procedure does not include any class or package name or information about parameterization.

'Superclass'

Name of the class that the test class derives from, specified as a string array, character vector, or cell array of character vectors. This argument filters TestSuite array elements. For the testing framework to include a test in the suite, the TestClass property of the Test element must point to a test class that derives from one of the classes specified by Superclass. If none of the Test elements matches a class, an empty test suite is returned.

'Tag'

Name of a test tag used by the test suite element, specified as a string array, character vector, or cell array of character vectors. This argument filters TestSuite array elements. For the testing framework to include a test in the suite, the Tags property of the Test element must contain at least one of the tag names specified by Tag. If none of the Test elements has a matching tag name, an empty test suite is returned. Use the wildcard character * to match any number of characters. Use the question mark character ? to match a single character.

Output Arguments

suite

Set of tests, specified as a matlab.unittest.Test array.

Attributes

Statictrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Add the matlab.unittest.TestSuite class to the current import list.

import matlab.unittest.TestSuite;
cls = ?mypackage.MyTestClass;
    
% Create the suite using the method name
suite = TestSuite.fromMethod(cls, 'testMethod');
result = run(suite)
 
% Create the suite using the meta.method instance
metaMethod = findobj(cls.MethodList, 'Name', 'testMethod');
suite = TestSuite.fromMethod(cls, metaMethod);
result = run(suite)

In your working folder, create the following testZeros.m test file. This class contains four test methods.

classdef testZeros < matlab.unittest.TestCase
    properties (TestParameter)
        type = {'single','double','uint16'};
        outSize = struct('s2d',[3 3], 's3d',[2 5 4]);
    end
    
    methods (Test)
        function testClass(testCase, type, outSize)
            testCase.verifyClass(zeros(outSize,type), type);
        end
        
        function testSize(testCase, outSize)
            testCase.verifySize(zeros(outSize), outSize);
        end
        
        function testDefaultClass(testCase)
            testCase.verifyClass(zeros, 'double');
        end
        function testDefaultSize(testCase)
            testCase.verifySize(zeros, [1 1]);
        end
        
        function testDefaultValue(testCase)
            testCase.verifyEqual(zeros,0);
        end
    end
end

The test class contains two parameterized test methods, testClass and testSize.

At the command prompt, create a test suite from all parameterized tests from the testClass method that use the parameter name 'single'.

import matlab.unittest.TestSuite;
import matlab.unittest.selectors.HasParameter;

suite = TestSuite.fromMethod(?testZeros,'testClass', ...
    HasParameter('Name','single'));
{suite.Name}'
ans = 

    'testZeros/testClass(type=single,outSize=s2d)'
    'testZeros/testClass(type=single,outSize=s3d)'

Create the testZeros.m class from the previous example.

At the command prompt, create a test suite from all parameterized tests from the testClass method that use the parameter name 'single'.

import matlab.unittest.TestSuite;

suite = TestSuite.fromMethod(?testZeros,'testClass', ...
    'ParameterName','single');
{suite.Name}'
ans = 

    'testZeros/testClass(type=single,outSize=s2d)'
    'testZeros/testClass(type=single,outSize=s3d)'

Tips

  • testClass must be on the MATLAB path when using this method to create suite, as well as when suite is run.

Introduced in R2013a