matlab.unittest.plugins.codecoverage.CoberturaFormat - Format for Cobertura XML code coverage report - MATLAB (original) (raw)
Main Content
Namespace: matlab.unittest.plugins.codecoverage
Superclasses: matlab.unittest.plugins.codecoverage.CoverageFormat
Format for Cobertura XML code coverage report
Description
The matlab.unittest.plugins.codecoverage.CoberturaFormat
class provides a way to generate code coverage reports in Cobertura XML format. To generate a code coverage report in this format, create a CodeCoveragePlugin instance using aCoberturaFormat
object, and then add the plugin to the test runner.
Creation
Description
format = matlab.unittest.plugins.codecoverage.CoberturaFormat([filename](#mw%5F7ee10c24-6cb9-4584-9e72-5e27d6292c80))
creates a CoberturaFormat
object that instructsCodeCoveragePlugin
to generate a report in Cobertura XML format and save it using the specified filename.
Input Arguments
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 relative path, but the relative path must be in the current folder. Otherwise, the value must be a full path.
Example: "myCoverageReport.xml"
Example: "C:\work\myCoverageReport.xml"
Examples
Generate Code Coverage Report in Cobertura XML Format
Run a suite of tests and generate a code coverage report in Cobertura XML format for your source code.
In a file 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 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
Create a test suite from the SolverTest
class.
suite = testsuite("SolverTest");
Create a test runner and customize it using a plugin that generates a Cobertura XML code coverage report for the source code in the file quadraticSolver.m
. Specify that the plugin writes its output to a file named coverageReport.xml
in your current folder.
import matlab.unittest.plugins.CodeCoveragePlugin import matlab.unittest.plugins.codecoverage.CoberturaFormat runner = testrunner("textoutput"); sourceCodeFile = "quadraticSolver.m"; reportFile = "coverageReport.xml"; reportFormat = CoberturaFormat(reportFile); p = CodeCoveragePlugin.forFile(sourceCodeFile,"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 a Cobertura XML code coverage report in your current folder.
results = runner.run(suite);
Running SolverTest ... Done SolverTest
You can process the generated code coverage report on continuous integration (CI) platforms. You also can view its contents with commands such as open(reportFile)
or disp(fileread(reportFile))
.
Version History
Introduced in R2017b
R2024b: 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, run the tests using amatlabtest.coder.plugins.GeneratedCodeCoveragePlugin
instance created with a matlab.unittest.plugins.codecoverage.CoberturaFormat
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, run the tests using amatlabtest.coder.plugins.GeneratedCodeCoveragePlugin
instance created with a matlab.unittest.plugins.codecoverage.CoberturaFormat
object.
R2023b: Collect 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 theMetricLevel
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.CoberturaFormat
suite = testsuite("MyTestClass"); runner = testrunner("textoutput"); format = CoberturaFormat("myReport.xml"); plugin = CodeCoveragePlugin.forFolder("myFolder", ... Producing=format,MetricLevel="decision"); runner.addPlugin(plugin) results = runner.run(suite);
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 collecting 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 |
R2020a: Recommended over ProfileReport
The ProfileReport
class will be removed. Use CoverageReport orCoberturaFormat
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.