selectIf
类: matlab.unittest.TestSuite
包: matlab.unittest
选择满足条件的 TestSuite
数组元素
说明
输入参数
suite
— 测试套件
matlab.unittest.TestSuite
数组
测试套件,指定为 matlab.unittest.TestSuite
数组。
selector
— 选择器
选择器类的实例
选择器,指定为 matlab.unittest.selectors
包中类的实例。如果您安装了 MATLAB® Test™,则还可以将 selector
指定为 matlabtest.selectors.DependsOn
对象。
示例: matlab.unittest.selectors.HasTag
名称-值参数
将可选的参数对组指定为 Name1=Value1,...,NameN=ValueN
,其中 Name
是参数名称,Value
是对应的值。名称-值参数必须出现在其他参数之后,但参数对组的顺序无关紧要。
示例: filteredSuite = selectIf(suite,Name="featureA_*")
在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name
引起来。
示例: filteredSuite = selectIf(suite,"Name","featureA_*")
BaseFolder
— 基本文件夹的名称
字符串数组 | 字符向量 | 字符向量元胞数组
包含测试文件的基本文件夹的名称,指定为字符串数组、字符向量或字符向量元胞数组。此参数过滤测试套件。要使测试框架包含经过过滤的套件中的一个测试,Test
元素必须包含在 BaseFolder
指定的基本文件夹之一中。如果没有 Test
元素与基本文件夹匹配,将返回空测试套件。使用通配符 (*
) 以匹配任何数量的字符。使用问号字符 (?
) 只匹配一个字符。
对于包中定义的测试文件,基本文件夹是顶级包文件夹的父级。
DependsOn
— 源文件和文件夹的名称
字符串向量 | 字符向量 | 字符向量元胞向量
包含源代码的文件和文件夹的名称,指定为字符串向量、字符向量或字符向量元胞向量。此参数过滤测试套件。为了使测试框架在过滤后的套件中包括测试,定义测试的文件必须依赖指定的源代码。如果没有测试文件依赖源代码,则返回空测试套件。
指定的值必须表示至少一个具有 .m
、.p
、.mlx
、.mlapp
、.mat
或 .slx
扩展名的现有文件。无法用不支持的扩展名显式指定文件名。如果指定文件夹名称,框架将提取该文件夹中受支持文件的路径。
您必须安装 MATLAB Test 才能使用 DependsOn
。有关通过源代码依存关系选择测试的详细信息,请参阅 matlabtest.selectors.DependsOn
(MATLAB Test)。
示例: DependsOn=["myFile.m" "myFolder"]
示例: DependsOn=["folderA" "C:\work\folderB"]
Name
— 测试名称
字符串数组 | 字符向量 | 字符向量元胞数组
测试的名称,指定为字符串数组、字符向量或字符向量元胞数组。此参数过滤测试套件。要使测试框架包含经过过滤的套件中的一个测试,Test
元素的 Name
属性必须匹配 Name
指定的名称之一。如果 Test
元素都没有匹配的名称,将返回空测试套件。使用通配符 (*
) 以匹配任何数量的字符。使用问号字符 (?
) 只匹配一个字符。
对于一个给定的测试文件,测试的名称唯一地标识测试内容的最小可运行部分。测试名称包括包名称、文件名(不包括扩展名)、过程名称和关于参数化的信息。
ParameterProperty
— 参数化属性的名称
字符串数组 | 字符向量 | 字符向量元胞数组
定义测试使用的参数的测试类属性的名称,指定为字符串数组、字符向量或字符向量元胞数组。此参数过滤测试套件。要使测试框架包含经过过滤的套件中的一个测试,Test
元素的 Parameterization
属性必须包含 ParameterProperty
指定的至少一个属性名称。如果 Test
元素都没有匹配的属性名称,将返回空测试套件。使用通配符 (*
) 以匹配任何数量的字符。使用问号字符 (?
) 只匹配一个字符。
ParameterName
— 参数名称
字符串数组 | 字符向量 | 字符向量元胞数组
测试使用的参数的名称,指定为字符串数组、字符向量或字符向量元胞数组。MATLAB 根据定义参数的测试类属性生成参数名称:
如果属性值是元胞数组,则 MATLAB 根据元胞数组元素的值、类型和维度,基于元胞数组的元素生成参数名称。
如果属性值是结构体,则 MATLAB 将根据结构体字段生成参数名称。
ParameterName
参数过滤测试套件。要使测试框架包含经过过滤的套件中的一个测试,Test
元素的 Parameterization
属性必须包含 ParameterName
指定的至少一个参数名称。如果 Test
元素都没有匹配的参数名称,将返回空测试套件。使用通配符 (*
) 以匹配任何数量的字符。使用问号字符 (?
) 只匹配一个字符。
ProcedureName
— 测试过程的名称
字符串数组 | 字符向量 | 字符向量元胞数组
测试过程的名称,指定为字符串数组、字符向量或字符向量元胞数组。此参数过滤测试套件。要使测试框架包含经过过滤的套件中的一个测试,Test
元素的 ProcedureName
属性必须匹配 ProcedureName
指定的过程名称之一。如果 Test
元素都没有匹配的过程名称,将返回空测试套件。使用通配符 (*
) 以匹配任何数量的字符。使用问号字符 (?
) 只匹配一个字符。
在基于类的测试中,测试过程的名称是包含该测试的 Test
方法的名称。在基于函数的测试中,它是包含测试的局部函数的名称。在基于脚本的测试中,它是从测试部分标题生成的名称。与测试套件元素的名称不同,测试过程的名称不包括任何包名称、文件名或关于参数化的信息。
Superclass
— 派生测试类的父类的名称
字符串数组 | 字符向量 | 字符向量元胞数组
派生测试类的父类的名称,指定为字符串数组、字符向量或字符向量元胞数组。此参数过滤测试套件。要使测试框架包含经过过滤的套件中的一个测试,Test
元素的 TestClass
属性必须指向从 Superclass
指定的类之一派生的一个测试类。如果没有 Test
元素与类匹配,将返回空测试套件。
Tag
— 标记的名称
字符串数组 | 字符向量 | 字符向量元胞数组
测试使用的标记的名称,指定为字符串数组、字符向量或字符向量元胞数组。此参数过滤测试套件。要使测试框架包含经过过滤的套件中的一个测试,Test
元素的 Tags
属性必须包含 Tag
指定的至少一个标记名称。如果 Test
元素都没有匹配的标记名称,将返回空测试套件。使用通配符 (*
) 以匹配任何数量的字符。使用问号字符 (?
) 只匹配一个字符。
示例
过滤测试套件
使用 selectIf
方法过滤测试套件。
在当前文件夹下名为 FeatureTest.m
的文件中,创建 FeatureTest
类,它包含使用共享测试脚手架的测试。为了简化测试代码,测试类使用无条件测试失败作为未实现测试的占位符。
classdef (SharedTestFixtures={ ... matlab.unittest.fixtures.SuppressedWarningsFixture( ... "MATLAB:rmpath:DirNotFound")}) ... FeatureTest < matlab.unittest.TestCase methods (Test) function testA(testCase) testCase.verifyFail("Implement the test.") end function testB(testCase) testCase.verifyFail("Implement the test.") end function testC(testCase) testCase.verifyFail("Implement the test.") end end end
导入此示例中使用的类。
import matlab.unittest.selectors.HasName import matlab.unittest.constraints.ContainsSubstring import matlab.unittest.constraints.EndsWithSubstring import matlab.unittest.selectors.HasSharedTestFixture import matlab.unittest.fixtures.SuppressedWarningsFixture
基于 FeatureTest
类创建一个测试套件,并显示测试名称。该套件包含三个 Test
元素。
suite = testsuite("FeatureTest");
disp({suite.Name}')
{'FeatureTest/testA'} {'FeatureTest/testB'} {'FeatureTest/testC'}
通过选择其名称包含不区分大小写的文本 "TA"
的测试,创建一个经过过滤的测试套件。
suite1 = selectIf(suite,HasName(ContainsSubstring("TA","IgnoringCase",true))); disp({suite1.Name}')
{'FeatureTest/testA'}
通过选择其名称包含 "A"
或 "B"
的测试来创建一个经过过滤的测试套件。
suite2 = selectIf(suite,"Name",["*A*" "*B*"]); disp({suite2.Name}')
{'FeatureTest/testA'} {'FeatureTest/testB'}
创建一个经过过滤的测试套件,该套件使用指定的共享测试脚手架并且名称不以 "C"
结尾。所有测试都使用指定的脚手架,但 "testC"
被排除在套件之外,因为其名称不满足命名约束。
suite3 = suite.selectIf(~HasName(EndsWithSubstring("C")) & ... HasSharedTestFixture(SuppressedWarningsFixture( ... "MATLAB:rmpath:DirNotFound"))); disp({suite3.Name}')
{'FeatureTest/testA'} {'FeatureTest/testB'}
按参数化类别选择测试
通过基于参数化选择测试来创建经过过滤的测试套件。
在当前文件夹中名为 ZerosTest.m
的文件中,创建 ZerosTest
类来测试 zeros
函数。该类有两个参数化的 Test
方法:testClass
和 testSize
。
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
导入 HasParameter
类。
import matlab.unittest.selectors.HasParameter
基于 ZerosTest
类创建一个测试套件,并显示测试名称。该套件包含 11 个 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' }
通过选择所有参数化测试来创建一个过滤后测试套件。
suite1 = suite.selectIf(HasParameter); disp({suite1.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)' }
选择所有未参数化的测试。
suite2 = suite.selectIf(~HasParameter); disp({suite2.Name}')
{'ZerosTest/testDefaultClass'} {'ZerosTest/testDefaultSize' } {'ZerosTest/testDefaultValue'}
选择具有参数化属性 "type"
和参数名称 "double"
的所有参数化测试。
suite3 = suite.selectIf(HasParameter("Property","type","Name","double")); disp({suite3.Name}')
{'ZerosTest/testClass(size=s2d,type=double)'} {'ZerosTest/testClass(size=s3d,type=double)'}
选择具有名称以 "t"
开头的参数化属性的所有参数化测试。
suite4 = suite.selectIf("ParameterProperty","t*"); disp({suite4.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)'}
根据标记选择测试
通过根据测试标记选择测试来创建经过过滤的测试套件。
在当前文件夹下名为 ExampleTest.m
的文件中,创建 ExampleTest
类,该类使用 TestTags
方法级属性来标记各个测试。为了简化测试代码,此示例中的 Test
方法使用无条件测试失败作为未实现测试的占位符。
classdef ExampleTest < matlab.unittest.TestCase methods (Test) function testA(testCase) testCase.verifyFail("Implement the test.") end end methods (Test,TestTags="Unit") function testB (testCase) testCase.verifyFail("Implement the test.") end function testC(testCase) testCase.verifyFail("Implement the test.") end end methods (Test,TestTags=["Unit" "FeatureA"]) function testD(testCase) testCase.verifyFail("Implement the test.") end end methods (Test,TestTags=["System" "FeatureA"]) function testE(testCase) testCase.verifyFail("Implement the test.") end end end
导入 HasTag
类。
import matlab.unittest.selectors.HasTag
基于 ExampleTest
类创建一个测试套件,并显示测试名称。该套件包含五个 Test
元素。
suite = testsuite("ExampleTest");
disp({suite.Name}')
{'ExampleTest/testE'} {'ExampleTest/testD'} {'ExampleTest/testB'} {'ExampleTest/testC'} {'ExampleTest/testA'}
选择标记为 "Unit"
的所有测试。
suite1 = suite.selectIf("Tag","Unit"); disp({suite1.Name}')
{'ExampleTest/testD'} {'ExampleTest/testB'} {'ExampleTest/testC'}
选择所有不含标记 "FeatureA"
的测试。
suite2 = suite.selectIf(~HasTag("FeatureA"));
disp({suite2.Name}')
{'ExampleTest/testB'} {'ExampleTest/testC'} {'ExampleTest/testA'}
选择所有没有标记的测试。
suite3 = suite.selectIf(~HasTag); disp({suite3.Name}')
{'ExampleTest/testA'}
版本历史记录
在 R2014a 中推出R2023a: 按源代码依存关系过滤测试套件
如果您安装了 MATLAB Test,则可以按测试文件对指定源代码的依存关系来过滤测试套件。使用 DependsOn
名称-值参数或 matlabtest.selectors.DependsOn
类来指定源文件和文件夹。
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)