matlab.unittest.selectors.HasParameter - Select TestSuite array elements by parameterization - MATLAB (original) (raw)

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

Select TestSuite array elements by parameterization

Description

The matlab.unittest.selectors.HasParameter class provides a selector for filtering a test suite based on parameterization. For more information about parameterized tests, see Use Parameters in Class-Based Tests.

Creation

Description

selector = matlab.unittest.selectors.HasParameter creates a selector that selects any parameterized TestSuite array elements.

example

selector = matlab.unittest.selectors.HasParameter([Name,Value](#namevaluepairarguments)) creates a selector with additional options specified by one or more name-value arguments. For example, selector = matlab.unittest.selectors.HasParameter("Property","type") creates a selector that selects all the parameterized tests with the parameterization property"type".

example

Name-Value Arguments

expand all

Specify optional pairs of arguments asName1=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.

Example: selector = matlab.unittest.selectors.HasParameter(Property="type")

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

Example: selector = matlab.unittest.selectors.HasParameter("Property","type")

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

This argument sets the [PropertyConstraint](matlab.unittest.selectors.hasparameter-class.html#mw%5Fb0e623a4-292f-443f-921e-e71a4e896616) property.

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

This argument sets the [NameConstraint](matlab.unittest.selectors.hasparameter-class.html#mw%5Ff42013bb-4dac-4979-be0d-dc8cb6a813b3) property.

Parameter value, specified as a value of any data type. Test selection by parameter value is subject to these conditions:

This argument sets the [ValueConstraint](matlab.unittest.selectors.hasparameter-class.html#mw%5F496e699f-f393-499c-a848-f417267e57d7) property.

Properties

expand all

Condition that the parameterization property of the test must satisfy for the test to be included in the filtered test suite, returned as amatlab.unittest.constraints.Constraint object.

This property is set by the Property name-value argument:

Attributes:

GetAccess public
SetAccess immutable

Condition that the parameter name of the test must satisfy for the test to be included in the filtered test suite, returned as amatlab.unittest.constraints.Constraint object.

This property is set by the Name name-value argument:

Attributes:

GetAccess public
SetAccess immutable

Condition that the parameter value of the test must satisfy for the test to be included in the filtered test suite, returned as amatlab.unittest.constraints.Constraint object.

This property is set by the Value name-value argument:

Attributes:

GetAccess public
SetAccess immutable

Examples

collapse all

Create filtered test suites by selecting tests using theHasParameter class.

In a file named ZerosTest.m in your current folder, create theZerosTest class, which tests the zeros function. The class has two parameterized Test methods:testClass and testSize.

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.HasParameter import matlab.unittest.constraints.StartsWithSubstring import matlab.unittest.constraints.HasLength

Create a test suite from the ZerosTest class and display the test names. The suite contains 11 Test elements.

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

Create a filtered test suite by selecting all the parameterized tests.

suite1 = suite.selectIf(HasParameter); 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)'}
{'ZerosTest/testSize(size=s2d)'             }
{'ZerosTest/testSize(size=s3d)'             }

Select all the tests that are not parameterized.

suite2 = suite.selectIf(~HasParameter); disp({suite2.Name}')

{'ZerosTest/testDefaultClass'}
{'ZerosTest/testDefaultSize' }
{'ZerosTest/testDefaultValue'}

Select all the parameterized tests with the parameterization property"type" and the parameter name "double".

suite3 = suite.selectIf(HasParameter("Property","type","Name","double")); disp({suite3.Name}')

{'ZerosTest/testClass(size=s2d,type=double)'}
{'ZerosTest/testClass(size=s3d,type=double)'}

Select all the parameterized tests with a parameterization property whose name starts with "t".

suite4 = suite.selectIf(HasParameter("Property",StartsWithSubstring("t"))); disp({suite4.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 parameterized tests that test the zeros function when it returns a 2-D array (such as zeros(2) andzeros(2,3)).

suite5 = suite.selectIf(HasParameter("Property","size", ... "Value",HasLength(1) | HasLength(2))); disp({suite5.Name}')

{'ZerosTest/testClass(size=s2d,type=single)'}
{'ZerosTest/testClass(size=s2d,type=double)'}
{'ZerosTest/testClass(size=s2d,type=uint16)'}
{'ZerosTest/testSize(size=s2d)'             }

Create a filtered test suite directly from the ZerosTest class by including only tests that test for a 2-D double array.

suite6 = TestSuite.fromClass(?ZerosTest, ... HasParameter("Property","type","Name","double") & ... HasParameter("Property","size","Name","s2d")); disp({suite6.Name}')

{'ZerosTest/testClass(size=s2d,type=double)'}

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.