Main Content

matlab.unittest.selectors.HasSharedTestFixture 类

命名空间: matlab.unittest.selectors

根据共享测试脚手架选择 TestSuite 数组元素

描述

matlab.unittest.selectors.HasSharedTestFixture 类提供用于根据共享测试脚手架过滤测试套件的选择器。

对于基于类的测试,共享测试脚手架是由对应的 TestCase 类的 SharedTestFixtures 类级属性指定的所有脚手架。

类属性

Sealed
true

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

创建对象

描述

示例

selector = matlab.unittest.selectors.HasSharedTestFixture(expectedFixture) 创建一个选择器,用于选择使用与 expectedFixture 兼容的共享测试脚手架的 TestSuite 数组元素并设置 ExpectedFixture 属性。如果两个脚手架属于同一类并且它们对环境作出相同的更改,则它们是兼容的。

属性

全部展开

共享测试脚手架,以 matlab.unittest.fixtures.Fixture 对象形式返回。在创建选择器的过程中指定此属性的值。

属性:

GetAccess
public
SetAccess
private

示例

全部折叠

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

在当前文件夹中创建名为 myTests 的文件夹。然后,在 myTests 中名为 Feature1Test.m 的文件中,创建 Feature1Test 类。该类包含使用脚手架隐藏指定警告的测试。

classdef (SharedTestFixtures={ ...
        matlab.unittest.fixtures.SuppressedWarningsFixture( ...
        "MATLAB:rmpath:DirNotFound")}) ...
        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

myTests 文件夹下名为 Feature2Test.m 的文件中,创建 Feature2Test 类。该类包含的测试使用一个脚手架来创建临时文件夹,并使用另一个脚手架来隐藏指定的警告。

classdef (SharedTestFixtures={ ...
        matlab.unittest.fixtures.TemporaryFolderFixture( ...
        "WithSuffix","_TestData"), ...
        matlab.unittest.fixtures.SuppressedWarningsFixture( ...
        "MATLAB:rmpath:DirNotFound")}) ...
        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.HasSharedTestFixture
import matlab.unittest.fixtures.SuppressedWarningsFixture
import matlab.unittest.fixtures.TemporaryFolderFixture

创建与此示例中使用的共享测试脚手架兼容的脚手架。

warnf = SuppressedWarningsFixture("MATLAB:rmpath:DirNotFound");
tempf = TemporaryFolderFixture("WithSuffix","_TestData");

根据 myTests 文件夹创建一个测试套件。然后,显示 TestSuite 数组元素的名称。测试套件包含四个测试。

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

选择使用与脚手架 tempf 兼容的脚手架的所有测试。经过过滤的测试套件包含来自 Feature2Test 的测试,因为此类有与 tempf 兼容的共享测试脚手架。

suite1 = suite.selectIf(HasSharedTestFixture(tempf));
disp({suite1.Name}')
    {'Feature2Test/defaultBehavior'}
    {'Feature2Test/otherBehavior'  }

选择使用与 warnf 兼容(但不与 tempf 兼容)的脚手架的所有测试。只有 Feature1Test 类中的测试满足此条件。

suite2 = suite.selectIf( ...
    ~HasSharedTestFixture(tempf) & HasSharedTestFixture(warnf));
disp({suite2.Name}')
    {'Feature1Test/defaultBehavior'}
    {'Feature1Test/otherBehavior'  }

通过只包含使用脚手架创建基本临时文件夹的测试,直接基于 myTests 创建一个经过过滤的测试套件。生成的测试套件为空,因为选择器使用了与任何共享测试脚手架都不兼容的脚手架。尽管 Feature2Test 中的测试使用脚手架来创建临时文件夹,但其脚手架包含该临时文件夹名称的后缀。

suite3 = TestSuite.fromFolder("myTests", ...
    HasSharedTestFixture(TemporaryFolderFixture))
suite3 = 

  1×0 Test array with properties:

    Name
    ProcedureName
    TestClass
    BaseFolder
    Parameterization
    SharedTestFixtures
    Tags

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

版本历史记录

在 R2014a 中推出