MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Use External Parameters in Parameterized Test


You can inject variable inputs into your existing class-based test. To provide test data that
is defined outside the test file and that should be used iteratively by the test (via
parameterized testing), create a Parameter and use the 'ExternalParameters'
option to TestSuite creation methods such as TestSuite.fromClass.

Create the following function to test. The function accepts an array, vectorizes the array,
removes 0, Nan, and Inf, and then sorts the array.

function Y = cleanData(X)
Y = X(:); % Vectorize array
Y = rmmissing(Y); % Remove NaN
% Remove 0 and Inf
idx = (Y==0 | Y==Inf);
Y = Y(~idx);
% If array is empty, set to eps
if isempty(Y)
Y = eps;
end
Y = sort(Y); % Sort vector
end

Create the following parameterized test for the cleanData function. The test repeats
each of the four test methods for the two data sets that are defined in the properties
block.

classdef TestClean < matlab.unittest.TestCase
properties (TestParameter)
Data = struct('clean',[5 3 9;1 42 5;32 5 2],...
'needsCleaning',[1 13;NaN 0;Inf 42]);
end
methods (Test)
function classCheck(testCase,Data)
act = cleanData(Data);
testCase.assertClass(act,'double')
end
function sortCheck(testCase,Data)
act = cleanData(Data);
testCase.verifyTrue(issorted(act))

Use External Parameters in Parameterized Test
Free download pdf