matlab.unittest.plugins.codecoverage.CoverageReport class

Package: matlab.unittest.plugins.codecoverage

Format for code coverage report

Description

To display code coverage metrics in the MATLAB® browser, use an instance of the CoverageReport class with the plugin CodeCoveragePlugin.

Creation

Description

reportFormat = CoverageReport constructs a CoverageReport format. Use this report format with matlab.unittest.plugins.CodeCoveragePlugin to produce an HTML file in a temporary folder that contains the code coverage report.

example

reportFormat = CoverageReport(reportFolder) constructs a CoverageReport format. Use this report format with matlab.unittest.plugins.CodeCoveragePlugin to produce an HTML file in the specified folder that contains the code coverage report. Generating the report overwrites the contents of the folder specified by reportFolder.

example

reportFormat = CoverageReport(___,'MainFile',mainFileName) constructs a CoverageReport format with mainFileName as the name of the main HTML file containing the code coverage report.

Input Arguments

expand all

Folder containing code coverage report files, specified as the path to a folder relative to the current folder or as an absolute path. If the folder does not exist, CoverageReport creates it.

Name of the main HTML file containing code coverage report, specified as a file name with a .html extension.

Properties

expand all

Name of the main HTML file that contains the code coverage report, specified as a file name with a .html extension. To set the property, pass the file name to the constructor as a 'MainFile','fileName' pair.

Example: myReport.html

Attributes:

GetAccess
public
SetAccess
immutable

Data Types: char | string

Examples

collapse all

Create a test report using the CoverageReport format. Specify the folder and HTML file name for the report.

Create a test for this function.

function res = add5(x)
% Increment input by 5
if ~isa(x,'numeric')
    error('add5:InputMustBeNumeric','Input must be numeric.')
end
res = x + 5;
end

Derive a class from matlab.unittest.TestCase to implement test methods.

classdef Add5Test < matlab.unittest.TestCase
    methods (Test)
        function testDoubleOut(testCase)
            actOutput = add5(1);
            testCase.verifyClass(actOutput,'double')
        end
        function testNonNumericInput(testCase)
            testCase.verifyError(@()add5('0'),'add5:InputMustBeNumeric')
        end
    end
end

This test script creates a test suite for a single file, adds the CodeCoveragePlugin with the CoverageReport format to the test runner, and runs the tests. The format on the plugin specifies the name of the main test results HTML file and the folder containing the results.

import matlab.unittest.TestSuite
import matlab.unittest.TestRunner
import matlab.unittest.plugins.CodeCoveragePlugin
import matlab.unittest.plugins.codecoverage.CoverageReport

suite = TestSuite.fromFile('Add5Test.m');
runner = TestRunner.withNoPlugins;
runner.addPlugin(CodeCoveragePlugin.forFile('add5.m', ...
   'Producing',CoverageReport('Add5Results', ...
   'MainFile','Add5TestResults.html')))
runner.run(suite)
Code coverage report has been saved to:
 C:\myWorkingFolder\Add5Results\Add5TestResults.html
Introduced in R2019a