Main Content

matlab.unittest.selectors.HasProcedureName Class

Namespace: matlab.unittest.selectors

Select TestSuite array elements by procedure name

Description

The matlab.unittest.selectors.HasProcedureName class provides a selector for filtering a test suite based on test procedure names.

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

Class Attributes

Sealed
true

For information on class attributes, see Class Attributes.

Creation

Description

selector = matlab.unittest.selectors.HasProcedureName(name) creates a selector that selects TestSuite array elements with the specified test procedure name.

example

Input Arguments

expand all

Name of the test procedure, specified as a string scalar, character vector, or matlab.unittest.constraints.Constraint object. Test selection by procedure name depends on how you specify name:

  • If you specify a string scalar or character vector, the name of the test procedure must be the same as the specified value.

  • If you specify a constraint, the name of the test procedure must satisfy the constraint.

This argument sets the Constraint property.

Properties

expand all

Condition that the test procedure name must satisfy for the test to be included in the filtered test suite, returned as a matlab.unittest.constraints.Constraint object.

This property is set by the name input argument:

  • If you specify a string scalar or character vector, the testing framework sets the property to the IsEqualTo constraint with the expected value as the specified test procedure name.

  • If you specify a constraint, the testing framework sets the property to the constraint.

Attributes:

GetAccess
public
SetAccess
immutable

Examples

collapse all

Create filtered test suites by selecting tests using the HasProcedureName class.

In a file named ZerosTest.m in your current folder, create the ZerosTest class, which tests the zeros function.

classdef ZerosTest < matlab.unittest.TestCase
    properties (TestParameter)
        type = {'single','double','uint16'};
        size = struct("s2d",[3 3],"s3d",[2 5 4]);
    end
    
    methods (Test)
        function testClass(testCase,size,type)
            testCase.verifyClass(zeros(size,type),type)
        end
        
        function testSize(testCase,size)
            testCase.verifySize(zeros(size),size)
        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

Import the classes used in this example.

import matlab.unittest.TestSuite
import matlab.unittest.selectors.HasProcedureName
import matlab.unittest.constraints.ContainsSubstring

Create a test suite from the ZerosTest class. Then, display the names of the TestSuite array elements. Each name includes the test procedure name.

suite = testsuite("ZerosTest");
disp({suite.Name}')
    {'ZerosTest/testClass(size=s2d,type=single)'}
    {'ZerosTest/testClass(size=s2d,type=double)'}
    {'ZerosTest/testClass(size=s2d,type=uint16)'}
    {'ZerosTest/testClass(size=s3d,type=single)'}
    {'ZerosTest/testClass(size=s3d,type=double)'}
    {'ZerosTest/testClass(size=s3d,type=uint16)'}
    {'ZerosTest/testSize(size=s2d)'             }
    {'ZerosTest/testSize(size=s3d)'             }
    {'ZerosTest/testDefaultClass'               }
    {'ZerosTest/testDefaultSize'                }
    {'ZerosTest/testDefaultValue'               }

Select all the tests with testClass as the procedure name.

suite1 = suite.selectIf(HasProcedureName("testClass"));
disp({suite1.Name}')
    {'ZerosTest/testClass(size=s2d,type=single)'}
    {'ZerosTest/testClass(size=s2d,type=double)'}
    {'ZerosTest/testClass(size=s2d,type=uint16)'}
    {'ZerosTest/testClass(size=s3d,type=single)'}
    {'ZerosTest/testClass(size=s3d,type=double)'}
    {'ZerosTest/testClass(size=s3d,type=uint16)'}

Select all the tests whose procedure name contains "Size" or "Value".

suite2 = suite.selectIf(HasProcedureName(ContainsSubstring("Size")) | ...
    HasProcedureName(ContainsSubstring("Value")));
disp({suite2.Name}')
    {'ZerosTest/testSize(size=s2d)'}
    {'ZerosTest/testSize(size=s3d)'}
    {'ZerosTest/testDefaultSize'   }
    {'ZerosTest/testDefaultValue'  }

Create a filtered test suite directly from the ZerosTest class by including only tests whose procedure name contains the substring "Class".

suite3 = TestSuite.fromClass(?ZerosTest, ...
    HasProcedureName(ContainsSubstring("Class")));
disp({suite3.Name}')
    {'ZerosTest/testClass(size=s2d,type=single)'}
    {'ZerosTest/testClass(size=s2d,type=double)'}
    {'ZerosTest/testClass(size=s2d,type=uint16)'}
    {'ZerosTest/testClass(size=s3d,type=single)'}
    {'ZerosTest/testClass(size=s3d,type=double)'}
    {'ZerosTest/testClass(size=s3d,type=uint16)'}
    {'ZerosTest/testDefaultClass'               }

Alternative Functionality

Use the HasProcedureName class for maximum flexibility when filtering a test suite based on test procedure names. Alternatively, you can create a filtered test suite using the ProcedureName name-value argument. For example:

filteredSuite = matlab.unittest.TestSuite.fromClass(?ZerosTest, ...
    "ProcedureName","testClass");

You can also select and run tests using the ProcedureName name-value argument of the runtests or runperf function. For example:

results = runtests("ZerosTest.m","ProcedureName","testClass");

Version History

Introduced in R2017a