Create Advanced Custom Fixture
This example shows how to create a custom fixture that sets an environment variable.
Prior to testing, this fixture will save the current UserName variable.
Create UserNameEnvironmentVariableFixture Class Definition
In a file in your working folder, create a new class,
UserNameEnvironmentVariableFixture that inherits from the
matlab.unittest.fixtures.Fixture class. Since you want to pass the fixture a user
name, create a UserName property to pass the data between methods.
classdef UserNameEnvironmentVariableFixture < ...
matlab.unittest.fixtures.Fixture
properties (SetAccess=private)
UserName
end
Define Fixture Constructor
In the methods block of the UserNameEnvironmentVariableFixture.m file, create a
constructor method that validates the input and defines the SetupDescription. Have
the constructor accept a character vector and set the fixture’s UserName property.
methods
function fixture = UserNameEnvironmentVariableFixture(name)
validateattributes(name, {'char'}, {'row'}, '','UserName')
fixture.UserName = name;
fixture.SetupDescription = sprintf( ...
'Set the UserName environment variable to "%s".',...
fixture.UserName);
end
Implement setup Method
Subclasses of the Fixture class must implement the setup method. Use this method to
save the original UserName variable. This method also defines the
TeardownDescription and registers the teardown task of setting the UserName to the
original state after testing.
Define the setup method within the methods block of the
UserNameEnvironmentVariableFixture.m file.
function setup(fixture)
originalUserName = getenv('UserName');
fixture.assertNotEmpty(originalUserName, ...
Create Advanced Custom Fixture