Use fixture with TestCase
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 test 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.
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. tempFolder = testCase.applyFixture(TemporaryFolderFixture); testCase.applyFixture(CurrentFolderFixture(tempFolder.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 test framework restores the working folder to its previous state and deletes the temporary folder.