matlab.unittest.fixtures.EnvironmentVariableFixture - Fixture for setting environment variable - MATLAB (original) (raw)
Main Content
Namespace: matlab.unittest.fixtures
Superclasses: matlab.unittest.fixtures.Fixture
Fixture for setting environment variable
Since R2023a
Description
The matlab.unittest.fixtures.EnvironmentVariableFixture
class provides a fixture for setting an operating system environment variable. When the testing framework sets up the fixture, the fixture sets the specified environment variable to the intended value. When the framework tears down the fixture, the fixture restores the specified environment variable to its original state.
The matlab.unittest.fixtures.EnvironmentVariableFixture
class is a handle class.
Creation
Description
fixture = matlab.unittest.fixtures.EnvironmentVariableFixture(name,value)
creates a fixture for setting the environment variable name
tovalue
. During fixture setup, if name
exists as an environment variable, then the fixture replaces its current value withvalue
. If environment variable name
does not exist, then the fixture creates an environment variable named name
and assigns value
to it.
The name
and value
inputs set the[Name](matlab.unittest.fixtures.environmentvariablefixture-class.html#mw%5Fd71db317-3198-4f8c-8746-7b9da9e1cded)
and [Value](matlab.unittest.fixtures.environmentvariablefixture-class.html#mw%5Fe963d5d7-54e0-479f-832e-f98cd0750bcf)
properties of fixture
.
Properties
Environment variable name, returned as a string scalar. Specify the value of this property during creation of the fixture.
Attributes:
GetAccess | public |
---|---|
SetAccess | immutable |
Environment variable value, returned as a string scalar. Specify the value of this property during creation of the fixture.
Attributes:
GetAccess | public |
---|---|
SetAccess | immutable |
Examples
Set an environment variable while testing by using an EnvironmentVariableFixture
instance.
In a file named EnvironmentVariableTest.m
in your current folder, create the EnvironmentVariableTest
class that uses a fixture to set an environment variable "NAME"
. For illustration purposes, the test in this example displays the environment variable value before and after applying the fixture.
classdef EnvironmentVariableTest < matlab.unittest.TestCase methods (Test) function testEnvironmentVariable(testCase) import matlab.unittest.fixtures.EnvironmentVariableFixture fixture = EnvironmentVariableFixture("NAME","David"); disp("Initial value of the environment variable " + ... fixture.Name + ": " + getenv(fixture.Name)) testCase.applyFixture(fixture) disp("Updated value of the environment variable " + ... fixture.Name + ": " + getenv(fixture.Name)) end end end
Run the test class. The fixture changes the empty value to the value specified in its constructor.
runtests("EnvironmentVariableTest");
Running EnvironmentVariableTest Initial value of the environment variable NAME: Updated value of the environment variable NAME: David . Done EnvironmentVariableTest
After testing, the framework tears down the fixture, which restores the environment variable to its previous state.
value =
0×0 empty char array
Version History
Introduced in R2023a