How to change unittest console log

6 views (last 30 days)
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.
p2 = {{'foo',1},{'foo',2},{'bar',1},{'bar',2},{'bar',3}};
p1 = {'a', 'b'};
When I run a test suite based on this class
suite = matlab.unittest.TestSuite.fromClass(?myTest)
res =;
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
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
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

Accepted Answer

Andy Campbell
Andy Campbell on 16 Jul 2018
Edited: Andy Campbell on 16 Jul 2018
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:
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'};
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
p2 = createP2;
p1 = {'a', 'b'};
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};
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

More Answers (1)

Christian Heigele
Christian Heigele on 9 Aug 2018
I had the same problem, and since the creation of those test names is not injectable, I did a nasty workaround:
This whole test-name is completely decoupled from the data that is provided with each test-case. Hence you can just overwrite / shadow the method that creates those test-names.
The parameter-part of those test-names is created in matlab.unittest.internal.getParameterNameString
So if you create a method within package-folders in your path that matches this method name, you'll just shadow the original implementation.
There I switched out the paramNames line with: paramNames = arrayfun(@(p) transformThingToString(p.Value), params, 'UniformOutput', false);
And transformThingToString is a method that is able to translate an arbitrary object to something "displayable". Arrays / strings / cells / char-arrays are trivial, most of our classes have an toString-extension, function handles are translated with func2str.
Not nice, not stable, but because we don't switch out the matlab version on our test-agents all the time, this is working fine for us.

Community Treasure Hunt

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

Start Hunting!

Translated by