Main Content

Find Tests That Depend on Files

To monitor for regressions in code functionality, you can find and run MATLAB® tests that depend on files or folders. This table summarizes the differences between the different methods.

TaskBenefitsMethod for Running TestsRequirements for Collecting Coverage
Find tests that depend on currently open file in the MATLAB Editor
  • Find tests that depend on the current file in the MATLAB Editor.

Test BrowserYou must enable coverage settings and select source files.
Find tests that depend on files or folders by using the MATLAB Test Manager
  • Create custom test suites in the MATLAB Test Manager that depend on files or folders that you select.

  • You can select files of any type.

  • Test suites persist across MATLAB sessions.

  • Test suites update when file dependencies change.

MATLAB Test ManagerYou must enable coverage settings and collect coverage for source files in project.
Find tests that depend on files or folders programmatically
  • Programmatically create test suites that depend on files and folders that you specify.

  • You can specify files of any type.

run method of matlab.unittest.TestSuiteYou must author additional code to configure coverage settings and specify source files.

To find tests impacted by changes for projects under source control, see Find and Run Impacted Tests in Projects Under Source Control.

Find Tests That Depend on Current File in the MATLAB Editor

Since R2025a

To find tests that depend on the current file in the MATLAB Editor, open the Editor tab and click Find Tests. Then, in the dialog box, select the folder to search in.

Close-up view of the MATLAB Toolstrip showing a mouse pointing to the Find Tests button

The dependent tests open in the Test Browser. To run the tests, click the Run button in the Test Browser. For more information, see Run Tests Using Test Browser.

Interactively Find Tests That Depend on Files or Folders

Since R2025a

To find tests that depend on files or folders in the MATLAB Test Manager, you can create a test suite that contains only the tests that depend on a file or folder you specify.

  1. Open the MATLAB Test Manager.

  2. Open the Test Suite Manager. In the menu, click the drop-down list on the left and select Manage Custom Test Suites.

  3. Create a new test suite by clicking New.

  4. In the Name box, enter a name for the test suite.

  5. Select Depends On.

  6. Select the files or folders by setting the drop-down list next to Depends On to Files or Folder, clicking the Browse button , and selecting the file or folder. You can select multiple files or folders by browsing for and selecting files or folders multiple times. The files or folders must be in the current project.

  7. Save the test suite by clicking Save.

The Test Suite Manager shows a custom test suite that depends on the shortest_path.m file in the src folder of the ShortestPath project.

To open the test suite, click the drop-down list on the left and select the test suite. To run the tests in the test suite, click the Run button . For more information, see Manage Tests and Results by Using the MATLAB Test Manager.

To update the test suite when project dependencies change, click the Refresh button .

Programmatically Find Tests That Depend on Files and Folders

Since R2023a

To programmatically find tests that depend on files or folders, you can create a selector by using the matlabtest.selectors.DependsOn class. Pass the files and folders that you want to find tests for as an input to the selector creation function. Then, create a test suite by using the matlab.unittest.TestSuite class. Pass the selector as an input to the test suite creation function.

To run the tests, use the run method of matlab.unittest.TestSuite. You can also collect coverage when you run the tests. For more information, see Collect Code Coverage Metrics for MATLAB Source Code.

This example code opens the ShortestPath project, creates the selector with the DependsOn class, creates a test suite from tests in the project that depend on the shortest_path.m file, and runs the tests.

openExample("matlabtest/ShortestPathExample")

import matlab.unittest.TestSuite
import matlabtest.selectors.DependsOn

dependentFiles = which("shortest_path.m");
dependsOnSelector = DependsOn(dependentFiles)
dependsOnSelector = 
  DependsOn with properties:

     Sources: "C:\Users\jdoe\Documents\MATLAB\MATLABShortestPath\src\shortest_path.m"
    MaxDepth: Inf
suite = TestSuite.fromProject(currentProject,dependsOnSelector)
suite = 
  1×14 Test array with properties:

    Name
    ProcedureName
    TestClass
    BaseFolder
    Parameterization
    SharedTestFixtures
    Tags

Tests Include:
    0 Parameterizations, 1 Unique Shared Test Fixture Class, 2 Unique Tags, 1 TestRequirements schedule.
results = run(suite)
results = 
  1×14 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

Totals:
   14 Passed, 0 Failed, 0 Incomplete.
   0.080809 seconds testing time.

See Also

Apps

Classes

Functions

Topics