This method uses the level property and, therefore, results in three test elements—one
for each value in level. The testClass method tests the class of the output from the
sierpinski function with each combination of the type and level properties. This
approach results in nine test elements. The testDefaultL1Output test method does
not use a TestParameter property and, therefore, is not parameterized. This test
method verifies that the level 1 matrix contains the expected values. Since the test
method is not parameterized, it results in a one test element.
In the test methods above, you did not define the ParameterCombination attribute of
the Test methods block. This attribute is, by default, 'exhaustive'. The test
framework invokes a given test method once for every combination of the test
parameters.
Define Test methods Block with ParameterCombination Attribute
Define the following test methods in the TestCarpet class to ensure that the matrix
output by the sierpinski function has the correct number of elements. Set the
ParameterCombination attribute to 'sequential'.
methods (Test, ParameterCombination='sequential')
function testNumel(testCase, level, side)
import matlab.unittest.constraints.HasElementCount
testCase.verifyThat(sierpinski(level),...
HasElementCount(side^2))
end
end
end
Test methods with the ParameterCombination attribute set to 'sequential' are
invoked once for each corresponding value of the parameter. The properties, level and
side, must have the same number of values. Since these properties each have three
values, the testNumel method is invoked three times.
TestCarpet Class Definition Summary
The complete contents of TestCarpet.m follows.
classdef TestCarpet < matlab.unittest.TestCase
properties (TestParameter)
type = {'single','double','uint16'};
level = struct('small', 2,'medium', 4, 'large', 6);
side = struct('small', 9, 'medium', 81,'large', 729);
33 Unit Testing