matlab.unittest.fixtures.Fixture.addTeardown - Dynamically add teardown code to fixture - MATLAB (original) (raw)
Class: matlab.unittest.fixtures.Fixture
Namespace: matlab.unittest.fixtures
Dynamically add teardown code to fixture
Syntax
Description
addTeardown([fixture](#mw%5F43a0f2f6-4307-4f19-9f27-ba3ebcc424e3),[teardownFcn](#mw%5Fd1684203-09d7-4f57-8564-4e402a13707d))
registers the teardown code teardownFcn
with the fixture. To dynamically run teardown code when the testing framework tears down the fixture, calladdTeardown
within the setup method of your Fixture
subclass.
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([fixture](#mw%5F43a0f2f6-4307-4f19-9f27-ba3ebcc424e3),[teardownFcn](#mw%5Fd1684203-09d7-4f57-8564-4e402a13707d),[input1,...,inputN](#mw%5F8d54959b-f28b-4b88-9e80-4899514d9a39))
also specifies the input arguments with which teardownFcn
is invoked for fixture teardown.
Input Arguments
Fixture, specified as a matlab.unittest.fixtures.Fixture
object.
Teardown code, specified as a function handle.
Example: addTeardown(fixture,@() 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(fixture,@close,fig)
Example: addTeardown(fixture,@setenv,"UserName",originalUserName)
Examples
Create and use a custom fixture that changes the output display format for numeric values to the currency format with two digits after the decimal point.
In a file named CurrencyFormatFixture.m
in your current folder, create the CurrencyFormatFixture
class by subclassing thematlab.unittest.fixtures.Fixture
interface. Implement thesetup
method in the class so that the fixture changes the display format for numeric values to the currency format. To restore the display format to its original state after testing, call the addTeardown
method within thesetup
method.
classdef CurrencyFormatFixture < matlab.unittest.fixtures.Fixture methods function setup(fixture) originalFormat = format; fixture.addTeardown(@format,originalFormat) format bank end end end
In a file named ExampleTest.m
in your current folder, create theExampleTest
class that applies the custom fixture and verifies that a numeric value is displayed in the expected format. To simplify this example, the actual value is produced by a call to the formattedDisplayText function. In practice, you test user-defined code.
classdef ExampleTest < matlab.unittest.TestCase methods (Test) function formatTest(testCase) testCase.applyFixture(CurrencyFormatFixture) actual = strtrim(formattedDisplayText(pi)); expected = "3.14"; testCase.verifyEqual(actual,expected) end end end
Run the ExampleTest
class. The testing framework sets up the fixture, which changes the display format to the currency format. Once the test run is complete, the framework tears down the fixture, which restores the original display format. In this example, the test passes.
Running ExampleTest . Done ExampleTest
Alternatives
Instead of specifying teardown code by calling the addTeardown
method within the setup method, you can implement the teardown method.
Version History
Introduced in R2014a