Unit test with file path parameter – improve formatting in PDF report
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:scAppsMatlabR2023B_64sysFastDDSwin64includefastcdrexceptionsException.h’,…
‘C:scAppsMatlabR2023B_64sysjavajrewin64jrebindtplugindeployJava1.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.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:scAppsMatlabR2023B_64sysFastDDSwin64includefastcdrexceptionsException.h’,…
‘C:scAppsMatlabR2023B_64sysjavajrewin64jrebindtplugindeployJava1.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. 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:scAppsMatlabR2023B_64sysFastDDSwin64includefastcdrexceptionsException.h’,…
‘C:scAppsMatlabR2023B_64sysjavajrewin64jrebindtplugindeployJava1.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. unit test, test report, testrunner, plugin, report customization, file paths MATLAB Answers — New Questions