addCodeCoverage
R2026bClass: matlab.buildtool.tasks.TestTask
Namespace: matlab.buildtool.tasks
Description
enables task = addCodeCoverage(task,results)task to produce the specified code coverage results for the
MATLAB® source code defined in its SourceFiles property. The
method returns the specified task with an updated CodeCoverageResults
property.
Note
To use the addCodeCoverage method, you must first specify your
source code by setting the SourceFiles property of the task.
Input Arguments
Task, specified as a matlab.buildtool.tasks.TestTask object with its
SourceFiles property specifying the source code.
Code coverage results to produce, specified as one of these values:
String vector
Character vector
Cell vector of character vectors
matlab.buildtool.io.FilevectorVector of
matlab.unittest.plugins.codecoverage.CoverageFormatobjects that are supported by the static methods of thematlab.unittest.plugins.CodeCoveragePluginclass (since R2024b)
The task can produce code coverage results in various formats. If you specify
results as a value of type
matlab.buildtool.io.File or convertible to
matlab.buildtool.io.File, then specify formats by using file extensions:
.html— Produce an interactive HTML code coverage report..xml— Produce code coverage results in Cobertura XML format..mat— Save thematlab.coverage.Resultarray to a MAT file.
Specify results as a CoverageFormat vector for
maximum flexibility in producing code coverage results. For example, this build file
contains one task that:
Runs the tests in the current folder and its subfolders and fails the build if any of the tests fail
Produces a standalone HTML code coverage report (requires MATLAB Test™) for the source code in the
mySourcesubfolder of the current folder
function plan = buildfile import matlab.buildtool.tasks.TestTask import matlabtest.plugins.codecoverage.StandaloneReport % Create a plan with no tasks plan = buildplan; % Add a task to run tests and produce coverage results plan("test") = TestTask(SourceFiles="mySource").addCodeCoverage( ... StandaloneReport("report.html")); end
Example: "code-coverage/report.html"
Example: ["code-coverage/results.xml"
"code-coverage/results.mat"]
Example: matlab.unittest.plugins.codecoverage.CoverageReport(MainFile="report.html",DocumentTitle="My
Report")
Since R2026b
Coverage metrics to collect, specified as a string vector, character vector, or cell vector of character vectors. You can specify one or more values from this list:
"statement"— Statement and function coverage"decision"(requires MATLAB Test) — Decision coverage"condition"(requires MATLAB Test) — Condition coverage"mcdc"(requires MATLAB Test) — Modified condition/decision coverage (MC/DC)"type-size"(requires MATLAB Test) — Data type and size coverage
Structural coverage metrics ("statement",
"decision", "condition", and
"mcdc") are hierarchical, meaning each metric level includes all
lower levels. For example, this build file contains one task that:
Runs the tests in the current folder and its subfolders and fails the build if any of the tests fail
Produces an interactive HTML code coverage report, including all the structural coverage metrics, for the source code in the
mySourcesubfolder of the current folder
function plan = buildfile import matlab.buildtool.tasks.TestTask % Create a plan with no tasks plan = buildplan; % Add a task to run tests and produce coverage results plan("test") = TestTask(SourceFiles="mySource").addCodeCoverage( ... "code-coverage/report.html",Metrics="mcdc"); end
For more information about coverage types, see Types of Code Coverage for MATLAB Source Code (MATLAB Test).
By default, if you have a MATLAB Test license, the task collects information about line and decision (branch) coverage with the Cobertura XML format, and all structural coverage with other formats. Otherwise, the task collects information about line coverage with the Cobertura XML format, and statement and function coverage with other formats.
Example: Metrics="decision"
Example: Metrics=["condition" "type-size"]
Output Arguments
Updated task, returned as a matlab.buildtool.tasks.TestTask
object.
Examples
Produce code coverage results in Cobertura XML format by using the addCodeCoverage method.
Open the example and then navigate to the code_coverage_example folder, which contains a build file, a source file named quadraticSolver.m, and a test file named SolverTest.m. For more information about the source and test files used in this example, see Write Simple Test Case Using Classes.
cd code_coverage_exampleThis code shows the contents of the build file. The build file contains one task that:
Runs the tests in the current folder and its subfolders and fails the build if any of the tests fail
Produces code coverage results in Cobertura XML format for the source code in
quadraticSolver.m
function plan = buildfile import matlab.buildtool.tasks.TestTask % Create a plan with no tasks plan = buildplan; % Add a task to run tests and produce coverage results plan("test") = TestTask(SourceFiles="quadraticSolver.m").addCodeCoverage( ... "code-coverage/results.xml"); end
Run the "test" task. The task runs the tests and saves the coverage results to the specified location in your current folder. In this example, all the tests pass, and the task runs successfully.
buildtool test** Starting test
...
Cobertura XML code coverage report has been saved to:
C:\work\ExampleManager\user.examples\matlab-ex23465918\code_coverage_example\code-coverage\results.xml
Test Summary:
Total Tests: 3
Passed: 3
Failed: 0
Incomplete: 0
Duration: 0.035096 seconds testing time.
Code Coverage:
Cobertura: C:\work\ExampleManager\user.examples\matlab-ex23465918\code_coverage_example\code-coverage\results.xml
** Finished test
Build Successful:
1 Task: 0 Failed, 0 Skipped
2.4726 sec total build time
Version History
Introduced in R2024aTo specify the coverage metrics to collect, use Metrics instead of
MetricLevel as an input. Metrics enables you to
collect data type and size coverage (requires MATLAB
Test) in addition to other types of coverage. Data type and size coverage
identifies the data types and sizes of inputs that tests use when calling MATLAB functions and class methods.
Using MetricLevel as an input is not recommended. To update your
code, replace instances of MetricLevel with
Metrics. There are no plans to remove support for
MetricLevel.
If you have a MATLAB
Test license, when you configure a task using the addCodeCoverage
method, the task collects all supported structural coverage metrics by default for easier
access to coverage information. In previous releases, the task collects only a subset of the
supported metrics by default. To collect a different set of metrics, specify the
Metrics input.
To produce customized code coverage results, specify the results
argument as a vector of code coverage formats.
See Also
Functions
Classes
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.
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
Cómo obtener el mejor rendimiento
Seleccione China (en idioma chino o inglés) para obtener el mejor rendimiento. Los sitios web de otros países no están optimizados para ser accedidos desde su ubicación geográfica.
América
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)