Main Content

Run Tests in Editor

When you open a function-based test file in the MATLAB® Editor or Live Editor, or when you open a class-based test file in the Editor, you can interactively run all tests in the file or run the test at your cursor location. This example shows how to run tests while working in the Editor or Live Editor.

In your current folder, create a function-based test file named sampleTest.m (or sampleTest.mlx).

function tests = sampleTest
tests = functiontests(localfunctions);
end

function testA(testCase)
verifyEqual(testCase,2+3,5)
end

function testB(testCase)
verifyGreaterThan(testCase,42,13)
end

function testC(testCase)
verifySubstring(testCase,"Hello World!","llo")
end

When you save the test file, the Run section in the Editor (or Live Editor) tab changes and lets you run the tests in the file.

Run section in the Editor or Live Editor tab

Click the Run Tests icon. MATLAB adds all the tests in the file to Test Browser and runs them. The Command Window displays the test run progress. In this example, all the tests pass.

Running sampleTest
...
Done sampleTest
__________

Now, place your cursor in the testB function and click Run Current Test. MATLAB includes only testB in the test browser and runs it.

Running sampleTest
.
Done sampleTest
__________

In addition to running tests, you can customize the test run by using the test options under Run Tests. To access the full list of test options, click the drop-down arrow under Run Tests, and then click Customize Test Run. MATLAB uses the selected test options whether you run all the tests in a file or just the test at your cursor location.

Test OptionDescription

Clear Output Before Running Tests

Clear the Command Window before running tests.

This selection persists for the duration of your current MATLAB session.

Strict

Apply strict checks when running tests. For example, the framework generates a qualification failure if a test issues a warning.

This selection persists across different MATLAB sessions.

Selecting this option is the same as specifying the Strict name-value argument of runtests as true.

Parallel

Run tests in parallel. This option is available only if you have Parallel Computing Toolbox™ installed.

This selection persists for the duration of your current MATLAB session.

Selecting this option is the same as specifying the UseParallel name-value argument of runtests as true.

Debug

Apply debugging capabilities when running tests. For example, the framework pauses test execution and enters debug mode if it encounters a test failure or uncaught error.

This selection persists for the duration of your current MATLAB session.

Selecting this option is the same as specifying the Debug name-value argument of runtests as true.

Output Detail

Control the amount of output detail displayed for a test run.

This selection persists across different MATLAB sessions.

Selecting a value for this option is the same as specifying the OutputDetail name-value argument of runtests as that value.

Logging Level

Display diagnostics logged by the log (TestCase) and log (Fixture) methods at the specified verbosity level and below.

This selection persists across different MATLAB sessions.

Selecting a value for this option is the same as specifying the LoggingLevel name-value argument of runtests as that value.

Note

The Strict, Parallel, Output Detail, and Logging Level options are synchronized across the Run section in the Editor (or Live Editor) tab and the Test Browser toolbar. If you select an option in one of these interfaces, the selection applies to the other interface as well. For example, if you enable parallel test execution in the test browser, MATLAB automatically selects the Parallel test option in the Run section of the toolstrip.

See Also

Apps

Functions

Related Topics