Main Content

matlab.unittest.parameters.Parameter.fromData

类: matlab.unittest.parameters.Parameter
命名空间: matlab.unittest.parameters

从数据创建参数

说明

示例

param = matlab.unittest.parameters.Parameter.fromData(prop,nameVal) 创建由 Parameter 实例组成的数组,其中 prop 定义所有 Parameter 元素的参数化属性名称,nameVal 定义每个 Parameter 元素的名称和值。

使用 fromData 类似于使用 properties 块在基于类的测试中定义参数。例如:

    properties (TestParameter)
        prop = nameVal;
    end
但是,使用 fromData,您可以从测试类外部重新定义现有参数。

使用 fromData 方法重新定义在参数化测试的 TestParameterMethodSetupParameterClassSetupParameter properties 块中定义的参数。

示例

param = matlab.unittest.parameters.Parameter.fromData(prop1,nameVal1,...,propN,nameValN) 使用多个参数化属性名称定义 Parameter 实例。

输入参数

全部展开

参数化属性名称,指定为字符串标量或字符向量。

示例: "myParam"

参数名称和值,指定为非空元胞数组或结构体。

  • 如果 nameVal 是元胞数组,则该元胞数组的每个元素表示一个参数值。MATLAB® 根据元素的值、类型和维度生成参数名称。

  • 如果 nameVal 是结构体,则结构体字段表示参数名称,结构体值表示参数值。

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

示例: {42,7,13}

示例: {'double','single','uint16'}

示例

全部展开

在您的工作文件夹中,创建 testZeros.m。该类包含五个测试方法,产生十一个参数化测试。

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

重新定义 type 参数,以便测试在参数化中使用 uint64int64 数据类型,而不是 singledoubleuint16。创建参数。

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

创建注入 param 参数的测试套件。查看该套件中测试的名称。注入的参数由 #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'                      }

运行该套件。

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

重新定义 outSize 参数,以便测试参数化一维和四维数组。从 newTypenewSize 创建参数。

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

创建注入 param 参数的测试套件。查看该套件中测试的名称。注入的参数由 #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'                          }

运行该套件。

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

版本历史记录

在 R2018b 中推出

全部展开