Main Content

applyFixture

Class: matlab.unittest.TestCase
Package: matlab.unittest

Use fixture with TestCase

Description

example

applyFixture(testCase,fixture) prepares the specified fixture for use with the TestCase. This method enables the use of a fixture within the scope of a single Test method or TestCase class. The life cycle of the fixture is tied to the TestCase. When the TestCase goes out of scope, the testing framework tears down the fixture.

Call applyFixture within a Test method or TestMethodSetup method to use a fixture for the current test method alone. Use applyFixture within a TestClassSetup method to set up a fixture for the entire class.

example

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

Input Arguments

testCase

matlab.unittest.TestCase instance

fixture

matlab.unittest.fixtures.Fixture instance

Examples

expand all

Create a temporary folder and make it the current working folder.

classdef applyFixtureTest < matlab.unittest.TestCase
    methods(TestMethodSetup)
        function addHelpers(testCase)
            import matlab.unittest.fixtures.TemporaryFolderFixture
            import matlab.unittest.fixtures.CurrentFolderFixture
            
            % Create a temporary folder and make it the current working folder.
            tempFixture = testCase.applyFixture(TemporaryFolderFixture);
            testCase.applyFixture(CurrentFolderFixture(tempFixture.Folder));
        end
    end
end

Each test method can write files to the current working folder, which is the temporary folder. After each test method runs, the testing framework restores the working folder to its previous state and deletes the temporary folder.