Main Content

generateDOCXReport

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

Generate DOCX test report from test results

Description

example

generateDOCXReport(results) generates a test report from the test results in DOCX format and saves it to a temporary folder.

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

generateDOCXReport(results,fileName) saves the report using the specified file name.

example

generateDOCXReport(___,PageOrientation=orientation) generates a report in the specified orientation. You can specify the orientation as the last argument in any of the previous syntaxes. For example, generateDOCXReport(results,PageOrientation="landscape") generates a test report in landscape orientation.

Input Arguments

expand all

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

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

Example: "myTestReport.docx"

Example: "C:\work\myTestReport.docx"

Report orientation, specified as "portrait" or "landscape". By default, the method generates a report in portrait orientation.

Examples

expand all

Run a suite of tests and then generate a DOCX 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 a DOCX test report from the test results in a temporary folder. By default, the report has portrait orientation.

generateDOCXReport(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\tp1036101c_483e_406a_acce_1f38be5d7a4d.docx

Generate another report in landscape orientation, and save it as myTestReport.docx in your current folder.

generateDOCXReport(results,"myTestReport.docx",PageOrientation="landscape")
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\myTestReport.docx

Open the test report in your current folder.

open("myTestReport.docx")

Limitations

  • A test report generated by the generateDOCXReport 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.unittest.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.unittest.Verbosity
    suite = testsuite("sampleTest");
    runner = testrunner("minimal");
    runner.addPlugin(DiagnosticsRecordingPlugin( ...
        IncludingPassingDiagnostics=true,LoggingLevel=Verbosity.Verbose))
    results = run(runner,suite);
    generateDOCXReport(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