Main Content

Interactively Collect Code Coverage

You can interactively measure the statement, function, decision, condition, and modified condition/decision coverage (MC/DC) of your MATLAB® source code when you run tests by using the Test Browser, MATLAB Test Manager, or Code Quality Dashboard.

This table compares the benefits and output of the Test Browser, MATLAB Test Manager, and Code Quality Dashboard.

Method for Collecting CoverageBenefitsInterface for Viewing Coverage Results
Test Browser
  • Specify source code files or folders to analyze when collecting code coverage.

  • Specify tests to run and collect coverage for.

Code coverage report, accessed from MATLAB Command Window
MATLAB Test Manager
  • Collect code coverage for tests in a project.

  • Analyze source code files in a project.

  • Maintain code coverage report data between MATLAB sessions.

  • View high-level code coverage results directly from the test manager.

  • Code coverage report, accessed from MATLAB Test Manager

  • Statement button in the MATLAB Test Manager

Code Quality Dashboard
  • Collect code coverage for tests in a project.

  • Analyze source code files in a project.

  • Maintain code coverage report data between MATLAB sessions.

  • View high-level code coverage results directly from the dashboard.

  • Code coverage report, accessed from Code Quality Dashboard

  • Coverage table in dashboard

For information about collecting coverage programmatically, see Collect Code Coverage Metrics for MATLAB Source Code.

Test Browser

To collect coverage using the Test Browser:

  1. Open the Test Browser. For more information, see Open the Test Browser App.

  2. Add tests to the browser by clicking the Add button .

  3. Enable coverage by clicking the Open coverage settings button and selecting Enable coverage reporting.

  4. Set Metrics to the highest level of coverage to include in the code coverage report.

    When you select a metric level, the report includes the metrics up to and including the selected level. For example, if you select Condition, the report includes the statement, function, decision, and condition coverage metrics.

  5. Specify source files under Source by clicking Add Files or Add Folders. You can include source code in the subfolders of the specified folder by right-clicking the folder path and selecting Include Subfolders.

    Coverage settings section, including the condition coverage metric level and the paths to a source file and a source folder

  6. Run the tests by clicking the Run button .

To view the code coverage report, click the hyperlink in the MATLAB Command Window. To automatically open the generated code coverage report after the test run, in the Test Browser, under Report, select Open coverage report after run.

The browser generates a code coverage report for your source code based on the tests that ran. If you do not specify source code, the tests still run, but the browser does not generate a report. For more information about the browser, see Run Tests Using Test Browser.

MATLAB Test Manager

You can use the MATLAB Test Manager to collect code coverage for all tests in the project that have the Test label, or for individual test suites or tests. To collect code coverage for all tests in the project:

  1. Open a project that contains MATLAB source code and tests.

  2. Open the MATLAB Test Manager. For more information, see Open the MATLAB Test Manager App.

  3. To collect code coverage for MATLAB source code, click the Code Coverage button , then select Enable code coverage.

    Enable code coverage is selected in the MATLAB Test Manager.

  4. Set the coverage metric level to the highest level of coverage that you want to include in the code coverage report.

    When you select a metric level, the report includes the metrics up to and including the selected level. For example, if you select Condition, the report includes the statement, function, decision, and condition coverage metrics.

  5. Set the drop-down to the left of the Run button to All Tests in Current Project.

  6. Click the Run button .

Note

You can use the MATLAB Test Manager to collect coverage for M, MLX, and MLAPP files in the project that have the Design label. For more information, see Add Labels to Project Files.

To collect code coverage for a test suite, click the drop-down to the left of the Run button . Select your test suite, then click the Run button .

To collect code coverage for selected tests, hold Shift or Ctrl and click the tests. Then, next to the Run button , click the Expand icon and select Run selected. Alternatively, you can right-click one of the selected tests and select Run selected.

To open the code coverage report, click the Report button , then, under Code Coverage Reports, select the report. The MATLAB Test Manager stores the five most recent code coverage reports.

The mouse points to the code coverage reports section of the MATLAB Test Manager, which lists five coverage reports.

Tip

To automatically open the code coverage report after you collect coverage, click the Code Coverage button , then select Automatically open code coverage report.

The Statement button displays the statement coverage percentage for the tests in the current test suite. To view the coverage summary for the selected coverage metrics, point to the Statement button. The coverage summary percentage includes justified missing coverage (since R2024b). For more information on justifying coverage, see Justify Missing Coverage for MATLAB Source Code.

The mouse points to the Statement button and the Coverage Summary is shown in the tooltip.

Running selected tests does not update the results in the coverage summary. To open the coverage report for the current test suite, click the Statement button.

Note

Running test suites or selected tests does not update the coverage metric results in the Code Quality Dashboard.

Additionally, if you have Embedded Coder®, you can collect coverage for generated C/C++ code that executes in SIL and PIL equivalence tests. For more information, see Collect Coverage for Generated C/C++ Code in Equivalence Tests. (since R2024b)

For more information about running tests using the test manager, see Manage Tests and Results by Using the MATLAB Test Manager.

Code Quality Dashboard

To collect coverage using the Code Quality Dashboard:

  1. Open a project that contains MATLAB source code and tests.

  2. Open the Code Quality Dashboard. For more information, see Open the Code Quality Dashboard App.

  3. Run tests and collect coverage by clicking the Run button .

    Code Quality Dashboard with coverage results.

Clicking the Run button runs all tests in the open project and its referenced projects and collects coverage for the metric level that you set in the MATLAB Test Manager. To change the metric level, open the MATLAB Test Manager, click the Code Coverage button and set the level. For more information, see MATLAB Test Manager.

Note

Clicking the Run button enables and collects coverage for MATLAB source code even if coverage is disabled in the MATLAB Test Manager. However, you must configure the test manager to collect coverage for generated C/C++ code in equivalence tests.

To open the coverage report, click the coverage table in the Code Coverage section.

Additionally, if you have Embedded Coder, you can collect coverage for generated C/C++ code that executes in SIL and PIL equivalence tests. For more information, see Collect Coverage for Generated C/C++ Code in Equivalence Tests. After you collect the coverage, open the generated coverage report by clicking the table in the Generated Code Coverage section. (since R2024b)

For more information about using the Code Quality Dashboard, see Measure and Monitor Project Quality.

See Also

Apps

Related Examples

More About