Main Content

Write Setup and Teardown Code Using Classes

This example shows how to implement setup and teardown code at the method level and the class level for class-based testing.

Test Fixtures

Test fixtures are setup and teardown code that sets up the pretest state of the system and returns it to the original state after running the test. Setup and teardown methods are defined in the TestCase class by these method attributes:

  • TestMethodSetup and TestMethodTeardown methods run before and after each Test method.

  • TestClassSetup and TestClassTeardown methods run before and after all Test methods in the test class.

The testing framework executes TestMethodSetup and TestClassSetup methods of superclasses before those in subclasses.

It is good practice to perform all teardown actions from within the TestMethodSetup and TestClassSetup methods blocks using the addTeardown method instead of implementing corresponding teardown methods in the TestMethodTeardown and TestClassTeardown methods blocks. Call addTeardown immediately before or after the original state change, without any other code in between that can throw an exception. Using addTeardown this way allows the testing framework to execute the teardown code in the reverse order of the setup code and also creates exception-safe test content.

Test Case with Method-Level Setup Code

The FigurePropertiesTest class tests the properties of a figure. It contains setup and teardown code at the method level. The TestMethodSetup method creates a figure before running each test, and the TestMethodTeardown method closes the figure afterwards. As discussed previously, you should try to define teardown actions with the addTeardown method. However, for illustrative purposes, this example shows the implementation of a TestMethodTeardown methods block.

classdef FigurePropertiesTest < matlab.unittest.TestCase
    properties
        TestFigure
    end

    methods (TestMethodSetup)
        function createFigure(testCase)
            testCase.TestFigure = figure;
        end
    end

    methods (TestMethodTeardown)
        function closeFigure(testCase)
            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

Test Case with Class-Level Setup Code

The CurrencyFormatTest class tests the currency display format for numeric values. It contains setup and teardown code at the class level. Before running the tests, the TestClassSetup method changes the output display format for numeric values to the currency format with two digits after the decimal point. After all the tests in the class run, the call to the addTeardown method restores the display format to its original state. This example shows the implementation of a TestClassSetup methods block for illustrative purposes. In practice, performing setup and teardown actions at the class level is helpful when it is time-consuming and inefficient to repeat these actions for each test.

classdef CurrencyFormatTest < matlab.unittest.TestCase
    methods (TestClassSetup)
        function setFormat(testCase)
            originalFormat = format;
            testCase.addTeardown(@format,originalFormat)
            format Bank
        end
    end

    methods (Test)
        function truncationTest(testCase)
            actual = strtrim(formattedDisplayText(pi));
            expected = "3.14";
            testCase.verifyEqual(actual,expected)
        end

        function divisionTest(testCase)
            actual = strtrim(formattedDisplayText(100/3));
            expected = "33.33";
            testCase.verifyEqual(actual,expected)
        end

        function negativeValueTest(testCase)
            actual = strtrim(formattedDisplayText(-1));
            expected = "-1.00";
            testCase.verifyEqual(actual,expected)
        end
    end

end

See Also

|

Related Topics