How to change unittest console log

조회 수: 3 (최근 30일)
Jack German
Jack German 2018년 7월 15일
답변: Christian Heigele 2018년 8월 9일
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.

채택된 답변

Andy Campbell
Andy Campbell 2018년 7월 16일
편집: Andy Campbell 2018년 7월 16일
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

추가 답변 (1개)

Christian Heigele
Christian Heigele 2018년 8월 9일
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.

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by