How to conditionally run TestMethodSetup functions
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Dimitris Floros
le 5 Déc 2020
Réponse apportée : Shraddha Jain
le 23 Déc 2020
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
0 commentaires
Réponse acceptée
Shraddha Jain
le 23 Déc 2020
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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Write Unit Tests dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!