generateStandaloneReport
R2026bSyntax
Description
generateStandaloneReport( generates a
standalone code coverage report as a single HTML file from the coverage results. The method
saves the report using an autogenerated filename to a temporary folder.results)
A standalone code coverage report is not interactive and contains hard-coded
information. To generate an interactive code coverage report, use the generateHTMLReport method instead.
generateStandaloneReport(___,Metrics=
specifies the code coverage metrics to include in the standalone report.metrics)
filePath = generateStandaloneReport(___) returns the
absolute path to the report file. Unlike the previous syntaxes, this syntax does not
automatically open the generated report.
Input Arguments
Results of the code coverage analysis, specified as a matlab.coverage.Result vector.
Name of the standalone report file, specified as a string scalar or character vector
ending in .html. The value can be a path relative to the current
folder or an absolute path.
Example: "myCoverageReport.html"
Example: "C:\work\myCoverageReport.html"
Since R2026b
Coverage metrics to include in the standalone report, 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"— Decision coverage"condition"— Condition coverage"mcdc"— Modified condition/decision coverage (MC/DC)"type-size"— Data type and size coverage
Structural coverage metrics ("statement",
"decision", "condition", and
"mcdc") are hierarchical, meaning each metric level includes all
lower levels. For more information about coverage types, see Types of Code Coverage for MATLAB Source Code.
By default, the method generates a report that includes all the metrics in
results.
Example: Metrics="mcdc" (all structural coverage)
Example: Metrics=["statement" "type-size"] (statement, function,
and data type and size coverage)
Example: Metrics=["mcdc" "type-size"] (all structural coverage
and data type and size coverage)
Examples
Run a suite of tests and collect the code coverage results. Then, generate a standalone code coverage report as a single HTML file from the coverage results.
Open the example to access the required files and folders. When you open the example, the tests subfolder of your current folder contains tests defined in the BankAccountTest.m and DocPolynomTest.m files. The source subfolder contains the source code required by the tests.
BankAccountTest Class Definition
This code shows the contents of the BankAccountTest class definition file, which uses a shared fixture to access the folder defining the BankAccount class. For more information about the BankAccount class and to view the class code, see Developing Classes That Work Together.
classdef (SharedTestFixtures={ ... matlab.unittest.fixtures.PathFixture( ... fullfile("..","source"))}) ... BankAccountTest < matlab.unittest.TestCase methods (Test) function testConstructor(testCase) b = BankAccount(1234,100); testCase.verifyEqual(b.AccountNumber,1234, ... "Constructor must correctly set account number.") testCase.verifyEqual(b.AccountBalance,100, ... "Constructor must correctly set account balance.") end function testConstructorNotEnoughInputs(testCase) import matlab.unittest.constraints.Throws testCase.verifyThat(@()BankAccount,Throws("MATLAB:minrhs")) end function testDeposit(testCase) b = BankAccount(1234,100); b.deposit(25) testCase.verifyEqual(b.AccountBalance,125) end function testWithdraw(testCase) b = BankAccount(1234,100); b.withdraw(25) testCase.verifyEqual(b.AccountBalance,75) end function testNotifyInsufficientFunds(testCase) callbackExecuted = false; function testCallback(~,~) callbackExecuted = true; end b = BankAccount(1234,100); b.addlistener("InsufficientFunds",@testCallback); b.withdraw(50) testCase.assertFalse(callbackExecuted, ... "The callback should not have executed yet.") b.withdraw(60) testCase.verifyTrue(callbackExecuted, ... "The listener callback should have fired.") end end end
DocPolynomTest Class Definition
This code shows the contents of the DocPolynomTest class definition file, which uses a shared fixture to access the folder defining the DocPolynom class. For more information about the DocPolynom class and to view the class code, see Representing Polynomials with Classes.
classdef (SharedTestFixtures={ ... matlab.unittest.fixtures.PathFixture( ... fullfile("..","source"))}) ... DocPolynomTest < matlab.unittest.TestCase properties TextToDisplay = "Equation under test: " end methods (Test) function testConstructor(testCase) p = DocPolynom([1 0 1]); testCase.verifyClass(p,?DocPolynom) end function testAddition(testCase) p1 = DocPolynom([1 0 1]); p2 = DocPolynom([5 2]); actual = p1 + p2; expected = DocPolynom([1 5 3]); diagnostic = [testCase.TextToDisplay ... "(x^2 + 1) + (5*x + 2) = x^2 + 5*x + 3"]; testCase.verifyEqual(actual,expected,diagnostic) end function testMultiplication(testCase) p1 = DocPolynom([1 0 3]); p2 = DocPolynom([5 2]); actual = p1 * p2; expected = DocPolynom([5 2 15 6]); diagnostic = [testCase.TextToDisplay ... "(x^2 + 3) * (5*x + 2) = 5*x^3 + 2*x^2 + 15*x + 6"]; testCase.verifyEqual(actual,expected,diagnostic) end end end
Collect Code Coverage Results
Import the classes used in this example.
import matlab.unittest.plugins.CodeCoveragePlugin import matlab.unittest.plugins.codecoverage.CoverageResult
Create a test suite from the tests folder.
suite = testsuite("tests");
Create a test runner and customize it using a plugin that provides programmatic access to statement, function, and decision coverage metrics for the code in the source folder.
runner = testrunner("textoutput"); format = CoverageResult; plugin = CodeCoveragePlugin.forFolder("source", ... Producing=format,Metrics="decision"); runner.addPlugin(plugin)
Run the tests. In this example, all the tests pass.
runner.run(suite);
Setting up PathFixture Done setting up PathFixture: Added 'C:\work\source' to the path. __________ Running BankAccountTest ..... Done BankAccountTest __________ Running DocPolynomTest ... Done DocPolynomTest __________ Tearing down PathFixture Done tearing down PathFixture: Restored the path to its original state. __________
Because the source folder contains three files, the Result property of format holds the coverage results as a 3-by-1 vector.
results = format.Result
results =
3×1 Result array with properties:
Filename
CreationDate
Filter
Coverage summary (use generateHTMLReport to generate an HTML report):
Function: 9/17 (52.94%)
Statement: 32/102 (31.37%)
Decision: 11/46 (23.91%)
Use coverageSummary to retrieve information from the coverage results.
Generate Standalone Code Coverage Report
Generate a standalone code coverage report as a single HTML file from the coverage results. The method saves the report using an autogenerated filename to a temporary folder. The report includes tables with detailed information about each source file.
generateStandaloneReport(results)
Generating standalone report. Please wait.
Preparing content for the standalone report.
Adding content to the standalone report.
Writing standalone report to file.
Generate a summarized standalone report by including only the statement and function coverage metrics. Save the report as report.html to your current folder.
filePath = generateStandaloneReport(results, ... "report.html",Metrics="statement");
Generating standalone report. Please wait.
Preparing content for the standalone report.
Adding content to the standalone report.
Writing standalone report to file.
Standalone code coverage report has been saved to:
C:\work\report.html
Open report.html.
open(filePath)
Version History
Introduced in R2024aTo specify the coverage metrics to include in the report, use
Metrics instead of MetricLevel as an input.
Metrics enables you to include data type and size coverage in
addition to other types of coverage.
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 generate a standalone code coverage report from coverage results that incorporate justified outcomes, then the report includes the justifications. Previously, the report represented the justified outcomes as covered and did not include the justification reason.
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)