MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

For example, if you manually write setup and teardown code to set up a temporary folder
for each test, and then you make that folder your current working folder, your setup and
teardown functions could look like this.


function setup(testCase)
% store current folder
testCase.TestData.origPath = pwd;


% create temporary folder
testCase.TestData.tmpFolder = ['tmpFolder' datestr(now,30)];
mkdir(testCase.TestData.tmpFolder)


% change to temporary folder
cd(testCase.TestData.tmpFolder)
end


function teardown(testCase)
% change to original folder
cd(testCase.TestData.origPath)


% delete temporary folder
rmdir(testCase.TestData.tmpFolder)
end


However, you also can use a fixture to replace both of those functions with just a modified
setup function. The fixture stores the information necessary to restore the initial state
and performs the teardown actions.


function setup(testCase)
% create temporary folder
f = testCase.applyFixture(matlab.unittest.fixtures.TemporaryFolderFixture);


% change to temporary folder
testCase.applyFixture(matlab.unittest.fixtures.CurrentFolderFixture(f.Folder));
end


Test Logging and Verbosity


Your test functions can use the TestCase.log method. By default, the test runner
reports diagnostics logged at verbosity level 1 (Terse). Use the
LoggingPlugin.withVerbosity method to respond to messages of other verbosity
levels. Construct a TestRunner object, add the LoggingPlugin, and run the suite with
the TestRunner.run method. For more information on creating a test runner, see “Test
Runner Customization” on page 33-46.


Extending Function-Based Tests
Free download pdf