MATLAB: In Matlab unittesting how to modify a property after test suite is created

ooptestparamterunit testunittest

I would like to modify a property (not a TestParameter) after the test suite is created but I am getting an error "No public field myparam exists for class matlab.unittest.Test". Here is what I am doing in terms of code: I create a class unitclass.m
classdef unitclass < matlab.unittest.TestCase
properties ( SetAccess = public )
myparam = 2;
end
properties (TestParameter)
test_param1 = struct('Low', 0.1,'Medium', 0.5);
test_param2 = struct('Cold', 10,'Hot', 200);
end
methods (Test)
function testError(testcase,test_param1,test_param2)
output = test_param1 * test_param2;
testcase.myparam
testcase.verifyLessThan(output,20);
end
end
end
Then a create a custom unittest SDLTest.m class to modify my parameter
classdef SDLTest < matlab.unittest.Test
methods (Static = true)
function this = set_myparam(this,myparam)
for i = 1:length(this)
this(i).myparam = myparam;
end
end
end
end
when I try to execute the code
mySuite = matlab.unittest.TestSuite.fromClass(?unitclass)
results = SDLTest.set_myparam(mySuite,1)
I get the error "No public field myparam exists for class matlab.unittest.Test".
Help with this is greatly appreciatd.
Thanks.

Best Answer

Hello Nadjib,
The reason you can't change the property like you were trying to do is becase matlab.unittest.Test and matlab.unittest.TestCase are not in the same class hierarchy. The Test arrays are what give the TestRunner the knowledge of how to run the tests defined in the TestCase classes, but the runner constructs the TestCase classes during the test run.
It sounds to me like you may be interested in the log method of TestCase which was included in the framework in R2014b.
Using this feature, if you write a " PlotDiagnostic " similar to the one shown in this post , you could generate the plots only when you run the test above a certain verbosity threshold. This would look like:
classdef unitclass < matlab.unittest.TestCase
properties (TestParameter)
test_param1 = struct('Low', 0.1,'Medium', 0.5);
test_param2 = struct('Cold', 10,'Hot', 200);
end
methods (Test)
function testError(testcase,test_param1,test_param2)
output = test_param1 * test_param2;
testcase.log(3, PlotDiagnostic(output)); % can be another verbosity
testcase.verifyLessThan(output,20);
end
end
end
Then when could run it as follows:
>> % run it normally (& easily)
>> runtests('unitclass')
>>
>> % create the suite and run it at a higher verbosity level to show the plots
>> import matlab.unittest.TestSuite
>> import matlab.unittest.TestRunner
>>
>> suite = TestSuite.fromClass(?unitclass)
>> runner = TestRunner.withTextOutput('Verbosity', 3)
>> runner.run(suite);
Here are a few relevant links of interest for you:
Does that help?