matlab.unittest.selectors.HasName 类
命名空间: matlab.unittest.selectors
根据名称选择 TestSuite
数组元素
描述
matlab.unittest.selectors.HasName
类提供用于根据测试名称过滤测试套件的选择器。
对于一个给定的测试文件,测试的名称唯一地标识测试内容的最小可运行部分。测试名称包括命名空间名称、文件名(不包括扩展名)、过程名称和关于参数化的信息。
创建对象
输入参量
name
— 测试名称
字符串标量 | 字符向量 | matlab.unittest.constraints.Constraint
对象
测试的名称,指定为字符串标量、字符向量或 matlab.unittest.constraints.Constraint
对象。根据名称选择测试受以下条件限制:
如果指定字符串标量或字符向量,测试的名称必须与指定的值相同。
如果指定约束,测试的名称必须满足该约束。
此参量设置 Constraint
属性。
属性
Constraint
— 测试名称必须满足的条件
matlab.unittest.constraints.Constraint
对象
将测试包含在经过过滤的测试套件中时测试名称必须满足的条件,以 matlab.unittest.constraints.Constraint
对象形式返回。
此属性由 name
输入参量设置:
如果您指定字符串标量或字符向量,测试框架会将该属性设置为
IsEqualTo
约束,并将预期值作为指定的测试名称。如果指定约束,测试框架会将该属性设置为约束。
属性:
GetAccess | public |
SetAccess | immutable |
示例
根据名称选择测试
通过使用 HasName
类选择测试来创建过滤后测试套件。
在当前文件夹中名为 ZerosTest.m
的文件中,创建 ZerosTest
类来测试 zeros
函数。
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 matlab.unittest.TestSuite import matlab.unittest.selectors.HasName import matlab.unittest.constraints.ContainsSubstring
根据 ZerosTest
类创建一个测试套件。然后,显示 TestSuite
数组元素的名称。每个名称包括类名称和对应的 Test
方法名称。对于参数化测试,元素名称还包含关于参数化的信息。
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' }
使用名称选择一个特定测试。经过过滤的测试套件只有一个 Test
元素。
suite1 = suite.selectIf(HasName("ZerosTest/testDefaultClass"));
disp({suite1.Name}')
{'ZerosTest/testDefaultClass'}
选择其名称包含 "Size"
或 "Value"
的所有测试。
suite2 = suite.selectIf(HasName(ContainsSubstring("Size")) | ... HasName(ContainsSubstring("Value"))); disp({suite2.Name}')
{'ZerosTest/testSize(size=s2d)'} {'ZerosTest/testSize(size=s3d)'} {'ZerosTest/testDefaultSize' } {'ZerosTest/testDefaultValue' }
通过仅包括其名称包含子字符串 "Class"
的测试,直接基于 ZerosTest
类创建一个经过过滤的测试套件。
suite3 = TestSuite.fromClass(?ZerosTest, ... HasName(ContainsSubstring("Class"))); disp({suite3.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/testDefaultClass' }
版本历史记录
在 R2014a 中推出R2022b: 从元胞数组生成的参数名称更具描述性
当您将非空元胞数组分配给参数化属性时,测试框架会根据元胞数组中元素的值、类型和维度生成参数名称。在以前的版本中,如果属性值是字符向量元胞数组,该框架将根据元胞数组中的值生成参数名称。否则,该框架将参数名称指定为 value1
、value2
、…、valueN
。
如果您的代码使用参数名称来创建或过滤测试套件,请用描述性参数名称替换旧的参数名称。例如,通过用描述性参数名称替换 value1
来更新 suite = testsuite(pwd,"ParameterName","value1")
。
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)