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.
Task | Benefits | Method for Running Tests | Requirements for Collecting Coverage |
---|---|---|---|
Find tests that depend on currently open file in the MATLAB Editor |
| Test Browser | You must enable coverage settings and select source files. |
Find tests that depend on files or folders by using the MATLAB Test Manager |
| MATLAB Test Manager | You must enable coverage settings and collect coverage for source files in project. |
Find tests that depend on files or folders programmatically |
| run method of
matlab.unittest.TestSuite | You 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.
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.
Open the MATLAB Test Manager.
Open the Test Suite Manager. In the menu, click the drop-down list on the left and select
Manage Custom Test Suites
.Create a new test suite by clicking New.
In the Name box, enter a name for the test suite.
Select Depends On.
Select the files or folders by setting the drop-down list next to Depends On to
Files
orFolder
, 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.
Save the test suite by clicking Save.
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.