Main Content

generateHTMLReport

Class: matlab.coverage.Result
Namespace: matlab.coverage

Generate interactive HTML report from coverage results

Since R2023a

Description

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

An interactive code coverage report lets you customize the displayed information by selecting options in the report. For example, you can choose from the list of included coverage metrics, or control the highlighting for covered or missed executables.

example

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

generateHTMLReport(___,Name=Value) specifies options using one or more name-value arguments in addition to any of the input argument combinations in previous syntaxes. For example, generateHTMLReport(results,MainFile="main.html") generates a code coverage report whose main file is main.html.

filePath = generateHTMLReport(___) returns the absolute path to the main HTML file. Unlike the previous syntaxes, this syntax does not automatically open the generated report.

example

Input Arguments

expand all

Results of the code coverage analysis, specified as a matlab.coverage.Result vector.

Name of the code coverage report folder, specified as a string scalar or character vector. The value can be a path relative to the current folder or an absolute path.

Example: "myCoverageReport"

Example: "C:\work\myCoverageReport"

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: generateHTMLReport(results,MainFile="main.html")

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

Example: MainFile="main.html"

Since R2024a

Title of the HTML document, specified as a string scalar or character vector. The specified title appears in a tab when the code coverage report opens in the browser.

Example: DocumentTitle="My Coverage Report"

Level of coverage metrics to include in the code coverage report, specified as one of the values in this table.

Value of MetricLevelTypes of Coverage Included
"statement"Statement and function coverage

"decision" (requires MATLAB® Test™)

Statement, function, and decision coverage

"condition" (requires MATLAB Test)

Statement, function, decision, and condition coverage

"mcdc" (requires MATLAB Test)

Statement, function, decision, condition, and modified condition/decision coverage (MC/DC)

By default, the method generates a report including all the metrics in results. When you specify a reporting level using the MetricLevel name-value argument, the method includes the metrics up to and including the specified level. For more information about coverage types, see Types of Code Coverage for MATLAB Source Code (MATLAB Test).

Data Types: string | char

Examples

expand all

Run a suite of tests and collect the code coverage result. Then, generate an interactive code coverage report in HTML format from the result.

In a file named quadraticSolver.m in your current folder, create the quadraticSolver function. The function takes as inputs the coefficients of a quadratic polynomial and returns the roots of that polynomial. If the coefficients are specified as nonnumeric values, the function throws an error.

function r = quadraticSolver(a,b,c)
% quadraticSolver returns solutions to the
% quadratic equation a*x^2 + b*x + c = 0.

if ~isa(a,"numeric") || ~isa(b,"numeric") || ~isa(c,"numeric")
    error("quadraticSolver:InputMustBeNumeric", ...
        "Coefficients must be numeric.")
end

r(1) = (-b + sqrt(b^2 - 4*a*c)) / (2*a);
r(2) = (-b - sqrt(b^2 - 4*a*c)) / (2*a);

end

To test the quadraticSolver function, create the SolverTest class in a file named SolverTest.m in your current folder. Define three Test methods that test the function against real solutions, imaginary solutions, and nonnumeric inputs.

classdef SolverTest < matlab.unittest.TestCase
    methods (Test)
        function realSolution(testCase)
            actSolution = quadraticSolver(1,-3,2);
            expSolution = [2 1];
            testCase.verifyEqual(actSolution,expSolution)
        end
        function imaginarySolution(testCase)
            actSolution = quadraticSolver(1,2,10);
            expSolution = [-1+3i -1-3i];
            testCase.verifyEqual(actSolution,expSolution)
        end
        function nonnumericInput(testCase)
            testCase.verifyError(@()quadraticSolver(1,"-3",2), ...
                "quadraticSolver:InputMustBeNumeric")
        end
    end
end

Import the classes used in this example.

import matlab.unittest.plugins.CodeCoveragePlugin
import matlab.unittest.plugins.codecoverage.CoverageResult

Create a test suite from the SolverTest class.

suite = testsuite("SolverTest");

Create a test runner and customize it using a plugin that provides programmatic access to the code coverage information for the source code in the file quadraticSolver.m.

runner = testrunner("textoutput");
format = CoverageResult;
p = CodeCoveragePlugin.forFile("quadraticSolver.m",Producing=format);
runner.addPlugin(p)

Run the tests. After the test run, the Result property of format holds the coverage result. In this example, all the tests pass and the source code receives full coverage.

runner.run(suite);
Running SolverTest
...
Done SolverTest
__________

Generate an interactive code coverage report from the coverage result in a temporary folder. By default, the main file of the report is index.html.

result = format.Result;
generateHTMLReport(result)

Generate another report whose main file is main.html.

filePath = generateHTMLReport(result,MainFile="main.html");

Open main.html.

open(filePath)

Tips

  • To generate an interactive HTML code coverage report without explicitly collecting the coverage results, create a CodeCoveragePlugin instance using a CoverageReport object, and then add the plugin to the test runner.

Version History

Introduced in R2023a

expand all