matlab.coverage.Result Class
Namespace: matlab.coverage
Description
An object of the matlab.coverage.Result
class provides the result of code
coverage analysis for a source file. The file can contain MATLAB® code or C/C++ code generated with MATLAB
Coder™.
You do not need to create objects of the matlab.coverage.Result
class
directly. To collect code coverage information and access the results, create a CodeCoveragePlugin
instance using a CoverageResult
object, and add the plugin to the test runner. After the test run, the
Result
property of the CoverageResult
object holds the
coverage results as an array of matlab.coverage.Result
objects. Each element of
the array provides information about one of the files in your source code that was covered by
the tests.
Properties
Filename
— Name of the source file corresponding to the result
string scalar
Name of the source file corresponding to the coverage result, returned as a string scalar. The testing framework sets the property to the full path to the file when creating the coverage result.
Attributes:
GetAccess | public |
SetAccess | private |
CreationDate
— Date that result was created
datetime
scalar
Date that the coverage result was created, returned as a datetime
scalar.
Attributes:
GetAccess | public |
SetAccess | private |
Filter
— Coverage filter applied to result
matlabtest.coverage.Justification
array
Since R2024b
Coverage filter applied to the coverage result, returned as a
matlabtest.coverage.Justification
array. You must have a MATLAB
Test™ license to use this property.
Attributes:
GetAccess | public |
SetAccess | protected |
Methods
Public Methods
coverageSummary | Retrieve information from coverage results |
generateCoberturaReport | Generate Cobertura XML report from coverage results |
generateHTMLReport | Generate interactive HTML report from coverage results |
generateStandaloneReport (MATLAB Test) | Generate standalone report from coverage results |
applyFilter (MATLAB Test) | Apply filters to coverage results |
resetFilter (MATLAB Test) | Remove filters from coverage results |
Specialized Operators and Functions
These methods specialize standard MATLAB operators and functions for objects of this class.
+ | Return the set union of the left and right operands. The returned value contains the total aggregated coverage for two tests. |
- | Return the set difference between the left and right operands. The
returned value contains the coverage satisfied by the left operand, but not the
right operand. Use |
* and .* | Return the set intersection between the left and right operands. The
returned value contains only the coverage satisfied by both operands. Use
|
Examples
Access Information About Statement Coverage
Run a suite of tests and collect the code coverage result. Then, retrieve information about statement coverage 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.
runner.run(suite);
Running SolverTest ... Done SolverTest __________
Access the statement coverage summary. The returned vector indicates that all four statements in the source code were executed by the tests.
result = format.Result;
summary = coverageSummary(result,"statement")
summary = 1×2
4 4
Use the description of the code coverage analysis to retrieve the execution count for each statement.
[~,description] = coverageSummary(result,"statement")
description = struct with fields:
statement: [1×4 struct]
disp([description.statement.ExecutionCount])
3 1 2 2
Version History
Introduced in R2023aR2024b: Justify missing code coverage
If you have a MATLAB Test license, you can:
Apply filters to coverage results using the
applyFilter
method.Remove filters from coverage results using the
resetFilter
method.Access the filters applied to coverage results using the
Filter
property.
R2024a: Generate standalone report from coverage results
If you have a MATLAB
Test license, you can use the generateStandaloneReport
method to
generate a standalone report as a single HTML file from the coverage results.
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 (한국어)