addCodeCoverage
Class: matlab.buildtool.tasks.TestTask
Namespace: matlab.buildtool.tasks
Description
task = addCodeCoverage(
enables 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
— Task
matlab.buildtool.tasks.TestTask
object
Task, specified as a matlab.buildtool.tasks.TestTask
object with its
SourceFiles
property specifying the source code.
results
— Code coverage results to produce
string vector | matlab.buildtool.io.File
vector | matlab.unittest.plugins.codecoverage.CoverageFormat
vector | ...
Code coverage results to produce, specified as one of these values:
String vector
Character vector
Cell vector of character vectors
matlab.buildtool.io.File
vectorVector of
matlab.unittest.plugins.codecoverage.CoverageFormat
objects that are supported by the static methods of thematlab.unittest.plugins.CodeCoveragePlugin
class (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.Result
array 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
mySource
subfolder 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")
level
— Level of coverage metrics
"statement"
(default) | "decision"
| "condition"
| "mcdc"
Level of coverage metrics to include in the code coverage results, specified as one of the values in this table. By default, the task collects information about line coverage with the Cobertura XML format, and statement and function coverage with other formats.
Value of level | Types of Coverage Included | |
---|---|---|
Cobertura XML Format | Other Formats | |
"statement" | Line coverage | Statement and function coverage |
| Line and decision coverage | Statement, function, and decision coverage |
| Line and decision coverage | Statement, function, decision, and condition coverage |
| Line and decision coverage | Statement, function, decision, condition, and modified condition/decision coverage (MC/DC) |
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 supported coverage metrics, for the source code in the
mySource
subfolder 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",MetricLevel="mcdc"); end
For more information about coverage types, see Types of Code Coverage for MATLAB Source Code (MATLAB Test).
Data Types: char
| string
Examples
Produce Code Coverage Results in Cobertura XML Format
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_example
This 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 ... Test Summary: Total Tests: 3 Passed: 3 Failed: 0 Incomplete: 0 Duration: 0.025416 seconds testing time. Code Coverage: Cobertura: code-coverage\results.xml ** Finished test
Version History
Introduced in R2024aR2024b: Produce results using coverage formats
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.
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 (한국어)