Main Content

matlab.unittest.fixtures.Fixture Class

Namespace: matlab.unittest.fixtures

Fundamental interface for test fixtures

Description

The matlab.unittest.fixtures.Fixture class provides an interface for test fixtures. Fixtures specify setup and teardown code that sets up the test environment and restores it to its original state after running the test.

A Fixture subclass must implement the setup method, which makes changes to the environment when the testing framework sets up the fixture. To restore the environment when the framework tears down the fixture, call the addTeardown method within the setup method, or implement the teardown method.

In addition to specifying setup and teardown actions in your Fixture subclass, you must implement the isCompatible method if the fixture is configurable (for example, if its class constructor accepts input arguments). The testing framework calls isCompatible to determine whether instances of the same Fixture subclass correspond to the same shared test fixture state.

The matlab.unittest.fixtures.Fixture class is a handle class.

Class Attributes

Abstract
true

For information on class attributes, see Class Attributes.

Properties

expand all

Description of the setup actions, returned as a character vector. Set this property in your Fixture implementation to describe the actions the fixture performs when the testing framework invokes the setup method. If you use the fixture as a shared test fixture, the framework displays the property value when setting up the fixture.

Attributes:

GetAccess
public
SetAccess
protected

Description of the teardown actions, returned as a character vector. Set this property in your Fixture implementation to describe the actions the fixture performs when the testing framework invokes the teardown method or the function handle passed to the addTeardown method. If you use the fixture as a shared test fixture, the framework displays the property value when tearing down the fixture.

Attributes:

GetAccess
public
SetAccess
protected

Methods

expand all

Events

Event NameTriggerEvent DataEvent Attributes
AssumptionFailedTriggered upon failing assumption. A QualificationEventData object is passed to listener callback functions.matlab.unittest.qualifications.QualificationEventData

NotifyAccess: private

ListenAccess: public

AssumptionPassedTriggered upon passing assumption. A QualificationEventData object is passed to listener callback functions.matlab.unittest.qualifications.QualificationEventData

NotifyAccess: private

ListenAccess: public

AssertionFailedTriggered upon failing assertion. A QualificationEventData object is passed to listener callback functions.matlab.unittest.qualifications.QualificationEventData

NotifyAccess: private

ListenAccess: public

AssertionPassedTriggered upon passing assertion. A QualificationEventData object is passed to listener callback functions.matlab.unittest.qualifications.QualificationEventData

NotifyAccess: private

ListenAccess: public

FatalAssertionFailedTriggered upon failing fatal assertion. A QualificationEventData object is passed to listener callback functions.matlab.unittest.qualifications.QualificationEventData

NotifyAccess: private

ListenAccess: public

FatalAssertionPassedTriggered upon passing fatal assertion. A QualificationEventData object is passed to listener callback functions.matlab.unittest.qualifications.QualificationEventData

NotifyAccess: private

ListenAccess: public

ExceptionThrownTriggered by the test runner when it catches an exception in the test content. An ExceptionEventData object is passed to listener callback functions.matlab.unittest.qualifications.ExceptionEventData

NotifyAccess: private

ListenAccess: public

DiagnosticLoggedTriggered upon a call to the log method. A LoggedDiagnosticEventData object is passed to listener callback functions.matlab.unittest.diagnostics.LoggedDiagnosticEventData

NotifyAccess: private

ListenAccess: public

Examples

collapse 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 the matlab.unittest.fixtures.Fixture interface. Implement the setup 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 the setup 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 the ExampleTest 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.

runtests("ExampleTest");
Running ExampleTest
.
Done ExampleTest
__________

More About

expand all

Version History

Introduced in R2014a

expand all