Customise test report generated by matlab.unittest.TestCase.generatePDFReport method
10 views (last 30 days)
Show older comments
Hi,
I am using matlab.unitTest.TestCase.generatePDFReport method to generate my testReport. I see that the report contains the failure/pass or some diagnostics from the command window only. I want to add some piecharts or some messages to the report, is it possible?
Thanks in advance.
0 Comments
Accepted Answer
T.Nikhil kumar
on 13 Feb 2024
Hello Harish,
As of today, I think the ‘generatePDFReport’ method in MATLAB does not support the inclusion of custom content such as pie charts or additional messages within the generated PDF report.
I would suggest you to try creating your own reporting mechanism by leveraging the MATLAB Report Generator.
You could obtain the results of the tests in the form of ’matlab.unittest.TestResult’ array using the ‘runtests’ function. Based on these test results, create pie charts using MATLAB's plotting functions. You can also prepare custom messages you wish to include in the report. You can then save these figures as image files.
You can now use MATLAB Report Generator ('mlreportgen.report.Report'), to create a custom PDF report. Please go through the content generation page to learn about adding figures and messages to your report. Refer to the below example of adding a figure and custom message to your report:
% Generate your pie chart
figure;
pie([0.5 0.25 0.25])
%saving figure as image
saveas(gcf, 'testResultsPieChart.png');
% Create a custom report
import mlreportgen.report.*;
rpt = Report('MyTestReport', 'pdf');
% Add a title page and table of contents
titlepg = TitlePage;
titlepg.Title = 'My Test Results';
titlepg.Author = 'Your Name';
add(rpt, titlepg);
add(rpt, TableOfContents);
% Add custom messages or results
add(rpt, Paragraph('Some custom message'));
% Add the pie chart to the report
fig = Image('testResultsPieChart.png');
fig.Width = '4in';
fig.Height = '3in';
add(rpt, fig);
% Close and view the report
close(rpt);
rptview(rpt);
Refer to the following documentation to understand more about the functions mentioned above:
generatePDFReport - https://www.mathworks.com/help/matlab/ref/matlab.unittest.testresult.generatepdfreport.html
Content Generation in MATLAB Report Generator - https://www.mathworks.com/help/rptgen/content-generation.html
Examples of Tasks with formatting content - https://www.mathworks.com/help/rptgen/matlab-report-generator-task-examples.html
Hope this helps you proceed further!
More Answers (1)
Mattia Marsetti
on 14 Mar 2024
Hi Harish
You can use the FigureDiagnostic class to add a custom artifact that will print together with the test as in the following example, where I have defined a simple test class, with a unique test method which will always fail and report a figure as diagnostic.
Please note that instead of "fdiag" you can have a different diagnostic or a simple text that will be reported in the final pdf report. See documentation here: https://it.mathworks.com/...diagnostic
classdef myTestCase < matlab.unittest.TestCase
methods(Test)
function test_with_diag_figure(testCase)
f=figure;
spy;% plot your custom chart here
fdiag=FigureDiagnostic(f,'Formats',{'png'},'Prefix','LoggedFigure_');
testCase.log(1,fdiag);% if you want to always log the figure in the test, even if it passes
testCase.verifyLessThan(1,0,fdiag);% this will add the figure as a detailed diag in case of failure
end
end
end
Is it what you where looking for?
0 Comments
See Also
Categories
Find more on Manual Performance Optimization 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!