matlab.unittest.parameters.Parameter.fromData

Class: matlab.unittest.parameters.Parameter
Package: matlab.unittest.parameters

Create parameters from data

Description

example

param = matlab.unittest.parameters.Parameter.fromData(prop,nameVal) creates an array of Parameter instances where prop defines the parameterizing property name for all Parameter elements and nameVal 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 test parameters from outside the test class.

Use the fromData method to redefine parameters defined in the TestParameter, MethodSetupParameter, or ClassSetupParameter properties block of a parameterized test.

example

param = matlab.unittest.parameters.Parameter.fromData(prop1,nameVal1,...,propN,nameValN) defines Parameter instances with multiple parameterizing property names.

Input Arguments

expand all

Parameterizing property name, specified as a character vector or string.

Example: 'myParam'

Parameter name and value, specified as a nonempty cell array or struct.

If nameVal is a struct, the struct field represents the parameter name and the struct value represents the parameter value. If nameVal is a cell array, each element represents a parameter value.

If nameVal is a cell array of character vectors, the parameter names are generated from the values. Otherwise, MATLAB® generates parameter names.

Example: struct('small', 1, 'medium', 10, 'large', 100)

Example: {42,7,13}

Example: {'double','single','uint16'}

Examples

expand all

In your working folder, create testZeros.m. This class contains five test methods, resulting in eleven parameterized tests.

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

Redefine the type parameter so that the test uses uint64 and int64 data types in the parameterization instead of single, double, and uint16. Create parameters.

import matlab.unittest.parameters.Parameter
newType = {'int64','uint64'};
param = Parameter.fromData('type',newType);

Create a test suite that injects the param parameters. View the names of the tests in the suite. The injected parameters are indicated by #ext.

import matlab.unittest.TestSuite
suite = TestSuite.fromClass(?testZeros,'ExternalParameters',param);
{suite.Name}'
ans =

  9×1 cell array

    {'testZeros/testClass(type=int64#ext,outSize=s2d)' }
    {'testZeros/testClass(type=int64#ext,outSize=s3d)' }
    {'testZeros/testClass(type=uint64#ext,outSize=s2d)'}
    {'testZeros/testClass(type=uint64#ext,outSize=s3d)'}
    {'testZeros/testSize(outSize=s2d)'                 }
    {'testZeros/testSize(outSize=s3d)'                 }
    {'testZeros/testDefaultClass'                      }
    {'testZeros/testDefaultSize'                       }
    {'testZeros/testDefaultValue'                      }

Run the suite.

results = suite.run;
Running testZeros
.........
Done testZeros
__________

Redefine the outSize parameter so that the test parameterizes for 1-d and 4-d arrays. Create parameters from newType and newSize.

newSize = struct('s2d',[5 3],'s4d',[2 3 2 4]);
param = Parameter.fromData('type',newType,'outSize',newSize);

Create a test suite that injects the param parameters. View the names of the tests in the suite. The injected parameters are indicated by #ext.

import matlab.unittest.TestSuite
suite = TestSuite.fromClass(?testZeros,'ExternalParameters',param);
{suite.Name}'
ans =

  9×1 cell array

    {'testZeros/testClass(type=int64#ext,outSize=s2d#ext)' }
    {'testZeros/testClass(type=int64#ext,outSize=s4d#ext)' }
    {'testZeros/testClass(type=uint64#ext,outSize=s2d#ext)'}
    {'testZeros/testClass(type=uint64#ext,outSize=s4d#ext)'}
    {'testZeros/testSize(outSize=s2d#ext)'                 }
    {'testZeros/testSize(outSize=s4d#ext)'                 }
    {'testZeros/testDefaultClass'                          }
    {'testZeros/testDefaultSize'                           }
    {'testZeros/testDefaultValue'                          }

Run the suite.

results = suite.run;
Running testZeros
.........
Done testZeros
__________

Introduced in R2018b