Create Basic Custom Fixture
This example shows how to create a basic custom fixture that changes the display format
to hexadecimal representation. The example also shows to use the fixture to test a
function that displays a column of numbers as text. After the testing completes, the
framework restores the display format to its pretest state.
Create FormatHexFixture Class Definition
In a file in your working folder, create a new class, FormatHexFixture that inherits
from the matlab.unittest.fixtures.Fixture class. Since we want the fixture to
restore the pretest state of the MATLAB display format, create an OriginalFormat
property to keep track of the original display format.
classdef FormatHexFixture < matlab.unittest.fixtures.Fixture
properties (Access=private)
OriginalFormat
end
Implement Setup and Teardown Methods
Subclasses of the Fixture class must implement the setup method. Use this method to
record the pretest display format, and set the format to 'hex'. Use the teardown
method to restore the original display format. Define the setup and teardown methods
in the methods block of the FormatHexFixture.m file.
methods
function setup(fixture)
fixture.OriginalFormat = get(0, 'Format');
set(0, 'Format', 'hex')
end
function teardown(fixture)
set(0, 'Format', fixture.OriginalFormat)
end
end
end
Apply Custom Fixture
In a file in your working folder, create the following test class, SampleTest.m.
classdef SampleTest < matlab.unittest.TestCase
methods (Test)
33 Unit Testing