Main Content

resetFilter

Remove filters from coverage results

Since R2024b

Description

newr = resetFilter(r) removes the coverage filters applied to the code coverage results r. The method returns the new coverage results as a matlab.coverage.Result vector.

example

Input Arguments

expand all

Code coverage results, specified as a matlab.coverage.Result vector.

Examples

expand all

Create a justification to filter a block of code that must be covered by a method other than automated testing, for instance, by interactive testing. Then, use the justification to create new coverage results.

First, open this example to access the required files.

openExample("matlabtest/UpdateCoverageResultsUsingJustificationsExample")

When you open the example, these files appear in your current folder:

  • logToFile function — This function appends a string to a file. The function throws a system-dependent error if it cannot open the file.

  • LoggingTest test class — This test class contains a test to verify that the logToFile function adds the expected string to a specified file. However, the test class does not test the logToFile function in the case of system-dependent errors because there is a plan to test the errors interactively.

  • collectCoverage script — This script provides programmatic access to the statement, function, and decision coverage information for the source code in the file logToFile.m and generates an interactive HTML code coverage report from the produced coverage result.

Run the collectCoverage script to collect code coverage information and produce an interactive code coverage report. Because the test does not achieve a decision outcome in the source code, the code coverage report includes a missed statement and a missed decision outcome.

collectCoverage
Running LoggingTest
.
Done LoggingTest
__________
result = 
  Result with properties:

        Filename: "C:\work\logToFile.m"
    CreationDate: 18-Jul-2024 16:10:59
          Filter: [0×0 matlabtest.coverage.Justification]

Coverage summary (HTML report):
   Function: 1/1 (100%)
   Statement: 5/6 (83.33%)
   Decision: 1/2 (50%)

Use coverageSummary to retrieve information from the coverage results.

Justify the missing code coverage by following these steps:

  1. With Decision selected from the Currently viewing list on the code coverage report, click the if fileID < 0 branching statement in the Source Details section.

  2. In the dialog box that appears, enter a reason for the justification, and then click OK.

  3. Using your system file browser, save the justification to a MAT file named myFilter.mat in your current folder. The testing framework saves the justification as a matlabtest.coverage.Justification object assigned to a variable named Filters in myFilter.mat.

Create Coverage Justification dialog box, with the reason entered as "Requires interactive testing"

After creating the Justification object, you can use the applyFilter and resetFilter methods to create new coverage results. For example, this code creates a coverage result that incorporates the justified outcomes and then generates an interactive code coverage report from the new result.

S = load("myFilter.mat");
result1 = result.applyFilter(S.Filters)
generateHTMLReport(result1)

This code creates another coverage result by removing the justification applied to result1.

result2 = result1.resetFilter

Version History

Introduced in R2024b