matlab.buildtool.tasks.TestTask Class
Namespace: matlab.buildtool.tasks
Superclasses: matlab.buildtool.Task
Description
The matlab.buildtool.tasks.TestTask class provides a task for running a suite of tests using
the MATLAB® unit testing framework.
Tasks created with the TestTask class support incremental builds. To opt in
to this performance optimization, set the SourceFiles property of your TestTask instance to a nonempty
value. By default, with an empty SourceFiles property, a
TestTask instance executes all tests in the test suite each time it runs:
Source code and test code are both integral to testing workflows. If the MATLAB source code exercised by the tests changes and the
TestTaskinstance is unaware of it, the build tool cannot assume that the task can be skipped. Specifying theSourceFilesproperty provides the build tool with the minimum set of task inputs that must be tracked to determine whether an unnecessary task rerun can be avoided. For details on how the build tool handles aTestTaskinstance in incremental builds, see Up-To-Date Check.If you have a MATLAB Test™ license, you can configure your
TestTaskinstance for incremental testing, in addition to incremental building. If you enable incremental testing, the task executes only the tests impacted by changes to code and data, rather than the entire test suite, whenever it reruns. For more information, see Test Impact Analysis Using MATLAB Build Tool (MATLAB Test). (since R2025a)
Creation
Description
task = matlab.buildtool.tasks.TestTask creates a
task for running the tests in the current folder and its subfolders. The task fails if any
of the tests fail.
task = matlab.buildtool.tasks.TestTask(
creates a task that runs the tests in the specified files and folders, including their
subfolders.tests)
task = matlab.buildtool.tasks.TestTask(___,Name=Value)
sets writable properties using
one or more name-value arguments in addition to any of the input argument combinations in
previous syntaxes. You can also set the Description and
Dependencies properties, which the class inherits from the Task class,
using name-value arguments. For example, task =
matlab.buildtool.tasks.TestTask(TestResults="results.xml") creates a task that
produces test results in JUnit-style XML format.
Input Arguments
Properties
Methods
Examples
More About
Tips
To display the test results associated with a
TestTaskinstance in the Test Browser app, first select the Use Test Browser option in the Run Build section on the MATLAB Toolstrip. Then, run the task interactively from the toolstrip or programmatically by specifying the-uioption of thebuildtoolcommand. For more information about the Use Test Browser option, see Run Build from Toolstrip. (since R2026a)If you run a build that includes a
TestTaskinstance in parallel (requires Parallel Computing Toolbox™), the build tool schedules that instance to run on the MATLAB client rather than a MATLAB worker in the current parallel pool. TheTestTaskinstance uses the parallelism of the MATLAB unit testing framework to run its tests in parallel.A
TestTaskinstance respects the build output verbosity level specified at run time using the-verbosityoption of thebuildtoolcommand. The run-time verbosity level takes precedence over any values specified in theOutputDetailandLoggingLevelproperties. (since R2024b)To select tests by tag, use the
TagorSelectorproperty. For example, using theTagproperty, create a task that runs the tests with the"Feature1"tag.task = matlab.buildtool.tasks.TestTask(Tag="Feature1");Alternatively, create a task that runs the same tests by using the
Selectorproperty. (since R2025a)task = matlab.buildtool.tasks.TestTask( ... Selector=matlab.unittest.selectors.HasTag("Feature1"));
If you specify tags by using both the
TagandSelectorproperties, then the task runs only the tests that have all the specified tags. For example, this code creates a task that runs the tests with both the"Feature1"and"Feature2"tags.task = matlab.buildtool.tasks.TestTask( ... Tag="Feature1",Selector=matlab.unittest.selectors.HasTag("Feature2"));
If you use MATLAB Test Manager to manage your tests (requires MATLAB Test), you can generate a code snippet that includes a
TestTaskinstance configured with the test and coverage settings from the test manager. For more information, see Configure Test Settings for Build Tool by Using MATLAB Test Manager (MATLAB Test). (since R2025a)If you specify the
BaseRevisionproperty or task argument (requires MATLAB Test) using a symbolic Git reference, such as"HEAD", the property or task argument value remains fixed to the reference name, even if the reference later moves and points to a different revision. This situation can occur when you update the repository through external Git operations, such asgit fetch, which are outside the scope of the build tool. As a result, running a build with the same value ofBaseRevisionmight lead to different results if the repository state has changed due to these external operations. For consistent build outcomes, ensure that the repository is in a known, stable state when using symbolic references. Alternatively, use unique commit IDs to explicitly specify the base revision for test impact analysis. (since R2026a)You cannot overwrite or remove the action of a built-in task, but you can specify additional task actions. For example, append an action to the
Actionsproperty of a built-in task.plan("myTask").Actions(end+1) = @myAction;