Is there a way share TestParameter and ClassSetupParameter properties of unit test class?

8 Ansichten (letzte 30 Tage)
Hello, I have a unit test class called testClass_ARIMA which has parametrized setup and tests methods. My class properties looks like this:
classdef testClass_ARIMA < matlab.unittest.TestCase
properties (ClassSetupParameter)
categorieClass=loadmat(1);
classeClass=loadmat(2);
sourceClass=loadmat(3);
donneeRefClass=loadmat(4);
end
properties (TestParameter)
categorieTest=loadmat(1);
classeTest=loadmat(2);
sourceTest=loadmat(3);
donneeRefTest=loadmat(5);
end
ClassSetupParameter and TestParameters properties are actually identical, but I had to give them different names since otherwise, the code does not run. So I wondered if there is a way to share the same properties between setup and tests methods? Because the problem is that since ClassSetupParameter and TestParameters are considered independant (artificially), when I want to build a test suit matlab tries every possible combination between ClassSetupParameter and TestParameters porperties but there is no need since they should be equal. Consequently, computation takes a lot of time, juste to calculate all possible combinations whoch most of them are useless.
Thanks!

Akzeptierte Antwort

Andy Campbell
Andy Campbell am 30 Nov. 2016
Bearbeitet: per isakson am 6 Dez. 2016
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)
% Do your setup with 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)
% Do your setup with 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!

Weitere Antworten (1)

Yvan Denis
Yvan Denis am 30 Nov. 2016
Yes! thanks! exactly what I've been looking for.
Thanks a lot!

Kategorien

Mehr zu Write Unit Tests finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by