matlab.unittest.fixtures.Fixture.teardown - Tear down fixture - MATLAB (original) (raw)

Main Content

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

Syntax

Description

teardown([fixture](#mw%5F71dd40d4-393d-462f-8510-2b163cdff78e)) restores the environment to its original state when the testing framework tears down the fixture.

To specify teardown code, you can implement this method in your Fixture subclass. Alternatively, you can call the addTeardown method within the setup method.

example

Input Arguments

expand all

Fixture, specified as a matlab.unittest.fixtures.Fixture object.

Examples

expand all

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. Add these elements to the class:

classdef CurrencyFormatFixture < matlab.unittest.fixtures.Fixture properties (Access=private) OriginalFormat end

methods
    function setup(fixture)
        fixture.OriginalFormat = format;
        format bank
    end

    function teardown(fixture)
        format(fixture.OriginalFormat)
    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


Tips

Version History

Introduced in R2014a