MATLAB Answers

How to conditionally run TestMethodSetup functions

9 ビュー (過去 30 日間)
Dimitris Floros
Dimitris Floros 2020 年 12 月 5 日
回答済み: Shraddha Jain 2020 年 12 月 23 日
Hello,
I have setup a simple Class-based unit testing:
properties
CASE
A
B
X
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Parameterization %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
properties (ClassSetupParameter)
sc = {0, 1};
end
properties (MethodSetupParameter)
ni = {123, 789, 987};
nj = {123, 789, 987};
nk = {123, 789, 987};
da = {0.01, 0.1};
db = {0.01, 0.1};
dx = {0.01, 0.1};
rr = {0, 1};
end
properties (TestParameter)
ri = {23, 33, 100};
rj = {23, 33, 100};
rk = {23, 33, 100};
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Class setup %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
methods(TestClassSetup)
function setupPerRun(testCase,sc)
CASE = sc;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Method setup %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
methods(TestMethodSetup)
function setupRandomMatrices(testCase,ni,nj,nk,da,db,dx)
testCase.assumeEqual(testCase.sc,0);
testCase.A = sprand( ni,nk,da ) > 0;
testCase.B = sprand( nk,nj,db ) > 0;
testCase.X = sprand( ni,nj,dx ) > 0;
end
function setupSpecialMatrices(tc,rr)
ni = 456;
nj = 789;
nk = 678;
switch rr
case 1 % SPECIAL CASE: EMPTY MATRICES
tc.A = sparse( ni, nk );
tc.B = sparse( nk, nj );
tc.X = sparse( ni, nj );
case 2 % SPECIAL CASE: FULL
tc.A = sparse( ones( ni, nk ) );
tc.B = sparse( ones( nk, nj ) );
tc.X = sparse( ones( ni, nj ) );
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'exhaustive' tests %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
methods(Test)
function testFun1(tc)
% run one test per setup
end
function testFun2(tc,ri,rj,rk)
% run multiple tests per setup
end
end
What I would to do, is run either one of the two TestMethodSetup functions, depending on the value of CASE. That is, create two separate class-based instatiations, one with CASE = 0 and one with CASE = 1 dfa and specify to either run the setupRandomMatrices or the setupSpecialMatrices. The current behavior, is an exhaustive creation of all combinations in both sc = 0 and sc = 1.
Is this available through MATLAB TestSuite, or should I create two independent Classes for the tests? Both tests will share the same test function, therefore I would prefere to be able to specify this within the class, to minimize re-write and maximize code reuse.
Thank you! -- Dimitris

採用された回答

Shraddha Jain
Shraddha Jain 2020 年 12 月 23 日
Hi Dimitris,
I would suggest that first you create a new method without test attributes and add the helper functions setupRandomMatrices and setupSpecialMatrices to it. Also, define sc as a property of the Test class. Then inside methods(TestMethodSetup) call the appropriate helper functions setupRandomMatrices or setupSpecialMatrices using if/else statment on test class property sc.
Hope this helps!

その他の回答 (0 件)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by