Main Content

applyFixture

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

Set up fixture to delegate work to another fixture

Description

applyFixture(fixture1,fixture2) sets up fixture2 for use with fixture1. To delegate work to fixture2, call this method from the setup method of fixture1.

The applyFixture method ties the lifecycle of fixture2 to the lifecycle of fixture1. When the testing framework tears down fixture1, it also tears down fixture2.

example

f = applyFixture(fixture1,fixture2) also returns fixture2 as an output once it has been set up.

example

Input Arguments

expand all

Primary fixture, specified as an instance of matlab.unittest.fixtures.Fixture.

Fixture for setting up the primary fixture, specified as an instance of matlab.unittest.fixtures.Fixture. The primary fixture delegates work to this fixture.

Examples

expand all

Construct a fixture that removes a folder from the search path and suppresses the warning that occurs when the folder you try to remove is not on the path. The fixture uses a SuppressedWarningsFixture fixture to disable a specified warning. As part of the RemoveFolderFromPathFixture teardown, the testing framework also tears down the SuppressedWarningsFixture at the appropriate time, which restores the state of warning to its previous value.

classdef RemoveFolderFromPathFixture < matlab.unittest.fixtures.Fixture
    properties (SetAccess = immutable)
        Folder (1,1) string
    end
    methods
        function fixture = RemoveFolderFromPathFixture(folder)
            fixture.Folder = folder;
        end
        function setup(fixture)
            import matlab.unittest.fixtures.SuppressedWarningsFixture
            
            % Delegate to SuppressedWarningsFixture to suppress display of warnings.
            fixture.applyFixture(SuppressedWarningsFixture('MATLAB:rmpath:DirNotFound'));
            
            % Remove the folder from the path.
            originalPath = path;
            fixture.addTeardown(@()path(originalPath));
            rmpath(fixture.Folder);
        end
    end
    methods (Access = protected)
        function bool = isCompatible(fixture1,fixture2)
            bool = fixture1.Folder == fixture2.Folder;
        end
    end
end

Construct a fixture that creates a temporary text file. The fixture uses a TemporaryFolderFixture fixture to create a temporary folder, and then creates a text file within that folder. As part of the TemporaryTextFileFixture teardown, the framework also tears down the TemporaryFolderFixture at the appropriate time, which deletes the folder and its contents.

classdef TemporaryTextFileFixture < matlab.unittest.fixtures.Fixture
    properties (SetAccess = private)
        File
    end   
    methods
        function setup(fixture)
            import matlab.unittest.fixtures.TemporaryFolderFixture
            
            % Delegate to TemporaryFolderFixture to create a temporary folder.
            tempFixture = fixture.applyFixture(TemporaryFolderFixture);
            
            fixture.File = fullfile(tempFixture.Folder,'file.txt');
            
            fid = fopen(fixture.File,'wt');
            fixture.fatalAssertNotEqual(fid,-1);
            
            status = fclose(fid);
            fixture.fatalAssertEqual(status,0);
        end
    end
end

Version History

Introduced in R2016a