Main Content

matlab.unittest.selectors.HasProcedureName 类

命名空间: matlab.unittest.selectors

根据过程名称选择 TestSuite 数组元素

描述

matlab.unittest.selectors.HasProcedureName 类提供用于根据过程名称过滤测试套件的选择器。

构造

selector = matlab.unittest.selectors.HasProcedureName(name) 创建一个选择器,用于选择具有指定过程名称的 TestSuite 数组元素。

要使某 Test 元素包括在经过过滤的测试套件中,该 Test 元素的过程名称必须与指定的名称匹配或满足指定的约束。

输入参量

全部展开

过程名称,指定为字符串标量、字符向量或 matlab.unittest.constraints.Constraint 对象。在基于类的测试中,测试过程的名称是包含该测试的 Test 方法的名称。在基于函数的测试中,它是包含测试的局部函数的名称。在基于脚本的测试中,它是从测试部分标题生成的名称。与测试套件元素的名称不同,测试过程的名称不包括任何命名空间名称、文件名或关于参数化的信息。

示例: "Test1"

示例: matlab.unittest.constraints.ContainsSubstring("Test")

属性

全部展开

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

复制语义

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

示例

全部折叠

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

classdef ExampleTest < matlab.unittest.TestCase
    methods(Test)
        function testPathAdd(testCase)
            % test code
        end
        function testOne(testCase)
            % test code
        end
         function testTwo(testCase)
            % test code
        end
    end
end

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

import matlab.unittest.TestSuite;
import matlab.unittest.selectors.HasProcedureName;
import matlab.unittest.constraints.EndsWithSubstring;

suite = TestSuite.fromFile('ExampleTest.m');
{suite.Name}
ans =

  1×3 cell array

    {'ExampleTest/testPathAdd'}    {'ExampleTest/testOne'}    {'ExampleTest/testTwo'}

该套件包含三个测试。

选择过程名称为 testPathAdd 的所有测试套件元素,并检查内容。

s1 = suite.selectIf(HasProcedureName("testPathAdd"))
s1 = 

  Test with properties:

                  Name: 'ExampleTest/testPathAdd'
         ProcedureName: 'testPathAdd'
             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.

已过滤测试套件仅包含一个测试元素。

选择所有包含以 'One''Two' 结尾的过程名称的测试套件元素,并检查内容。

s1 =  suite.selectIf(HasProcedureName(EndsWithSubstring('One')) | ...
    HasProcedureName(EndsWithSubstring('Two')));
{s1.Name}
ans =

  1×2 cell array

    {'ExampleTest/testOne'}    {'ExampleTest/testTwo'}

在测试套件构造期间,创建一个仅包含具有子字符串 'One' 的测试的测试套件。

import matlab.unittest.constraints.ContainsSubstring;
s2 = TestSuite.fromFile('ExampleTest.m',...
    HasProcedureName(ContainsSubstring('One')))
s2 = 

  Test with properties:

                  Name: 'ExampleTest/testOne'
         ProcedureName: 'testOne'
             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.

备选方法

使用 HasProcedureName 选择器能实现根据过程名称创建测试套件的最大灵活性。您也可以在创建测试套件时使用 ProcedureName 名称-值参量过滤测试套件。例如,下面的代码行在功能上是等效的。

s = matlab.unittest.TestSuite.fromClass(?ExampleTest,"ProcedureName","Test1");
s = testsuite("ExampleTest.m","ProcedureName","Test1");

版本历史记录

在 R2017a 中推出