matlab.unittest.fixtures.WorkingFolderFixture - Fixture for creating and changing to temporary working
folder - MATLAB (original) (raw)
Namespace: matlab.unittest.fixtures
Fixture for creating and changing to temporary working folder
Description
The matlab.unittest.fixtures.WorkingFolderFixture
creates a temporary folder and sets it as the current working folder. The test or the product under test can create files and modify the contents of the folder without affecting the source code or test folder structure.
When the testing framework sets up the fixture, it adds the current folder to the path. Then, the fixture creates a temporary folder, and changes the current working folder to the temporary folder. When the testing framework tears down the fixture, by default, it deletes the temporary folder and all folder contents. The testing framework restores the current working folder to its previous state.
Both the WorkingFolderFixture
and TemporaryFolderFixture
fixtures create a temporary folder. However, the WorkingFolderFixture
also sets the folder as the current working folder.
Construction
matlab.unittest.fixtures.WorkingFolderFixture
constructs a fixture for creating and changing to a temporary working folder.
matlab.unittest.fixtures.WorkingFolderFixture([Name,Value](#namevaluepairarguments))
constructs a fixture with additional options specified by one or more Name,Value
pair arguments. For example, matlab.unittest.fixtures.WorkingFolderFixture('PreservingOnFailure',true)
constructs a fixture that does not delete the temporary folder in the event of an error.
Name-Value Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, where Name
is the argument name and Value
is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose Name
in quotes.
Setting to preserve the temporary folder and contents after test failure, specified as false
or true
(logical 0
or 1
). This value is false
by default. You can specify it as true
during fixture construction.
If you specify 'PreservingOnFailure'
as true
and a test using the fixture encounters a failure, the testing framework displays a message in the Command Window and does not delete the folder. Failures include verification, assertion, or fatal assertion qualification failures and uncaught errors within the tests that use the fixture. Preserving the temporary folder and its contents can aid in investigation of the cause of the test failure.
Data Types: logical
Suffix for temporary folder name, specified as a character vector. The value of this parameter is appended to the name of the temporary folder.
Example: WorkingFolderFixture('WithSuffix','_ProductA')
Properties
Absolute path of the folder created by the fixture, specified as a character vector.
Indicator to preserve the temporary folder and its contents in the event of a test failure, specified as false
or true
. Set this property through the constructor via the name-value pair argument 'PreservingOnFailure'
.
Suffix for the temporary folder name, specified as a character vector. Set this property through the constructor via the name-value pair argument 'WithSuffix'
.
Copy Semantics
Handle. To learn how handle classes affect copy operations, see Copying Objects.
Examples
Create the following ExampleTest
class definition on your MATLABĀ® path.
classdef ExampleTest < matlab.unittest.TestCase methods(Test) function test1(testCase) import matlab.unittest.fixtures.WorkingFolderFixture;
testCase.applyFixture(WorkingFolderFixture);
x = 1:10;
% Save a file in the temporary folder
save('data.mat','x');
disp(['The temporary working folder: ' pwd])
ls
end
end
end
At the command prompt, run the test.
Running ExampleTest The temporary working folder: C:\AppData\Local\Temp\tp6ff2cadf_9eed_4e90_88c1_5ff9ee8abb25
. .. data.mat
. Done ExampleTest
The name of the temporary folder varies.
Create the following ExampleTest2
class definition on your MATLAB path.
classdef ExampleTest2 < matlab.unittest.TestCase methods(Test) function test1(testCase) import matlab.unittest.fixtures.WorkingFolderFixture;
f = WorkingFolderFixture('WithSuffix','_ProductA');
testCase.applyFixture(f);
x = 1:10;
% Save a file in the temporary folder
save('data.mat','x');
disp(['The temporary working folder: ' pwd])
ls
end
end
end
At the command prompt, run the test.
Running ExampleTest2 The temporary working folder: C:\AppData\Local\Temp\tp72c6ce7c_a380_4f5e_be3b_4f7191a6cd2c_ProductA
. .. data.mat
. Done ExampleTest2
The name of the temporary folder varies, but it ends with _ProductA
.
Version History
Introduced in R2016a