generateCoberturaReport
Class: matlab.coverage.Result
Namespace: matlab.coverage
Description
generateCoberturaReport(
generates a code coverage report in Cobertura XML format from the coverage results and saves
it using the specified filename. results
,filename
)
By default, the report provides information about line coverage. If you have a MATLAB® Test™ license, you can generate a report that includes both the line and decision (branch) coverage metrics.
Input Arguments
results
— Results of code coverage analysis
matlab.coverage.Result
vector
Results of the code coverage analysis, specified as a
matlab.coverage.Result
vector.
filename
— Name of code coverage report file
string scalar | character vector
Name of the code coverage report file, specified as a string scalar or character
vector ending in .xml
. The value can be a path relative to the
current folder or an absolute path.
Example: "myCoverageReport.xml"
Example: "C:\work\myCoverageReport.xml"
Examples
Generate Cobertura XML Code Coverage Report After Running Tests
Run a suite of tests and collect the code coverage result. Then, generate a Cobertura XML 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 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 a Cobertura XML code coverage report from the coverage result and save it as coverageReport.xml
in your current folder.
result = format.Result;
filename = "coverageReport.xml";
generateCoberturaReport(result,filename)
You can process the generated code coverage report on continuous integration (CI) platforms. You also can view its contents with commands such as open(filename)
or disp(fileread(filename))
.
Tips
To generate a Cobertura XML code coverage report without explicitly collecting the coverage results, create a
CodeCoveragePlugin
instance using aCoberturaFormat
object, and then add the plugin to the test runner.
Version History
Introduced in R2023aR2024b: Produce results for generated C++ code in equivalence tests
If you have a MATLAB
Test license, you can produce code coverage results in Cobertura XML format for
generated C++ code in equivalence tests. To produce the results using the
generateCoberturaReport
method, first run the tests using a
matlabtest.coder.plugins.GeneratedCodeCoveragePlugin
instance created with
a matlab.unittest.plugins.codecoverage.CoverageResult
object.
R2024a: Produce results for generated C code in equivalence tests
If you have a MATLAB
Test license, you can produce code coverage results in Cobertura XML format for
generated C code in equivalence tests. To produce the results using the
generateCoberturaReport
method, first run the tests using a
matlabtest.coder.plugins.GeneratedCodeCoveragePlugin
instance created with
a matlab.unittest.plugins.codecoverage.CoverageResult
object.
R2023b: Generate decision coverage results in Cobertura XML format
If you have a MATLAB
Test license, you can collect information on decision (branch) coverage in addition
to line coverage. To collect decision coverage information, specify the
MetricLevel
name-value argument when you create a plugin using one of
the static methods of the CodeCoveragePlugin
class. For example, run your
tests and generate code coverage results in Cobertura XML format that include both the line
and decision coverage metrics for the source code in a folder.
import matlab.unittest.plugins.CodeCoveragePlugin import matlab.unittest.plugins.codecoverage.CoverageResult suite = testsuite("MyTestClass"); runner = testrunner("textoutput"); format = CoverageResult; plugin = CodeCoveragePlugin.forFolder("myFolder", ... Producing=format,MetricLevel="decision"); runner.addPlugin(plugin) runner.run(suite); results = format.Result; generateCoberturaReport(results,"myReport.xml")
The MetricLevel
argument specifies which coverage types to include
in the results. This table shows the possible values of MetricLevel
and
the corresponding coverage types when reporting results in Cobertura XML format. Results in
Cobertura XML format do not support condition coverage or modified condition/decision
coverage (MC/DC).
Value of MetricLevel | Types of Coverage Included |
---|---|
"statement" (default) | Line coverage |
"decision" , "condition" , or
"mcdc" (requires MATLAB
Test) | Line and decision coverage |
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)