Main Content

applyAndRun

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

Run function in environment provided by fixture

Since R2024b

Description

[output1,...,outputN] = applyAndRun(fixture,fcn) runs the function handle fcn in the environment provided by the specified fixture and returns any output that fcn produces. The method sets up the fixture before invoking fcn and tears down the fixture after the execution of fcn is complete.

Unlike the applyFixture method, the applyAndRun method does not tie a fixture to a specific test execution scope. You can use this method to simplify the application of setup and teardown code in tests and other automated workflows.

example

Input Arguments

expand all

Fixture to apply when running fcn, specified as a matlab.unittest.fixtures.Fixture vector.

Example: matlab.unittest.fixtures.TemporaryFolderFixture

Example: [MyCustomFixture matlab.unittest.fixtures.SuppressedWarningsFixture("MATLAB:MKDIR:DirectoryExists")]

Function to run, specified as a function handle.

Example: @() myFunction(1,2)

Example: @() mkdir("myFolder")

Attributes

Sealedtrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Display the name of the temporary folder that a fixture creates by using the applyAndRun method.

Import the TemporaryFolderFixture class.

import matlab.unittest.fixtures.TemporaryFolderFixture

Construct a fixture for creating a temporary folder. This fixture creates a temporary folder upon setup and deletes the folder and its contents upon teardown.

f1 = TemporaryFolderFixture;

Use the applyAndRun method to set up the fixture, display the name of the resulting temporary folder, and then tear down the fixture.

f1.applyAndRun(@() disp("Folder name: " + f1.Folder))
Folder name: C:\Temp\tp3f570552_a090_41aa_9e39_50f700e4db75

Construct another TemporaryFolderFixture instance that creates a temporary folder with a specified suffix for the folder name. Then, use the applyAndRun method to display the folder name once the fixture is set up. The displayed value includes the suffix specified when constructing the fixture.

f2 = TemporaryFolderFixture(WithSuffix="_UserData");
f2.applyAndRun(@() disp("Folder name: " + f2.Folder))
Folder name: C:\Temp\tp8e18567d_9431_45c4_90ae_dbcc7906db08_UserData

Write a test to verify that two Fibonacci sequence generation functions produce the same output. To run each function in your test, apply a fixture that makes the function available on the path.

Create a folder named fibonacci_i in your current folder. Then, in a file named sequence.m in the fibonacci_i folder, create a function that iteratively calculates the first n numbers of the Fibonacci sequence.

function s = sequence(n)
arguments
    n (1,1) {mustBeInteger,mustBePositive}
end
s = zeros(1,n);
for i = 1:n
    if i == 1
        s(i) = 0;
    elseif i == 2
        s(i) = 1;
    else
        s(i) = s(i-1) + s(i-2);
    end
end
end

Create another folder named fibonacci_r in your current folder. Then, in a file named sequence.m in the fibonacci_r folder, create a function that recursively calculates the first n numbers of the Fibonacci sequence.

function s = sequence(n)
arguments
    n (1,1) {mustBeInteger,mustBePositive}
end
s = zeros(1,n);
for i = 1:n
    s(i) = fib(i);
end
end

function f = fib(n)
if n == 1
    f = 0;
elseif n == 2
    f = 1;
else
    f = fib(n-1) + fib(n-2);
end
end

In a file named SequenceTest.m in your current folder, create the SequenceTest test class that compares the functions in the fibonacci_i and fibonacci_r folders. Running each function requires the folder containing the function to be on the path. In this example, the Test method uses matlab.unittest.fixtures.PathFixture instances to access the functions under test. Each call to the applyAndRun method:

  1. Makes one of the functions available on the path by setting up a PathFixture instance

  2. Runs the function and returns its output

  3. Restores the path to its original state by tearing down the fixture

classdef SequenceTest < matlab.unittest.TestCase
    methods (Test)
        function testSequence(testCase)
            import matlab.unittest.fixtures.PathFixture

            % Iterative implementation
            f1 = PathFixture("fibonacci_i");    
            output1 = f1.applyAndRun(@() sequence(10));
          
            % Recursive implementation
            f2 = PathFixture("fibonacci_r");    
            output2 = f2.applyAndRun(@() sequence(10));

            testCase.verifyEqual(output1,output2)
        end
    end
end

Run the test in the SequenceTest test class. The test passes because both implementations of the sequence function return the same output.

result = runtests("SequenceTest")
Running SequenceTest
.
Done SequenceTest
__________
result = 
  TestResult with properties:

          Name: 'SequenceTest/testSequence'
        Passed: 1
        Failed: 0
    Incomplete: 0
      Duration: 2.1351
       Details: [1×1 struct]

Totals:
   1 Passed, 0 Failed, 0 Incomplete.
   2.1351 seconds testing time.

Version History

Introduced in R2024b