Main Content

matlab.unittest.fixtures.EnvironmentVariableFixture Class

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 to 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 name and assigns value to it.

The name and value inputs set the Name and Value properties of fixture.

example

Properties

expand all

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

collapse all

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 = getenv("NAME")
value =

  0×0 empty char array

Version History

Introduced in R2023a