MATLAB: How to change unittest console log

logunittest

hi, I've got a parametrized test (using a Test Class) which produces the following text when an assertion fails: p1 and p2 are cell arrays in the ClassSetupParameter properties section.
properties(ClassSetupParameter)
p2 = {{'foo',1},{'foo',2},{'bar',1},{'bar',2},{'bar',3}};
p1 = {'a', 'b'};
end
When I run a test suite based on this class
suite = matlab.unittest.TestSuite.fromClass(?myTest)
res = runner.run(suite);
I get this text which references the current cell in p1.
Assertion failed while setting up or tearing down myTest[p1=a,p2=value1].
As a result, all myTest[p1=a,p2=value1] tests failed and did not run to
completion.
How do I edit this text? I'd like to get rid of the text 'value6' and replace with a concatenated value of both the cells, i.e: something like this
Assertion failed while setting up or tearing down myTest[p1=a,p2=foo-1].
As a result, all myTest[p1=a,p2=foo-1] tests failed and did not run to
completion.
or
Assertion failed while setting up or tearing down myTest[p1=a,p2=bar-3].
As a result, all myTest[p1=a,p2=bar-3] tests failed and did not run to
completion.

Best Answer

Hi Jack,
You can do this by using the struct syntax to define the parameters, which allows you to give a label to the parameter value as the struct field. This looks like the following:
properties(ClassSetupParameter)
p2 = struct('foo_1', {{'foo',1}}, 'foo_2', {{'foo',2}},...
'bar_1', {{'bar',1}}, 'bar_2', {{'bar',2}}, 'bar_3', {{'bar',3}});
p1 = {'a', 'b'};
end
Note that if your data is in cell arrays you need to use the "double" cell array syntax when making it part of a struct. Alternatively you can do this the following way if you prefer:
classdef tfoo < matlab.unittest.TestCase
properties(ClassSetupParameter)
p2 = createP2;
p1 = {'a', 'b'};
end
%...
end % classdef
% at the bottom of the file define the local function
function p2 = createP2
p2.foo_1 = {'foo',1};
p2.foo_1 = {'foo',2};
p2.bar_1 = {'bar',1};
p2.bar_2 = {'bar',2};
p2.bar_3 = {'bar',3};
end
Also, cell arrays of strings are a special case where we can usually do a pretty good job of showing the label as just the string itself, although if the string is not a valid variable name the label is modified to become one.
Hope that helps! Andy