'An existing UserName environment variable must be defined.')
fixture.addTeardown(@setenv, 'UserName', originalUserName)
fixture.TeardownDescription = sprintf(...
'Restored the UserName environment variable to "%s".',...
originalUserName);
setenv('UserName', fixture.UserName)
end
end
Implement isCompatible Method
Classes that derive from Fixture must implement the isCompatible method if the
constructor is configurable. Since you can configure the UserName property through the
constructor, UserNameEnvironmentVariableFixture must implement
isCompatible.
The isCompatible method is called with two instances of the same class. In this case, it
is called with two instances of UserNameEnvironmentVariableFixture. The testing
framework considers the two instances compatible if their UserName properties are
equal.
In a new methods block within UserNameEnvironmentVariableFixture.m, define an
isCompatible method which returns logical 1 (true) or logical 0 (false).
methods (Access=protected)
function bool = isCompatible(fixture, other)
bool = strcmp(fixture.UserName, other.UserName);
end
end
Fixture Class Definition Summary
Below are the complete contents of UserNameEnvironmentVariableFixture.m.
classdef UserNameEnvironmentVariableFixture < ...
matlab.unittest.fixtures.Fixture
properties (SetAccess=private)
UserName
end
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
33 Unit Testing