Main Content

matlab.unittest.selectors.HasBaseFolder 类

命名空间: matlab.unittest.selectors

根据基本文件夹选择 TestSuite 数组元素

描述

matlab.unittest.selectors.HasBaseFolder 类提供用于根据测试的基本文件夹过滤测试套件的选择器。

测试的基本文件夹是包含定义测试的文件的文件夹。对于命名空间中定义的测试,基本文件夹是顶级命名空间文件夹的父级。

类属性

Sealed
true

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

创建对象

描述

示例

selector = matlab.unittest.selectors.HasBaseFolder(folder) 创建一个选择器,用于选择其基本文件夹与指定值匹配的 TestSuite 数组元素。

输入参量

全部展开

基本文件夹的名称,指定为字符串标量、字符向量或 matlab.unittest.constraints.Constraint 对象。根据基本文件夹选择测试受以下条件限制:

  • 如果指定字符串标量或字符向量,测试的基本文件夹必须与指定的值相同。指定的值必须为完整路径。

  • 如果指定约束,测试的基本文件夹必须满足该约束。

此参量设置 Constraint 属性。

属性

全部展开

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

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

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

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

属性:

GetAccess
public
SetAccess
immutable

示例

全部折叠

通过使用 HasBaseFolder 类选择测试来创建过滤后测试套件。为了简化测试代码,此示例中的测试类使用无条件测试失败作为未实现测试的占位符。

在当前文件夹中创建名为 myTests 的文件夹。然后在 myTests 中创建两个文件夹 feature1feature2

feature1 文件夹下名为 Feature1Test.m 的文件中,创建 Feature1Test 类。

classdef Feature1Test < matlab.unittest.TestCase
    methods (Test)
        function defaultBehavior(testCase)
            testCase.verifyFail("Add code to test default behavior.")
        end
        function otherBehavior(testCase)
            testCase.verifyFail("Add code to test nondefault behavior.")
        end
    end
end

feature2 文件夹下名为 Feature2Test.m 的文件中,创建 Feature2Test 类。

classdef Feature2Test < matlab.unittest.TestCase
    methods (Test)
        function defaultBehavior(testCase)
            testCase.verifyFail("Add code to test default behavior.")
        end
        function otherBehavior(testCase)
            testCase.verifyFail("Add code to test nondefault behavior.")
        end
    end
end

导入此示例中使用的类。

import matlab.unittest.TestSuite
import matlab.unittest.selectors.HasBaseFolder
import matlab.unittest.constraints.ContainsSubstring

请确保您的当前文件夹是 myTests 的父文件夹。基于 myTests 文件夹及其子文件夹创建一个测试套件。然后,显示 TestSuite 数组元素的名称。该测试套件包含在 feature1feature2 文件夹中定义的测试。

suite = testsuite("myTests","IncludingSubfolders",true);
disp({suite.Name}')
    {'Feature1Test/defaultBehavior'}
    {'Feature1Test/otherBehavior'  }
    {'Feature2Test/defaultBehavior'}
    {'Feature2Test/otherBehavior'  }

选择在 feature1 文件夹中定义的所有测试。

suite1 = suite.selectIf(HasBaseFolder( ...
    fullfile(pwd,"myTests","feature1")));
disp({suite1.Name}')
    {'Feature1Test/defaultBehavior'}
    {'Feature1Test/otherBehavior'  }

选择 suite 中所有在 feature1 之外定义的测试。经过过滤的测试套件仅包含在 feature2 文件夹中定义的测试。

suite2 = suite.selectIf(~HasBaseFolder( ...
    fullfile(pwd,"myTests","feature1")));
disp({suite2.Name}')
    {'Feature2Test/defaultBehavior'}
    {'Feature2Test/otherBehavior'  }

通过仅包括其基本文件夹包含子字符串 "2" 的测试,直接基于 myTests 及其子文件夹创建一个经过过滤的测试套件。

suite3 = TestSuite.fromFolder("myTests", ...
    HasBaseFolder(ContainsSubstring("2")), ...
    "IncludingSubfolders",true);
disp({suite3.Name}')
    {'Feature2Test/defaultBehavior'}
    {'Feature2Test/otherBehavior'  }

版本历史记录

在 R2014a 中推出