matlab.unittest.fixtures.Fixture.isCompatible - Determine if two fixtures are compatible - MATLAB (original) (raw)

Main Content

Class: matlab.unittest.fixtures.Fixture
Namespace: matlab.unittest.fixtures

Determine if two fixtures are compatible

Syntax

Description

tf = isCompatible([fixture](#mw%5F67d9ecaa-a1e8-4759-adf3-1f605f542111)1,[fixture](#mw%5F67d9ecaa-a1e8-4759-adf3-1f605f542111)2) reports the compatibility of two fixtures to the testing framework. If the fixtures are compatible, the method returns logical 1 (true). Otherwise, it returns logical 0 (false). Two fixtures are compatible if they are of the same class and if they make the same changes to the environment.

Implement the isCompatible method in your Fixture subclass if the fixture is configurable (for example, if its class constructor accepts input arguments). The framework calls isCompatible to determine whether instances of the same Fixture subclass correspond to the same shared test fixture state. The information about fixture compatibility helps the framework decide when to perform teardown and setup actions. The framework calls the isCompatible method only with two instances of the same class, so you are not required to implement code to handle the case where the fixtures are of different classes.

example

Input Arguments

expand all

Fixture, specified as a matlab.unittest.fixtures.Fixture object.

Examples

expand all

Create a configurable fixture that changes the output display format for numeric values. Classes that define configurable fixtures must implement theisCompatible method in addition to the setup method.

In a file named NumericFormatFixture.m in your current folder, create the NumericFormatFixture class by subclassing thematlab.unittest.fixtures.Fixture interface. Add these elements to the class:

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

In your current folder, create three test classes that each use an instance ofNumericFormatFixture as a shared test fixture. Each class tests if a numeric value is displayed in the format enforced by the shared test fixture. To simplify this example, the actual value is produced by a call to the formattedDisplayText function. In practice, you test user-defined code.

In a file named TestA.m, create the TestA class.

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

In a file named TestB.m, create the TestB class.

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

In a file named TestC.m, create the TestC class.

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

The TestA and TestB classes are assigned shared fixtures that make the same change to the environment. On the other hand, theTestC class is assigned a fixture that enforces a different numeric format. According to the implementation of the isCompatible method in this example, the testing framework finds the fixtures on TestA andTestB compatible. However, it finds the fixture on TestC incompatible with the other fixtures.

The information about fixture compatibility helps the framework decide when to perform teardown and setup actions. If you run TestA,TestB, and TestC as part of the same test suite, the framework does not tear down the fixture when switching from TestA toTestB because both classes require the same environment. However, when switching from TestB to TestC, the framework tears down the existing fixture and sets up a fresh fixture required by TestC. In this example, all the tests pass.

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.


Version History

Introduced in R2014a