Main Content

addTeardown

Class: matlab.unittest.TestCase
Namespace: matlab.unittest

Dynamically add teardown code to test case

Description

addTeardown(testCase,teardownFcn) registers the teardown code teardownFcn with the test case. To dynamically run teardown code when tests run, use addTeardown within a Test method, TestMethodSetup method, or TestClassSetup method. The timing and frequency of the teardown actions depend on the scope in which you call the method:

  • Test method — teardownFcn runs after the current test.

  • TestMethodSetup method — teardownFcn runs after each test in the test class.

  • TestClassSetup method — teardownFcn runs a single time after all the tests in the class.

To restore the environment, the method enforces a last-in, first-out (LIFO) policy so that teardown actions are performed in the reverse order of their corresponding setup actions. Use addTeardown to achieve test content that is exception safe.

addTeardown(testCase,teardownFcn,input1,...,inputN) also specifies the input arguments with which teardownFcn is invoked for fixture teardown.

example

Input Arguments

expand all

Test case, specified as a matlab.unittest.TestCase object.

Teardown code, specified as a function handle.

Example: addTeardown(testCase,@() format(originalFormat))

Input arguments with which the function handle teardownFcn is invoked for fixture teardown, specified as a comma-separated list of values.

Example: addTeardown(testCase,@close,fig)

Example: addTeardown(testCase,@setenv,"UserName",originalUserName)

Attributes

Sealedtrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Dynamically execute teardown code after each test runs.

In a file in your current folder, create the FigurePropertiesTest class. Define two Test methods in the class that each verify a figure property. To create a fresh figure for each test, use a TestMethodSetup methods block. To restore the environment after each test, call the addTeardown method in the same block.

classdef FigurePropertiesTest < matlab.unittest.TestCase
    properties
        TestFigure
    end

    methods (TestMethodSetup)
        function createFigure(testCase)
            testCase.TestFigure = figure;
            testCase.addTeardown(@close,testCase.TestFigure)
        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. Once a test runs to completion, the testing framework automatically closes the figure for that test by invoking the function handle passed to addTeardown.

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

More About

expand all

Version History

Introduced in R2013a