Hi Yvan,
it sounds like you simply need access to the same parameters in a TestClassSetup as you do in one or more of your test methods right? The way to do this is to state these properties as ClassSetupParameters (only) and then within a TestClassSetup method you can assign them to another "normal" instance property of your test class and then you can access that property within your test method. This looks like the following:
classdef TestWithSharedParameters < matlab.unittest.TestCase
properties(ClassSetupParameter)
categorie=loadmat(1);
classe=loadmat(2);
source=loadmat(3);
donneeRef=loadmat(4);
end
properties
CurrentCategorie;
CurrentClasse;
CurrentSource;
CurrentDonneeRef;
end
methods(TestClassSetup)
function doSomeSetup(testCase, categorie, classe, source, donneeRef)
end
function assignClassParams(testCase, categorie, classe, source, donneeRef)
testCase.CurrentCategorie = categorie;
testCase.CurrentClasses = classe;
testCase.CurrentSource = source;
testCase.CurrentDonneeRef = donneeRef;
end
end
methods(Test)
function testThatUsesClassParams(testCase)
callFunctionUsingParams(...
testCase.CurrentCategorie, ...
testCase.CurrentClasses, ...
testCase.CurrentSource, ...
testCase.CurrentDonneeRef);
end
end
end
As you see you do need to use additional properties, but there are actually some good reasons for that. For one, TestParameters have different semantics than ClassSetupParameters and both of them have different semantics than normal instance properties. However, as you can see, you can always shared the properties across TestClassSetup methods using these instance properties. If you dont want to create another property per parameter you can also just add them to a struct and have a single property to communicate between methods, something like this:
classdef TestWithSharedParameters < matlab.unittest.TestCase
properties(ClassSetupParameter)
categorie=loadmat(1);
classe=loadmat(2);
source=loadmat(3);
donneeRef=loadmat(4);
end
properties
Params = struct;
end
methods(TestClassSetup)
function doSomeSetup(testCase, categorie, classe, source, donneeRef)
end
function assignClassParams(testCase, categorie, classe, source, donneeRef)
testCase.Params.categorie = categorie;
testCase.Params.classes = classe;
testCase.Params.source = source;
testCase.Params.donneeRef = donneeRef;
end
end
methods(Test)
function testThatUsesClassParams(testCase)
callFunctionUsingParams(...
testCase.Params.categorie, ...
testCase.Params.classes, ...
testCase.Params.source, ...
testCase.Params.donneeRef);
end
end
end
Hope that helps!
Best Answer