HTML format for code coverage report
matlab.unittest.plugins.codecoverage.CoverageReport class provides
a way to generate 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
reportFormat = matlab.unittest.plugins.codecoverage.CoverageReport
CoverageReport object that instructs
CodeCoveragePlugin to generate a report in HTML format and
save it to a temporary folder. By default, the main file of the report is
reportFormat = matlab.unittest.plugins.codecoverage.CoverageReport(
specifies the name of the code coverage report folder.
reportFormat = matlab.unittest.plugins.codecoverage.CoverageReport(___,"MainFile",
MainFile property to the specified name. You can specify the
file name as the last argument in any of the previous syntaxes. For example,
CoverageReport object with its
MainFile property set to
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
folderName argument can
include a relative path, but the relative path must be in the current
folderName must include a full
path. If the folder does not exist,
MainFile — Name of main HTML file
'index.html' (default) | character vector
Name of the main HTML file, returned as a character vector ending in
.htm. If not set, the
main file of the report is
You can specify the value of this property during construction as a string scalar or character vector.
Generate Code Coverage Report in HTML Format
Run a suite of tests and generate a 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 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 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
suite = testsuite("testsFolder");
Create a test runner and customize it using a plugin that generates an HTML code coverage report for the code in
sourceFolder. Instruct the plugin to write 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"); sourceCodeFolder = "sourceFolder"; reportFolder = "coverageReport"; reportFormat = CoverageReport(reportFolder); p = CodeCoveragePlugin.forFolder(sourceCodeFolder,"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 HTML code coverage report in the specified folder
coverageReport, created in your current folder. By default, the main file of the report is
results = runner.run(suite);
Running SolverTest ... Done SolverTest __________ Code coverage report has been saved to: C:\work\coverageReport\index.html
Open the main file of the report.
Version HistoryIntroduced in R2019a
R2022a: Collect statement and function coverage metrics for your source code
When you generate an HTML code coverage report using the
CoverageReport class, the report displays statement and function
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 generate only line coverage metrics 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.