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 coverage.

  • Specify tests to run and collect coverage for.

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

  • Analyze source code files in a project.

  • Maintain coverage report data between MATLAB sessions.

Code coverage report, accessed from MATLAB Test Manager
Code Quality Dashboard
  • Collect coverage for tests in a project.

  • Analyze source code files in a project.

  • Maintain coverage report data between MATLAB sessions.

  • View high-level 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 coverage for all tests in the project that have the Test label, or for individual test suites or tests. To collect 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. Click the Coverage button , then select Enable Coverage.

    Enable 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 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 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 coverage report, click the Report button , then, under Coverage Reports, select the report. The MATLAB Test Manager stores the five most recent coverage reports.

Note

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

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

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 Coverage button and set the level. For more information, see MATLAB Test Manager.

Note

Clicking the Run button enables and collects coverage even if coverage is disabled in the MATLAB Test Manager.

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

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

See Also

Apps

Related Examples

More About