Main Content

isCompatible

类: matlab.unittest.fixtures.Fixture
命名空间: matlab.unittest.fixtures

确定两个脚手架是否兼容

说明

示例

tf = isCompatible(fixture1,fixture2) 向测试框架报告两个脚手架的兼容性。如果脚手架兼容,该方法将返回逻辑值 1 (true)。否则,将返回逻辑值 0 (false)。如果两个脚手架属于同一类并且它们对环境作出相同的更改,则它们是兼容的。

如果脚手架是可配置的(例如,如果其类构造函数接受输入参量),则在您的 Fixture 子类中实现 isCompatible 方法。该框架调用 isCompatible 来确定相同 Fixture 子类的实例是否对应于相同的共享测试脚手架状态。关于脚手架兼容性的信息有助于框架确定何时执行拆解和设置操作。该框架只使用相同类的两个实例调用 isCompatible 方法,因此您不需要实现代码来处理脚手架属于不同类的情况。

输入参数

全部展开

脚手架,指定为 matlab.unittest.fixtures.Fixture 对象。

属性

Accessprotected

要了解方法的属性,请参阅方法属性

示例

全部展开

创建一个可配置的脚手架,以更改数值的输出显示格式。定义可配置脚手架的类除了实现 setup 方法之外,还必须实现 isCompatible 方法。

在当前文件夹的名为 NumericFormatFixture.m 的文件中,通过子类化 matlab.unittest.fixtures.Fixture 接口来创建 NumericFormatFixture 类。将以下元素添加到类:

  • Format 属性 - 添加此属性以存储在脚手架构造期间指定的数值格式。

  • NumericFormatFixture 方法 - 添加此构造函数方法以设置 Format 属性。

  • setup 方法 - 实现此方法,以便脚手架将输出显示格式更改为指定的数值格式。要在测试框架拆解脚手架时还原环境,请在 setup 方法中调用 addTeardown 方法。

  • isCompatible 方法 - 实现此方法,以便测试框架可以测试 NumericFormatFixture 实例在用作共享测试脚手架时的兼容性。

classdef NumericFormatFixture < matlab.unittest.fixtures.Fixture
    properties (SetAccess=immutable)
        Format (1,1) string
    end

    methods
        function fixture = NumericFormatFixture(fmt)
            fixture.Format = fmt;
        end

        function setup(fixture)
            originalFormat = format;
            fixture.addTeardown(@format,originalFormat)
            format(fixture.Format)
            fixture.SetupDescription = "Set the numeric format to " + ...
                fixture.Format + ".";
            fixture.TeardownDescription =  ...
                "Restored the numeric format to " + ...
                originalFormat.NumericFormat + ".";
        end
    end

    methods (Access=protected)
        function tf = isCompatible(fixture1,fixture2)
            tf = fixture1.Format == fixture2.Format;
        end
    end
end

在您当前文件夹中,创建三个测试类,每个测试类都使用 NumericFormatFixture 的一个实例作为共享测试脚手架。每个类测试数值是否以共享测试脚手架强制实现的格式显示。为了简化此示例,实际值是通过调用 formattedDisplayText 函数生成的。在实际操作中,您要测试用户定义的代码。

在名为 TestA.m 的文件中,创建 TestA 类。

classdef (SharedTestFixtures={NumericFormatFixture("bank")}) ...
        TestA < matlab.unittest.TestCase
    methods (Test)
        function formatTest(testCase)
            actual = strtrim(formattedDisplayText(pi));
            expected = "3.14";
            testCase.verifyEqual(actual,expected)
        end
    end
end

在名为 TestB.m 的文件中,创建 TestB 类。

classdef (SharedTestFixtures={NumericFormatFixture("bank")}) ...
        TestB < matlab.unittest.TestCase
    methods (Test)
        function formatTest(testCase)
            actual = strtrim(formattedDisplayText(100/3));
            expected = "33.33";
            testCase.verifyEqual(actual,expected)
        end
    end
end

在名为 TestC.m 的文件中,创建 TestC 类。

classdef (SharedTestFixtures={NumericFormatFixture("hex")}) ...
        TestC < matlab.unittest.TestCase
    methods (Test)
        function formatTest(testCase)
            actual = strtrim(formattedDisplayText(1));
            expected = "3ff0000000000000";
            testCase.verifyEqual(actual,expected)
        end
    end
end

TestATestB 类分配有共享脚手架,它们可对环境作出相同的更改。而 TestC 类分配有强制实施不同数值格式的脚手架。根据此示例中 isCompatible 方法的实现,测试框架发现 TestATestB 上的脚手架是兼容的。但是,它发现 TestC 上的脚手架与其他脚手架不兼容。

关于脚手架兼容性的信息有助于框架确定何时执行拆解和设置操作。如果您将 TestATestBTestC 作为同一测试套件的一部分运行,当从 TestA 切换到 TestB 时,框架不会拆解脚手架,因为这两个类需要相同的环境。然而,当从 TestB 切换到 TestC 时,框架会拆解现有脚手架,并设置 TestC 所需的新脚手架。在此示例中,所有测试都通过。

runtests(["TestA" "TestB" "TestC"]);
Setting up NumericFormatFixture
Done setting up NumericFormatFixture: Set the numeric format to bank.
__________

Running TestA
.
Done TestA
__________

Running TestB
.
Done TestB
__________

Tearing down NumericFormatFixture
Done tearing down NumericFormatFixture: Restored the numeric format to short.
__________

Setting up NumericFormatFixture
Done setting up NumericFormatFixture: Set the numeric format to hex.
__________

Running TestC
.
Done TestC
__________

Tearing down NumericFormatFixture
Done tearing down NumericFormatFixture: Restored the numeric format to short.
__________

版本历史记录

在 R2014a 中推出