Unit test with file path parameter – improve formatting in PDF report
Show older comments
I am relatively new to MATLAB and have a question about generating test reports.
I am working with a class-based unit test that takes a file path as a parameter. These file paths can become quite long. The default presentation of test results using
table(results)
is rather unsatisfactory.
Therefore, I wrote a custom
TestRunnerPlugin
that produces a nicer output on the command line – in particular, the file paths are displayed correctly and in full.
For the user of the unit test, it is crucial to know exactly which file was used when calling the test. This makes a clear and readable formatting of the file paths very important.
My question: How can I achieve the same improved output in the generated PDF report?
Attached is a complete minimal example:
- ModuleTest provides two test functions.
- Wrapper.m performs the test execution and result output.
- MyPlugin.m adjusts the command-line output at the end of the tests.
Wrapper.m
import matlab.unittest.TestSuite;
import matlab.unittest.TestRunner;
import matlab.unittest.plugins.TestReportPlugin;
runner = TestRunner.withNoPlugins;
%for custom command line output
runner.addPlugin(MyPlugin);
%for pdf report
pdfFile = 'TestReport.pdf';
plugin = TestReportPlugin.producingPDF(pdfFile);
runner.addPlugin(plugin);
suite = TestSuite.fromClass(?ModuleTest);
results = runner.run(suite);
%standard command line output
table(results)
classdef ModuleTest < matlab.unittest.TestCase
properties (TestParameter)
file = {'C:\scApps\Matlab\R2023B_64\sys\FastDDS\win64\include\fastcdr\exceptions\Exception.h',...
'C:\scApps\Matlab\R2023B_64\sys\java\jre\win64\jre\bin\dtplugin\deployJava1.dll'};
end
methods(Test)
function testFuncA(tc, file)
tc.verifyEqual(isfile(file), fileExists(file));
end
function testFuncB(tc, file)
tc.verifyEqual(~isfile(file), fileExists(file));
end
end
end
classdef MyPlugin < matlab.unittest.plugins.TestRunnerPlugin
methods (Access=protected)
function reportFinalizedSuite(plugin,pluginData)
disp('### Test Results ###');
for i=1:numel(pluginData.TestResult)
thisResult = pluginData.TestResult(i);
if thisResult.Passed
status = 'PASSED';
elseif thisResult.Failed
status = 'FAILED';
elseif thisResult.Incomplete
status = 'SKIPPED';
end
%Obtain name of the test
parts = split(thisResult.Name, '/');
testName = parts{end};
testName = erase(testName, regexp(testName, '\(.*\)', 'match'));
%Parameter array, each element has the fileds Name, Property, Value
params = pluginData.TestSuite(i).Parameterization;
%% extract information from the test
% file
selection = arrayfun(@(x) strcmp(x.Property, 'file'), params);
file = params(selection).Value;
fprintf('%s: %s in %f seconds. Test configuration: file %s.\n', testName, status, thisResult.Duration, file);
end
disp('### Test Results ###');
reportFinalizedSuite@ ...
matlab.unittest.plugins.TestRunnerPlugin(plugin,pluginData);
end
end
end
Below you can see a direct comparison between the modified output with correct file paths and the hard-to-read default output

My main concern is the formatting of file paths in the generated test report. Currently, long paths are truncated and not displayed correctly, which makes it difficult to see which file was actually used.
Similar to how I already customized the command line output with a plugin, I’d like to apply the same kind of adjustment to the PDF test report so that the full paths are shown clearly and in a readable format.


2 Comments
I don't have the TB and the above code doesn't run here; something in the classdef breaks so that's not possible.
The table output above looks like the table has been created internally with a maximum file string length; at the command line the default presentation of the table doesn't appear to truncate a cellstring; it just causes the window scroll bar to be displayed. It then appears the pdf file content is truncated because the table data were.
The only guess I would have would be if you can somewhere set a maximum column width that is long enough (although that looks to be going to create extremely long lines and any report is going to be very difficult to format on a single line which a table would require). Since it appears the internals are built around using a table, changing that is probably not going to be feasible(*).
I would also guess that even if could make that column width wide enough that it will, with very long pathnames end up conflicting with the page formatting for margins and all that.
Given that there's a relatively small user base for the publishing TB, I'd suggest your better hope for any resolution will be to submit this to Mathworks as an official support request/bug at <Product Support Page>.
(*) The one user hack I can think of would be if the package is in m-files one might just be able to find where that limit is internally and create an aliased user copy of the function with a bigger number there.
dpb
on 18 Sep 2025
Could you not go ahead and create a table or other data structure in the plugin code and then write the revised output to file there? It wouldn't be in pdf form, but could be exported to a spreadsheet for viewing in its entirety.
I don't know that you then couldn't publish that file independently to get into the pdf format instead of trying to beat on the apparent limitations?
Answers (0)
Categories
Find more on Write Unit Tests 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!