MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Define properties Block


Define the properties used for parameterized testing. In the TestCarpet class, define
these properties in a property block with the TestParameter attribute.


properties (TestParameter)
type = {'single','double','uint16'};
level = struct('small', 2,'medium', 4, 'large', 6);
side = struct('small', 9, 'medium', 81,'large', 729);
end


The type property contains the different data types you want to test. The level property
contains the different fractal level you want to test. The side property contains the
number of rows and columns in the Sierpinski carpet matrix and corresponds to the
level property. To provide meaningful names for each parameterization value, level
and side are defined as structs.


Define Test methods Block


Define the following test methods in the TestCarpet class.


methods (Test)
function testRemainPixels(testCase, level)
% expected number pixels equal to 1
expPixelCount = 8^level;
% actual number pixels equal to 1
actPixels = find(sierpinski(level));
testCase.verifyNumElements(actPixels,expPixelCount)
end


function testClass(testCase, type, level)
testCase.verifyClass(...
sierpinski(level,type), type);
end


function testDefaultL1Output(testCase)
exp = single([1 1 1; 1 0 1; 1 1 1]);
testCase.verifyEqual(sierpinski(1), exp)
end


end


The testRemainPixels method tests the output of the sierpinski function by
verifying that the number of nonzero pixels is the same as expected for a particular level.


Create Basic Parameterized Test
Free download pdf