matlab.unittest.selectors.HasName - Select TestSuite array elements by name - MATLAB (original) (raw)

Namespace: matlab.unittest.selectors
Superclasses: matlab.unittest.selectors.Selector

Select TestSuite array elements by name

Description

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

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 namespace name, filename (excluding the extension), procedure name, and information about parameterization.

Creation

Description

selector = matlab.unittest.selectors.HasName([name](#mw%5F10b81987-25d9-43a2-8a8e-376acd6d8133)) creates a selector that selects TestSuite array elements whose name matches the specified value.

example

Input Arguments

expand all

Name of the test, specified as a string scalar, character vector, or matlab.unittest.constraints.Constraint object. Test selection by name is subject to these conditions:

This argument sets the [Constraint](matlab.unittest.selectors.hasname-class.html#mw%5F677982c4-d0f6-40fc-bd9f-011d4156d4e4) property.

Properties

expand all

Condition that the test 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:

Attributes:

GetAccess public
SetAccess immutable

Examples

collapse all

Create filtered test suites by selecting tests using theHasName class.

In a file named ZerosTest.m in your current folder, create theZerosTest 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.HasName 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 class name and the corresponding Test method name. For the parameterized tests, the names also include information about parameterization.

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 a specific test using its name. The filtered test suite has a singleTest element.

suite1 = suite.selectIf(HasName("ZerosTest/testDefaultClass")); disp({suite1.Name}')

{'ZerosTest/testDefaultClass'}

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

suite2 = suite.selectIf(HasName(ContainsSubstring("Size")) | ... HasName(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 name contains the substring"Class".

suite3 = TestSuite.fromClass(?ZerosTest, ... HasName(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'               }

Version History

Introduced in R2014a

expand all

When you assign a nonempty cell array to a parameterization property, the testing framework generates parameter names from the elements of the cell array by taking into account their values, types, and dimensions. In previous releases, if the property value is a cell array of character vectors, the framework generates parameter names from the values in the cell array. Otherwise, the framework specifies parameter names as value1, value2, …, valueN.

If your code uses parameter names to create or filter test suites, replace the old parameter names with the descriptive parameter names. For example, update suite = testsuite(pwd,"ParameterName","value1") by replacing value1 with a descriptive parameter name.