How to dynamically set method/class setup parameters

16 visualizaciones (últimos 30 días)
Jack German
Jack German el 11 de Jul. de 2018
Comentada: Steven Lord el 12 de Jul. de 2018
hi,
I'm trying to write a parameterized test where one class setup parameter depends on another one. I'm running it from a script.
myScript.m:
suite = matlab.unittest.TestSuite.fromClass(?myTest)
runner = matlab.unittest.TestRunner.withTextOutput();
results = runner.run(suite);
The test class is defined in a seperate file:
classdef myTest < matlab.unittest.TestCase
properties(ClassSetupParameter)
prop1 = foo();
prop2 = {1,2,3};
prop3 = bar(prop1);
end
end
function foo_result = foo()
foo_result = {3,4,5};
end
function bar_result = bar(prop1)
bar_result = prop1 -1;
end
I'm getting an error:
Error using myScript (line 1)
Invalid default value for property 'prop3' in class 'myTest':
Undefined function or variable 'prop1'.
I have also tried seperating prop1 and prop3 to Class and Method setup parameters:
classdef myTest < matlab.unittest.TestCase
properties(ClassSetupParameter)
prop1 = foo();
prop2 = {1,2,3};
end
properties(MethodSetupParameter)
prop3 = bar(prop1);
end
end
function foo_result = foo()
foo_result = {3,4,5};
end
function bar_result = bar(prop1)
bar_result = prop1 -1;
end
And I'm getting the same error
Any ideas how to overcome this? I really don't want to have another loop inside any test method.
I'm on Matlab 2017a
Thank you! Jack

Respuesta aceptada

Steven Lord
Steven Lord el 11 de Jul. de 2018
Try this:
classdef myTest < matlab.unittest.TestCase
properties(ClassSetupParameter)
prop1 = foo();
end
properties
prop3;
end
methods(TestClassSetup)
function setup_prop3(testcase, prop1)
testcase.prop3 = computeProp3FromProp1(prop1);
end
end
methods(Test)
function displayProp3(testcase)
testcase.log(sprintf('The value of prop3 is %d.\n', testcase.prop3));
end
end
end
function y = foo()
y = {3, 4, 5};
end
function y = computeProp3FromProp1(p)
y = p-1;
end
To see that the prop3 property takes on the values 2, 3, and 4 run this with concise logging.
suite = matlab.unittest.TestSuite.fromClass(?myTest)
runner = matlab.unittest.TestRunner.withTextOutput();
L = matlab.unittest.plugins.LoggingPlugin.withVerbosity(matlab.unittest.Verbosity.Concise);
runner.addPlugin(L);
results = runner.run(suite)
  7 comentarios
Jack German
Jack German el 12 de Jul. de 2018
I'm trying to accomplish the second option. While prop1 is (say) 1 prop3 will change from 1 to a number that can be determined from prop1.
Steven Lord
Steven Lord el 12 de Jul. de 2018
Rather than using two separate properties in that case I'd probably assemble a cell array of vectors, the first element of each vector being the value of prop1 and the second prop3, and use that cell array as the property.
prop13 = {[1 1], [1 2], [2 1], [2 2], [2 3]};

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Run Unit Tests en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by