MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

end


Create Fresh Fixture Functions


Fresh fixtures are function level setup and teardown code that runs before and after each
test function in your file. In this example, the functions open the saved figure and find the
handles. After testing, the framework closes the figure.


You must name fresh fixture functions setup and teardown, respectively. Similar to the
file fixture functions, these functions take a single input argument, testCase. In this
example, these functions create a new field in the TestData structure that includes
handles to the figure and to the axes. This allows information to pass between setup, test,
and teardown functions.


Create the setup and teardown functions as a local functions to axesPropertiesTest.
Open the saved figure for each test to ensure test independence.


% Copyright 2015 The MathWorks, Inc.


function setup(testCase)
testCase.TestData.Figure = openfig(testCase.TestData.figName);
testCase.TestData.Axes = findobj(testCase.TestData.Figure,...
'Type','Axes');
end


function teardown(testCase)
close(testCase.TestData.Figure)
end


In addition to custom setup and teardown code, the Unit Testing Framework provides
some classes for creating fixtures. For more information see
matlab.unittest.fixtures.


Create Test Functions


Each test is a local function that follows the naming convention of having ‘test’ at the
beginning or end of the function name. The test array does not include local functions
that do not follow this convention. Similar to setup and teardown functions, individual test
functions must accept a single input argument, testCase. Use this test case object for
verifications, assertions, assumptions, and fatal assertions functions.


Write Test Using Setup and Teardown Functions
Free download pdf