主要内容

matlab.unittest.fixtures.UIFigureFixture Class

R2026b

Namespace: matlab.unittest.fixtures
Superclasses: matlab.unittest.fixtures.Fixture

Fixture for creating UI figure

Since R2026b

Description

The matlab.unittest.fixtures.UIFigureFixture class provides a fixture for creating a UI figure. When the testing framework sets up the fixture, the fixture creates a UI figure. When the framework tears down the fixture, the fixture deletes the figure. You can use this fixture when writing tests that require a figure created using the uifigure function.

The matlab.unittest.fixtures.UIFigureFixture class is a handle class.

Creation

Description

fixture = matlab.unittest.fixtures.UIFigureFixture constructs a fixture for creating a UI figure.

example

fixture = matlab.unittest.fixtures.UIFigureFixture(Name=Value) specifies figure properties using one or more name-value arguments. For example, fixture = matlab.unittest.fixtures.UIFigureFixture(Visible="off") constructs a fixture that creates an invisible UI figure, which is suitable for testing in continuous integration (CI) environments. For more information about the supported figure properties, see uifigure.

Properties

expand all

In addition to the following properties, the UIFigureFixture class inherits properties from the Fixture class.

UI figure created by the fixture, represented as a Figure object. The fixture sets this property when the framework sets up the fixture.

Attributes:

GetAccess
public
SetAccess
private

Options used to create the UI figure, represented as a structure. The structure field names and values correspond to the figure property names and values specified as name-value arguments during fixture creation.

Attributes:

GetAccess
public
SetAccess
immutable

Examples

collapse all

Create UI figures for testing by using the UIFigureFixture class.

In a file named FigurePropertiesTest.m in your current folder, create the FigurePropertiesTest test class. Define two Test methods in the class that each verify a UI figure property. To create a fresh figure for each test, use a UIFigureFixture instance within a TestMethodSetup methods block.

classdef FigurePropertiesTest < matlab.unittest.TestCase
    properties
        TestFigure
    end

    methods (TestMethodSetup)
        function createFigure(testCase)
            fixture = testCase.applyFixture( ...
                matlab.unittest.fixtures.UIFigureFixture);
            testCase.TestFigure = fixture.Figure;
        end
    end

    methods (Test)
        function defaultCurrentPoint(testCase)
            cp = testCase.TestFigure.CurrentPoint;
            testCase.verifyEqual(cp,[0 0], ...
                "Default current point must be [0 0].")
        end

        function defaultCurrentObject(testCase)
            import matlab.unittest.constraints.IsEmpty
            co = testCase.TestFigure.CurrentObject;
            testCase.verifyThat(co,IsEmpty, ...
                "Default current object must be empty.")
        end
    end
end

Run the tests. Before running each test, the testing framework sets up a fixture that creates a UI figure. After testing, the framework tears down the fixture, which deletes the figure. In this example, the tests pass.

results = runtests("FigurePropertiesTest");
Running FigurePropertiesTest
..
Done FigurePropertiesTest
__________

Version History

Introduced in R2026b

See Also

Functions

Objects