matlabtest.plugins.codecoverage.StandaloneReport - Format for standalone code coverage report - MATLAB (original) (raw)
Main Content
Namespace: matlabtest.plugins.codecoverage
Superclasses: matlab.unittest.plugins.codecoverage.CoverageFormat
Format for standalone code coverage report
Since R2024a
Description
The matlabtest.plugins.codecoverage.StandaloneReport
class provides a way to generate a standalone code coverage report as a single HTML file. To generate a code coverage report in this format, create a CodeCoveragePlugin instance using a StandaloneReport
object, and then add the plugin to the test runner.
A standalone code coverage report is not interactive and contains hard-coded information. To generate an interactive code coverage report, use the matlab.unittest.plugins.codecoverage.CoverageReport class instead.
Creation
Description
format = matlabtest.plugins.codecoverage.StandaloneReport([filename](#mw%5Fc875f2a2-d52b-44ac-8e9d-53fa329504a5))
creates a StandaloneReport
object that instructsCodeCoveragePlugin
to generate a standalone code coverage report and save it using the specified filename.
Input Arguments
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"
Examples
Run a suite of tests and generate a standalone code coverage report as a single HTML file for your source code.
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
Run Tests to Generate Report
Import the classes used in this example.
import matlab.unittest.plugins.CodeCoveragePlugin import matlabtest.plugins.codecoverage.StandaloneReport
Create a test suite from the tests
folder.
suite = testsuite("tests");
Create a test runner and customize it using a plugin that generates a standalone HTML code coverage report, including the statement, function, and decision coverage metrics, for the code in the source
folder. Specify that the plugin writes its output to a file named report.html
in your current folder.
runner = testrunner("textoutput"); format = StandaloneReport("report.html"); plugin = CodeCoveragePlugin.forFolder("source", ... Producing=format,MetricLevel="decision"); runner.addPlugin(plugin)
Run the tests. In this example, all the tests pass. The plugin generates a standalone code coverage report as a single HTML file in your current folder.
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.
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
. The report includes tables with detailed information about each file in the source
folder.
Since R2025a
Generate a standalone code coverage report by reusing justifications previously created for the source code under test. For more information about justifications, see Justify Missing Coverage for MATLAB Source Code.
Suppose that you previously justified the missing coverage for your source code and saved the justifications to a file named myFilters.xml
in your current folder. Using the justifications in myFilters.xml
, run your tests and produce a standalone code coverage report that includes the justifications.
import matlab.unittest.plugins.CodeCoveragePlugin import matlabtest.plugins.codecoverage.StandaloneReport
runner = testrunner("textoutput"); format = StandaloneReport("report.html"); plugin = CodeCoveragePlugin.forFolder("myFolder", ... Producing=format,MetricLevel="mcdc",Filter="myFilters.xml"); runner.addPlugin(plugin)
suite = testsuite("MyTestClass"); results = runner.run(suite);
If you are using a MATLABĀ® project to organize your work, then you do not need to specify theFilter
name-value argument. The testing framework automatically applies the justifications stored in the project.
Version History
Introduced in R2024a
If you generate a standalone code coverage report by reusing justifications previously created for the source code under test, then the report includes the justifications. Previously, the report represented the justified outcomes as covered and did not include the justification reason.