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.
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 Tests||Benefits||Requires Additional Code||Able to Collect Coverage|
||Yes||Yes (requires additional code)|
|MATLAB Editor toolstrip||
|MATLAB Test Manager||
|Code Quality Dashboard||
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 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.
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.
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
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 .
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.