matlab.coverage.Result.generateHTMLReport - Generate interactive HTML report from coverage results - MATLAB (original) (raw)
Class: matlab.coverage.Result
Namespace: matlab.coverage
Generate interactive HTML report from coverage results
Since R2023a
Syntax
Description
generateHTMLReport([results](#mw%5F58d13aa6-143d-4c7e-8e8b-de8aece82c28))
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 reportindex.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.
generateHTMLReport([results](#mw%5F58d13aa6-143d-4c7e-8e8b-de8aece82c28),[folderName](#mw%5Fd15775f1-d533-483c-9b16-834111f047b5))
saves the report to the specified folder.
generateHTMLReport(___,[Name=Value](#namevaluepairarguments))
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.
Input Arguments
Results of the code coverage analysis, specified as amatlab.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 asName1=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 reportindex.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 MetricLevel | Types 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 inresults. When you specify a reporting level using theMetricLevel
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
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 ax^2 + bx + 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 - 4ac)) / (2a); r(2) = (-b - sqrt(b^2 - 4ac)) / (2a);
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.
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
.
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
You can control the highlighting of executables in an interactive code coverage report. By default, the report highlights all the executables using different colors. To turn off highlighting for a group of executables, clear the corresponding check box in theOverall Coverage Summary section of the report. You can control the highlighting for covered, missed, or partially covered executables.
To modify the HTML document title of your code coverage report, specify theDocumentTitle
name-value argument.