Main Content

generateHTMLReport

Class: matlab.coverage.Result
Package: matlab.coverage

Generate HTML report from coverage results

Since R2023a

Description

example

generateHTMLReport(results) generates a 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.

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.

example

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

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 relative path, but the relative path must be in the current folder. Otherwise, the value must be a full 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 not specified, the method names the main file of the report index.html.

Example: MainFile="main.html"

Highest 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 only the metrics up to and including the specified level. If an expected metric does not exist in results, then MATLAB throws an error. For more information about coverage types, see Types of Code Coverage for MATLAB Source Code (MATLAB Test).

Data Types: char | string

Examples

expand all

Run a suite of tests and collect the code coverage result. Then, generate an HTML code coverage report 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 roots = 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

roots(1) = (-b + sqrt(b^2 - 4*a*c)) / (2*a);
roots(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 HTML 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 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