matlab.unittest.parameters.Parameter.fromData - Create parameters from data - MATLAB (original) (raw)
Class: matlab.unittest.parameters.Parameter
Namespace: matlab.unittest.parameters
Create parameters from data
Syntax
Description
param = matlab.unittest.parameters.Parameter.fromData([prop](#mw%5F14080c10-ff65-476d-aa30-19beda59a731),[nameVal](#mw%5Ffb8f6765-0a69-4b46-a906-3435f41f8f8f))
creates an array of Parameter
instances where prop
defines the parameterization property name for all Parameter
elements andnameVal
defines the name and value for each Parameter
element.
Using fromData
is analogous to defining parameters within a class-based test using a properties
block. For example:
properties (TestParameter)
prop = nameVal;
end
However, with fromData
you can redefine existing parameters from outside the test class.
Use the fromData
method to redefine parameters defined in theTestParameter
, MethodSetupParameter
, orClassSetupParameter
properties
block of a parameterized test.
param = matlab.unittest.parameters.Parameter.fromData(prop1,nameVal1,...,propN,nameValN)
defines Parameter
instances with multiple parameterization property names.
Input Arguments
Parameterization property name, specified as a string scalar or character vector.
Example: "myParam"
Parameter names and values, specified as a nonempty cell array or structure.
- If
nameVal
is a cell array, each element of the cell array represents a parameter value. MATLAB® generates parameter names from the elements by taking into account their values, types, and dimensions. - If
nameVal
is a structure, the structure fields represent the parameter names and the structure values represent the parameter values.
Example: struct('small',1,'medium',10,'large',100)
Example: {42,7,13}
Example: {'double','single','uint16'}
Examples
Create parameters that are external to a test class by using the matlab.unittest.parameters.Parameter.fromData
method. Then, create a test suite by injecting these parameters and run the tests.
First, import the classes used in this example.
import matlab.unittest.parameters.Parameter import matlab.unittest.TestSuite
In a file named ZerosTest.m
in your current folder, create the ZerosTest
test 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
Using the fromData
static method, redefine the parameters associated with the type
property so that parameterized tests use int64
and uint64
instead of single
, double
, and uint16
as the data type.
newType = {'int64','uint64'}; param = Parameter.fromData("type",newType);
Create a test suite from the test class by injecting the new parameters, and then display the test names. The injected parameters are indicated by #ext
.
suite = TestSuite.fromClass(?ZerosTest,ExternalParameters=param); disp({suite.Name}')
{'ZerosTest/testClass(size=s2d,type=int64#ext)' }
{'ZerosTest/testClass(size=s2d,type=uint64#ext)'}
{'ZerosTest/testClass(size=s3d,type=int64#ext)' }
{'ZerosTest/testClass(size=s3d,type=uint64#ext)'}
{'ZerosTest/testSize(size=s2d)' }
{'ZerosTest/testSize(size=s3d)' }
{'ZerosTest/testDefaultClass' }
{'ZerosTest/testDefaultSize' }
{'ZerosTest/testDefaultValue' }
Run the tests in the test suite. In this example, all the tests pass.
Running ZerosTest ......... Done ZerosTest
Redefine the parameters associated with both the type
and size
parameterization properties.
newSize = struct("s1d",[1 5],"s4d",[2 3 2 4]); param = Parameter.fromData("type",newType,"size",newSize);
Create a test suite by injecting the new parameters in the param
array. Then, display the test names.
suite = TestSuite.fromClass(?ZerosTest,ExternalParameters=param); disp({suite.Name}')
{'ZerosTest/testClass(size=s1d#ext,type=int64#ext)' }
{'ZerosTest/testClass(size=s1d#ext,type=uint64#ext)'}
{'ZerosTest/testClass(size=s4d#ext,type=int64#ext)' }
{'ZerosTest/testClass(size=s4d#ext,type=uint64#ext)'}
{'ZerosTest/testSize(size=s1d#ext)' }
{'ZerosTest/testSize(size=s4d#ext)' }
{'ZerosTest/testDefaultClass' }
{'ZerosTest/testDefaultSize' }
{'ZerosTest/testDefaultValue' }
Run the tests using the newly injected parameters. The tests pass.
Running ZerosTest ......... Done ZerosTest
Version History
Introduced in R2018b
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.