Main Content

matlab.unittest.parameters.Parameter 类

命名空间: matlab.unittest.parameters

参数的基类

描述

在参数化测试中,使用参数将数据传递给测试方法。

构造

使用静态 fromData 方法实例化 Parameter

属性

全部展开

此 属性 为只读。

用于定义 Parameter 的属性的名称,存储为字符向量。

此 属性 为只读。

参数值名称,存储为字符向量。Name 唯一标识参数的特定值。

此 属性 为只读。

参数值,存储为任何类型的数组。Value 保存 TestRunner 传递给参数化方法的数据。

方法

fromData从数据创建参数

复制语义

值。要了解值类如何影响复制操作,请参阅复制对象

示例

全部折叠

在您的工作文件夹中,创建 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 中推出