主要内容

matlab.unittest.selectors.HasTag 类

命名空间: matlab.unittest.selectors
超类: matlab.unittest.selectors.Selector

根据测试标记选择 TestSuite 数组元素

描述

matlab.unittest.selectors.HasTag 类提供用于根据测试标记过滤测试套件的选择器。

在基于类的测试中,测试标记由对应的 TestCase 类的 TestTags 类级或方法级属性指定。有关详细信息,请参阅标记单元测试

类属性

Sealed
true

有关类属性的信息,请参阅类属性

创建对象

描述

selector = matlab.unittest.selectors.HasTag 创建一个选择器,用于选择任何标记的 TestSuite 数组元素。

示例

selector = matlab.unittest.selectors.HasTag(tag) 创建一个选择器,用于选择具有指定标记的测试。要使选择器包含经过过滤的套件中的一个测试,Test 元素的 Tag 属性必须包含至少一个与 tag 匹配的测试标记。

示例

输入参量

全部展开

测试标记,指定为字符串标量、字符向量或 matlab.unittest.constraints.Constraint 对象。按测试标记的测试选择取决于您如何指定 tag

  • 如果指定字符串标量或字符向量,测试标记必须与指定的值相同。

  • 如果指定约束,测试标记必须满足该约束。

此参量设置 Constraint 属性。

属性

全部展开

将测试包含在经过过滤的测试套件中时测试标记必须满足的条件,以 matlab.unittest.constraints.Constraint 对象形式返回。

此属性由 tag 输入参量设置:

  • 如果您指定字符串标量或字符向量,测试框架会将该属性设置为 IsEqualTo 约束,并将预期值作为指定的测试标记。

  • 如果指定约束,测试框架会将该属性设置为约束。

属性:

GetAccess
public
SetAccess
immutable

示例

全部折叠

通过使用 HasTag 类选择测试来创建经过过滤的测试套件。

在当前文件夹下名为 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" "FeatureB"])
        function testE(testCase)
            testCase.verifyFail("Implement the test.")
        end
    end
end

导入此示例中使用的类。

import matlab.unittest.TestSuite
import matlab.unittest.selectors.HasTag
import matlab.unittest.constraints.StartsWithSubstring

基于 ExampleTest 类创建一个测试套件,并显示测试名称。该套件包含五个 Test 元素。

suite = testsuite("ExampleTest");
disp({suite.Name}')
    {'ExampleTest/testE'}
    {'ExampleTest/testD'}
    {'ExampleTest/testB'}
    {'ExampleTest/testC'}
    {'ExampleTest/testA'}

选择标记为 "Unit" 的所有测试。

suite1 = suite.selectIf(HasTag("Unit"));
disp({suite1.Name}')
    {'ExampleTest/testD'}
    {'ExampleTest/testB'}
    {'ExampleTest/testC'}

选择所有没有标记的测试。

suite2 =  suite.selectIf(~HasTag);
disp({suite2.Name}')
    {'ExampleTest/testA'}

通过仅包括具有以 "Feature" 开头的标记的测试,直接基于 ExampleTest 类创建一个经过过滤的测试套件。

suite3 = TestSuite.fromClass(?ExampleTest, ...
    HasTag(StartsWithSubstring("Feature")));
disp({suite3.Name}')
    {'ExampleTest/testE'}
    {'ExampleTest/testD'}

替代功能

根据测试标记过滤测试套件时,使用 HasTag 类可以获得最大的灵活性。您也可以使用 Tag 名称-值参量创建一个经过过滤的测试套件。例如:

filteredSuite = matlab.unittest.TestSuite.fromClass(?ExampleTest, ...
    "Tag","Unit");

您还可以使用 runtestsrunperf 函数的 Tag 名称-值参量来选择并运行标记的测试。例如:

results = runtests("ExampleTest.m","Tag","Unit");

版本历史记录

在 R2015a 中推出