How to change unittest console log
Show older comments
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.
Accepted Answer
More Answers (1)
Christian Heigele
on 9 Aug 2018
0 votes
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.
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!