Main Content

generateHTMLReport

Class: matlab.unittest.TestResult
Package: matlab.unittest

Generate HTML test report from test results

Since R2022a

Description

example

generateHTMLReport(results) generates a test report from the test results in HTML format and saves it to a temporary folder. By default, the method names the main file of the report index.html.

Use this method to generate an HTML test report once the test run is complete and the test results are available.

generateHTMLReport(results,folderName) saves the report to the specified folder.

example

generateHTMLReport(___,MainFile=fileName) generates a report with the specified name for the main HTML file. You can specify the file name as the last argument in any of the previous syntaxes. For example, generateHTMLReport(results,MainFile="main.html") generates a test report whose main file is main.html.

Input Arguments

expand all

Results of running a test suite, specified as a matlab.unittest.TestResult array.

Name of the test report folder, specified as a string scalar or character vector. The folderName argument can include a relative path, but the relative path must be in the current folder. Otherwise, folderName must include a full path.

Example: "myTestReport"

Example: "C:\work\myTestReport"

Name of the main HTML file, specified as a string scalar or character vector ending in .html or .htm. If not specified, the method names the main file of the report index.html.

Example: "main.html"

Examples

expand all

Run a suite of tests and then generate an HTML test report from the test results.

Create a function-based test sampleTest.m in your current folder. The file contains two tests that pass and one test that fails.

function tests = sampleTest
tests = functiontests(localfunctions);
end

function testA(testCase)      % Test passes
verifyEqual(testCase,2+3,5)
end

function testB(testCase)      % Test fails
verifyGreaterThan(testCase,13,42)
end

function testC(testCase)      % Test passes
verifySubstring(testCase,"Hello World!","llo")
end

Run the tests in sampleTest.m.

results = runtests("sampleTest");
Running sampleTest
.
================================================================================
Verification failed in sampleTest/testB.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyGreaterThan failed.
    --> The value must be greater than the minimum value.
    
    Actual Value:
        13
    Minimum Value (Exclusive):
        42
    ------------------
    Stack Information:
    ------------------
    In C:\work\sampleTest.m (testB) at 10
================================================================================
..
Done sampleTest
__________

Failure Summary:

     Name              Failed  Incomplete  Reason(s)
    ===============================================================
     sampleTest/testB    X                 Failed by verification.

Generate an HTML test report from the test results in a temporary folder. By default, the main file of the report is index.html.

generateHTMLReport(results)
Generating test report. Please wait.
    Preparing content for the test report.
    Adding content to the test report.
    Writing test report to file.
Test report has been saved to:
 C:\TEMP\tpe2ebe7bd_4a4f_4c9a_8bf7_beeac8713b4c\index.html

Generate another report whose main file is myTestReport.html. Save the report to the subfolder myResults in your current folder.

generateHTMLReport(results,"myResults",MainFile="myTestReport.html")
Generating test report. Please wait.
    Preparing content for the test report.
    Adding content to the test report.
    Writing test report to file.
Test report has been saved to:
 C:\work\myResults\myTestReport.html

Open myTestReport.html.

open(fullfile("myResults","myTestReport.html"))

Limitations

  • A test report generated by the generateHTMLReport method does not include the text output from the Command Window.

Tips

  • When you generate a test report from test results that are created by a default runner, the report includes diagnostics for failing events and messages logged at the matlab.automation.Verbosity.Terse level. To generate a test report that includes diagnostics for passing events or messages logged at different verbosity levels, first customize your test run by adding a DiagnosticsRecordingPlugin instance to the runner. For example, run your tests and generate a test report that includes passing diagnostics and messages logged at all verbosity levels.

    import matlab.unittest.plugins.DiagnosticsRecordingPlugin
    import matlab.automation.Verbosity
    suite = testsuite("sampleTest");
    runner = testrunner("minimal");
    runner.addPlugin(DiagnosticsRecordingPlugin( ...
        IncludingPassingDiagnostics=true,LoggingLevel=Verbosity.Verbose))
    results = run(runner,suite);
    generateHTMLReport(results)
  • To generate a test report without explicitly collecting the test results, customize your test run by adding a TestReportPlugin instance to the test runner.

Version History

Introduced in R2022a