matlab.unittest.fixtures.Fixture.setup - Set up fixture - MATLAB (original) (raw)
Main Content
Class: matlab.unittest.fixtures.Fixture
Namespace: matlab.unittest.fixtures
Syntax
Description
setup([fixture](#mw%5F6f85b676-8d9b-4c7c-9461-d2b1b0e399ff))
makes changes to the environment when the testing framework sets up the fixture. Classes deriving from theFixture
interface must implement the setup
method.
A fixture must restore the environment to its original state when the framework tears down the fixture. To restore the environment, call the addTeardown method within the setup
method, or implement theteardown method.
Input Arguments
Fixture, specified as a matlab.unittest.fixtures.Fixture
object.
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
Tips
- Only the testing framework calls the
setup
method when setting up the fixture. You cannot directly call the method in your code. To perform the setup actions insetup
, use the fixture by calling the applyFixture method.
Version History
Introduced in R2014a