matlab.unittest.plugins.codecoverage.CoverageReport - Format for interactive HTML code coverage report - MATLAB (original) (raw)
Namespace: matlab.unittest.plugins.codecoverage
Superclasses: matlab.unittest.plugins.codecoverage.CoverageFormat
Format for interactive HTML code coverage report
Description
The matlab.unittest.plugins.codecoverage.CoverageReport
class provides a way to generate interactive code coverage reports in HTML format. To generate a code coverage report in this format, create a CodeCoveragePlugin instance using a CoverageReport
object, and then add the plugin to the test runner.
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.
Creation
Description
format = matlab.unittest.plugins.codecoverage.CoverageReport
creates a CoverageReport
object that instructsCodeCoveragePlugin
to generate an interactive report in HTML format and save it to a temporary folder. By default, the main file of the report is index.html
.
format = matlab.unittest.plugins.codecoverage.CoverageReport([folderName](#mw%5Feac8654b-8c8f-4884-b476-2c913716d850))
specifies the name of the code coverage report folder.
format = matlab.unittest.plugins.codecoverage.CoverageReport(___,[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,format = matlab.unittest.plugins.codecoverage.CoverageReport("MainFile","main.html")
creates a CoverageReport
object for generating a code coverage report whose main file is main.html
.
Input Arguments
folderName
— Name of code coverage report folder
string scalar | character 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. If the folder does not exist, CoverageReport
creates it.
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: format = matlab.unittest.plugins.codecoverage.CoverageReport(MainFile="main.html")
Before R2021a, use commas to separate each name and value, and enclose Name
in quotes.
Example: format = matlab.unittest.plugins.codecoverage.CoverageReport("MainFile","main.html")
MainFile
— Name of main HTML file
string scalar | character vector
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 plugin names the main file of the reportindex.html
.
This argument sets the [MainFile](matlab.unittest.plugins.codecoverage.coveragereport-class.html#mw%5Fed7f66a6-4ca1-4e8b-aeb3-4419757c7db3)
property.
Example: MainFile="main.html"
DocumentTitle
— Title of HTML document
string scalar | character vector
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"
Properties
MainFile
— Name of main HTML file
'index.html'
(default) | character vector
Name of the main HTML file, returned as a character vector ending in.html
or .htm
. By default, the main file of the report is index.html
.
This property is set by the MainFile name-value argument.
Example: 'main.html'
Attributes:
GetAccess | public |
---|---|
SetAccess | immutable |
Examples
Generate Interactive Code Coverage Report
Run a suite of tests and generate an interactive code coverage report in HTML format for your source code.
In a folder named sourceFolder
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 folder named testsFolder
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
To run the tests and generate a code coverage report, first add sourceFolder
to the path.
Create a test suite from testsFolder
.
suite = testsuite("testsFolder");
Create a test runner and customize it using a plugin that generates an interactive code coverage report for the code in sourceFolder
. Specify that the plugin writes its output to a folder named coverageReport
in your current folder.
import matlab.unittest.plugins.CodeCoveragePlugin import matlab.unittest.plugins.codecoverage.CoverageReport runner = testrunner("textoutput"); reportFormat = CoverageReport("coverageReport"); p = CodeCoveragePlugin.forFolder("sourceFolder","Producing",reportFormat); runner.addPlugin(p)
Run the tests. In this example, all the tests pass and the source code receives full coverage. The plugin generates an interactive code coverage report in the specified folder coverageReport
, created in your current folder. By default, the main file of the report is index.html
.
results = runner.run(suite);
Running SolverTest ... Done SolverTest
MATLAB code coverage report has been saved to: C:\work\coverageReport\index.html
Open the main file of the report.
open(fullfile("coverageReport","index.html"))
Version History
Introduced in R2019a
R2024a: Control highlighting for covered or missed executables
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.
R2024a: Modify HTML document title
To modify the HTML document title of your code coverage report, specify theDocumentTitle
name-value argument.
R2022a: Collect statement and function coverage information for your source code
When you generate an interactive HTML code coverage report using theCoverageReport
class, the report provides information about statement and function coverage:
- Use statement coverage to determine whether every MATLAB® statement in your source code is executed at least once.
- Use function coverage to determine whether every function in your source code is called at least once.
In previous releases, you can access only line coverage information for your source code. Compared to line coverage, statement and function coverage provide a more detailed analysis of the source code covered by the tests.
R2020a: Recommended over ProfileReport
The ProfileReport
class will be removed. UseCoverageReport
or CoberturaFormat instead. The recommended classes result in more accurate code coverage reports.
To create a code coverage report without specifying a format, see Profile Your Code to Improve Performance.