Main Content

Run MATLAB Tests

To guide software development and monitor for regressions in code functionality, you can write tests for your MATLAB® programs. You can use MATLAB to run tests programmatically or by using the MATLAB Editor or the Test Browser.

Additionally, if you have MATLAB Test™, you can run tests in projects by using the MATLAB Test Manager or the Code Quality Dashboard.

For more information about authoring MATLAB tests, see Ways to Write Unit Tests.

When you run tests, you can customize your run with strict checks, use parallel execution, and specify the output detail and logging level. This table summarizes the differences between the different methods.

Method for Running TestsBenefitsRequires Additional CodeAble to Collect Coverage
MATLAB commands
  • Runs tests multiple times with the same settings by authoring code that runs tests.

  • Enables you to customize the test run by using user-authored plugins.

  • Displays test results at the MATLAB command line.

YesYes (requires additional code)
MATLAB Editor toolstrip
  • Eliminates the need for additional MATLAB code that runs tests.

  • Displays test results in the Test Browser app.

NoNo
Test Browser
  • Eliminates the need for additional MATLAB code that runs tests.

  • Displays test results in the Test Browser app.

  • Enables running tests from multiple files or folders in a single run.

NoYes
MATLAB Test Manager
  • Eliminates the need for additional MATLAB code that runs tests.

  • Displays test results in the MATLAB Test Manager app.

  • Enables running tests in the open project from multiple files or folders in a single run.

  • Allows you to group tests in test suites that you can save for subsequent runs.

  • Maintains test result data between MATLAB sessions.

NoYes
Code Quality Dashboard
  • Eliminates the need for additional MATLAB code that runs tests.

  • Displays test results at a high level in the Code Quality Dashboard app.

  • Enables you tu run tests in the open project from multiple files or folders in a single run.

  • Allows you to run all tests in the open project.

  • Maintains test result data between MATLAB sessions.

NoYes

Run Tests Programmatically

You can author code to run tests programmatically. Running tests programmatically allows you to run tests multiple times with the same settings and use user-authored plugins that customize your test run.

To run tests, use run or runtests. You can also create and run test suites by using testsuite, then run. For more information, see Run Tests for Various Workflows.

To generate a test report, add an instance of the matlab.unittest.plugins.TestReportPlugin to your test runner.

To collect coverage, add an instance of the matlab.unittest.plugins.CodeCoveragePlugin class to your test runner. For more information, see Collect Statement and Function Coverage Metrics for MATLAB Source Code.

When you run tests, the test results are returned at the MATLAB command line and do not persist between MATLAB sessions.

Run Tests Using the MATLAB Editor or Test Browser

You can run tests from the MATLAB Editor and Test Browser and eliminate the need to write additional MATLAB code that runs your tests and customizes your test settings.

To run tests using the MATLAB Editor, in the Editor tab, in the Run section, click Run Tests. When you run tests by using the MATLAB Editor, MATLAB also opens the Test Browser and displays the tests in the run and their status. For more information, see Run Tests in Editor.

Alternatively, you can manually open the Test Browser. In the Apps tab, in the MATLAB section, click Test Browser. Then, add tests by clicking the Add button and run tests by clicking the Run button . For more information, see Run Tests Using Test Browser.

When you run tests by using the Editor or Test Browser, the output is returned at the MATLAB command line and the results are displayed in the Test Browser. When tests fail, the Test Browser displays the failure diagnostic.

The Test Browser shows 14 passed tests from a test file call graph_unit_tests.

Test results from the MATLAB Editor and Test Browser do not persist between MATLAB sessions.

Comparison of Test Settings

The MATLAB Editor and Test Browser share test settings and provide similar functionality for setting test options, including parallel execution, strictness, output detail, and logging level options. If you select an option in one of these interfaces, the selection applies to the other interface as well. However:

  • In the MATLAB Editor, you can apply debugging capabilities by selecting Run Tests > Customize Test Run > Debug.

  • In the Test Browser, you can add and run tests from multiple files or folders by clicking the Add button , then the Run button .

  • In the Test Browser, you can collect coverage and generate coverage reports by clicking the Coverage button , selecting Enable coverage reporting, setting the metric level, and specifying the source code by using the Source menu.

Run Tests by Using the MATLAB Test Manager

You can use the MATLAB Test Manager to organize and run tests in projects and view the results. To open the test manager, first open a project. Then, in the Project tab, in the Tools menu, under Apps, click MATLAB Test Manager. You can:

  • Organize tests by creating and saving test suites based on test selection criteria and using test tags.

  • Run tests and view a summary of the results.

  • Investigate test results by viewing failure diagnostics and using filters.

  • Run subsets of tests by using test suites, test selections, and filters.

  • Collect coverage and generate coverage reports.

  • Verify linked requirements and view verification status in the Code Quality Dashboard.

MATLAB Test Manager with 14 passed tests and 1 failed test.

The MATLAB Test Manager shares test settings with the MATLAB Editor and Test Browser. For more information, see Comparison of Test Settings. To edit test settings, click the Settings button .

Results from the test manager persist across sessions.

For more information, see Manage Tests and Results by Using the MATLAB Test Manager.

Run Tests Using the Code Quality Dashboard

You can use the Code Quality Dashboard to run all tests in the project that have the Test label and view the results. To open the dashboard, first open a project. Then, in the Project tab, in the Tools menu, under Apps, click Code Quality Dashboard. To run the tests, click the Run button .

Clicking the Run button :

  • Runs all tests in the open project and its referenced projects

  • Collects coverage for the tests

  • Verifies requirements that link to the tests

Note

Clicking the Run button in the Code Quality Dashboard collects coverage using the metric level specified in the MATLAB Test Manager. The dashboard enables and collects coverage even if coverage is disabled in the MATLAB Test Manager.

To stop the tests from running to completion, click the Stop button .

Code Quality Dashboard with metric results for a project.

You can view detailed test and coverage results and view the requirements by clicking the sections in the dashboard. For more information, see Measure and Monitor Project Quality.

See Also

Apps

Related Topics