Contenido principal

Justify Missing Coverage Using MATLAB Test Manager

Since R2025a

This example shows how to use MATLAB® Test Manager to analyze code coverage, view the code coverage report, justify missing outcomes, and explains the differences between the coverage reports for the most recent run and the run history.

Open the Project

This example uses the MATLABShortestPath project. The project includes a src folder, which contains the shortest_path function, a tests folder, which contains tests for the function, and a requirements folder, which contains Requirements Toolbox™ software requirements. The shortest_path function finds the shortest path between two nodes in a graph.

openProject("MATLABShortestPath");

Open MATLAB Test Manager and Run Tests

Open the MATLAB Test Manager. In the Project tab, in the Tools menu, under Apps, click MATLAB Test Manager.

Click the Code Coverage button Code Coverage button, select Enable MATLAB code coverage, and set the slider to MC/DC.

MATLAB Test Manager with Enable code coverage selected and the metric slider set to MC/DC.

Click the Run tests button run_16.png.

View Coverage Results

In the MATLAB Test Manager, the top row summarizes of the results of the tests. Fourteen tests pass, one fails, and 88% of code statements execute. To see more details, point to the Statement button in the MATLAB Test Manager summary.

MATLAB Test Manager with a tooltip showing the results of each coverage metric.

Create the code coverage report by clicking the Statement button in the MATLAB Test Manager.

The Code Coverage Report shows the overall code coverage results for the project.

Justify Coverage Outcome

When you analyze coverage with the MC/DC metric selected, the code coverage report opens to the MC/DC results. You cannot justify code coverage outcomes in the MC/DC or condition coverage views. To justify coverage outcomes, set Currently viewing to Decision.

In the Source Details section, create a justification for line 21 by clicking the entry point for the code block if (~isAdjMatrixValid(adjMatrix)). In the Create Coverage Justification dialog box, enter a reason for the justification. For this example, enter Defensive coding and click OK.

The Create Coverage Justification window. It displays the missing justification coverage at the top. The Reason field below is filled with text that says Defensive coding.

In the MATLAB Test Manager, point to the Statement button to see the updated coverage information. Decision coverage increases from 80.6% to 83.3%, and the statement coverage increases from 88% to 90%.

Archive Justifications into Run History

In the MATLAB Test Manager, click the Run tests button Run_16.png. When you re-run a test, the run history records information on the applied coverage filters. Because coverage reports that you create from the run history are read only you cannot add or remove justification filters to those reports.

In the MATLAB Test Manager, in the menu, click the drop down menu on the left and select your first run, which is the last option under Previous runs. The first run shows the statement coverage results before you created the justification, which was 88%. Then, select the more recent run under the Previous runs to see the updated value of 90%.

Click the Statement button from this run, set Currently viewing to Decision, and scroll to Source Details. Row 21 displays the code coverage justification that you created in that run, and row 26 shows missing coverage that you cannot justify in this read only report.

On line 26, the cursor remains as the default cursor icon, which indicates that you cannot modify this report.

Create Coverage Report from Code Quality Dashboard

Open the Code Quality Dashboard.

codeQualityDashboard;

The Code Quality Dashboard. The pane displays three panes, Code Issues, Tests, and Code Coverage. A yellow banner at the top warns the user that the files have changes.

In the yellow banner, click Refresh Metrics. Open the code coverage report by clicking the MATLAB Code Coverage section title or clicking the three dot menu three dot menu icon and selecting Open Report. Coverage reports that you create from the Code Quality Dashboard open with the most recent results, apply coverage justifications that you created, and are writable, so you can add or remove coverage justifications.

Hovering on line 26, the cursor transforms to a hand, which indicates that you can create coverage filters.

See Also

Functions

Classes

Apps

Topics