Fixture for setting environment variable
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.
matlab.unittest.fixtures.EnvironmentVariableFixture class is a
fixture = matlab.unittest.fixtures.EnvironmentVariableFixture(name,value)
creates a fixture for setting the environment variable
value. During fixture setup, if
name exists as an
environment variable, then the fixture replaces its current value with
value. If environment variable
name does not
exist, then the fixture creates an environment variable named
value to it.
Name — Environment variable name
Environment variable name, returned as a string scalar. Specify the value of this property during creation of the fixture.
Value — Environment variable value
Environment variable value, returned as a string scalar. Specify the value of this property during creation of the fixture.
Modify Environment Variable While Testing
Set an environment variable while testing by using an
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.
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 = getenv("NAME")
value = 0×0 empty char array
Introduced in R2023a