Main Content

Run Tests in Editor

When you open a function-based or class-based test file in the MATLAB® Editor, you have the option to run all tests in the file or to run the test at your cursor location. (This functionality is also available when you open a function-based test file in the Live Editor.) This example shows how to run tests while working in the Editor.

In the Editor, create a test in a file named sampleTest.m.

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

function testA(testCase)
    verifyEqual(testCase,5,5)
end

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

function testC(testCase)
    verifySubstring(testCase,'hello, world','llo')
end

When you save the test, the Run section in the Editor tab changes to Run Tests.

Run Tests section in the Editor tab

Click the Run Tests icon. MATLAB displays the command it uses to run the tests in the Command Window, and the test output follows. MATLAB runs all three tests from sampleTest.m.

runtests('sampleTest')
Running sampleTest
...
Done sampleTest
__________


ans = 

  1×3 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

Totals:
   3 Passed, 0 Failed, 0 Incomplete.
   0.0071673 seconds testing time.

In the Editor, place your cursor in the testB function and click Run Current Test. MATLAB runs testB only.

runtests('sampleTest','ProcedureName','testB')
Running sampleTest
.
Done sampleTest
__________


ans = 

  TestResult with properties:

          Name: 'sampleTest/testB'
        Passed: 1
        Failed: 0
    Incomplete: 0
      Duration: 9.9411e-04
       Details: [1×1 struct]

Totals:
   1 Passed, 0 Failed, 0 Incomplete.
   0.00099411 seconds testing time.

In addition to running tests, you can customize the test run using the test options under Run Tests. (Click the down arrow under Run Tests to access the full option list.) MATLAB uses test options whether you run all the tests in a file or just the test at your cursor location. When you select a test option, the selection persists for the duration of your current MATLAB session.

Test OptionDescription

Clear Output Before Running Tests

Clears the Command Window before running tests.

Strict

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

Tests that run with this option selected have the 'Strict' option of runtests set to true.

Parallel

Runs tests in parallel. This option is only available if Parallel Computing Toolbox™ is installed.

Tests that run with this option selected have the 'UseParallel' option of runtests set to true.

Output Detail

Controls the amount of detail displayed for a test run.

For example, tests that run with Output Detail specified as 0: None have the 'OutputDetail' option of runtests set to 0.

Logging Level

Displays diagnostics logged by the log method at the specified verbosity level or lower.

For example, tests that run with Logging Level specified as 3: Detailed have the 'LoggingLevel' option of runtests set to 3.

You can also run the tests of this example in the Live Editor by saving them in a file with a .mlx extension and using the Run section in the Live Editor tab.

Run section in the Live Editor tab

See Also