Main Content

matlab.unittest.selectors.HasTag 类

命名空间: matlab.unittest.selectors

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

描述

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

构造

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

selector = matlab.unittest.selectors.HasTag(tag) 创建一个选择器,用于选择具有指定的测试标记的 TestSuite 数组元素。您可以将 tag 指定为字符串标量、字符向量或 matlab.unittest.constraints.Constraint 对象。如果 tag 是字符串标量或字符向量,则测试框架创建 IsEqualTo 约束来选择用指定值标记的 Test 元素。

对于要包括在经过过滤的测试套件中的 Test 元素,Test 元素必须使用指定的字符串标量或字符向量或者满足指定约束的值进行标记。

输入参量

全部展开

测试标记,指定为字符串标量、字符向量或 matlab.unittest.constraints.Constraint 对象。如果标记的测试满足以下条件,则经过过滤的测试套件包含该测试:

  • 如果 tag 是字符向量或字符串标量,则测试标记就是指定的值。

  • 如果 tag 是一个约束,则测试标记是一个满足指定约束的值。

属性

全部展开

测试标记要包含在经过过滤的测试套件中时必须满足的条件,指定为 matlab.unittest.constraints.Constraint 对象。

复制语义

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

示例

全部折叠

在当前文件夹中的文件 ExampleTest.m 中创建以下测试类。

classdef ExampleTest < matlab.unittest.TestCase
    methods (Test)
        function testA (testCase)
            % test code
        end
    end
    methods (Test, TestTags = {'Unit'})
        function testB (testCase)
            % test code
        end
        function testC (testCase)
            % test code
        end
    end
    methods (Test, TestTags = {'Unit','FeatureA'})
        function testD (testCase)
            % test code
        end
    end
    methods (Test, TestTags = {'System','FeatureA'})
        function testE (testCase)
            % test code
        end
    end
end

在命令提示符处,从 ExampleTest 类中创建一个测试套件并检查内容。

import matlab.unittest.TestSuite
import matlab.unittest.selectors.HasTag

suite = TestSuite.fromClass(?ExampleTest)
suite = 

  1×5 Test array with properties:

    Name
    ProcedureName
    TestClass
    BaseFolder
    Parameterization
    SharedTestFixtures
    Tags

Tests Include:
   0 Parameterizations, 0 Shared Test Fixture Classes, 3 Unique Tags.

点击 3 Unique Tags 的超链接以显示套件中的所有标记。

        Tag     
    ____________

    {'FeatureA'}
    {'System'  }
    {'Unit'    }   

选择标记为 'Unit' 的所有测试套件元素。

s1 = suite.selectIf(HasTag('Unit'))
s1 = 

  1×3 Test array with properties:

    Name
    ProcedureName
    TestClass
    BaseFolder
    Parameterization
    SharedTestFixtures
    Tags

Tests Include:
   0 Parameterizations, 0 Shared Test Fixture Classes, 2 Unique Tags.

选择不包含 'FeatureA' 标记的所有测试套件元素。

s2 =  suite.selectIf(~HasTag('FeatureA'));
{s2.Name}
ans =

  1×3 cell array

    {'ExampleTest/testB'}    {'ExampleTest/testC'}    {'ExampleTest/testA'}

选择不包含标记的所有测试套件元素。

s3 =  suite.selectIf(~HasTag)
s3 = 

  Test with properties:

                  Name: 'ExampleTest/testA'
         ProcedureName: 'testA'
             TestClass: "ExampleTest"
            BaseFolder: 'C:\work'
      Parameterization: [0×0 matlab.unittest.parameters.EmptyParameter]
    SharedTestFixtures: [0×0 matlab.unittest.fixtures.EmptyFixture]
                  Tags: {1×0 cell}

Tests Include:
   0 Parameterizations, 0 Shared Test Fixture Classes, 0 Tags.

备选方法

使用 HasTag 选择器能实现最大的灵活性,可以根据标记创建测试套件。您也可以在创建测试套件时使用 Tag 名称-值参量过滤测试套件。例如:

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

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

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

版本历史记录

在 R2015a 中推出