Run Tests in Editor

This example shows how to run a test while working in the Editor. When you open a function-based test or class-based test in the Editor, the Editor tab contains a Run Tests section instead of a Run section. This section provides several options to run the tests in your current file.

The Run Tests button runs all the tests in the file. The Run Current Test button runs the test at your current cursor location.

In the Editor, create a test in a file named sampleTest.m. When you save the test, the Run section in the Editor tab changes to Run Tests.

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

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 the Run Current Test icon. 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 the Run Tests icon. MATLAB uses test options whether you run all the tests in a file or just the test at your cursor location.

Test OptionDescription

Clear Command Window

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 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 the Parallel Computing Toolbox™ is installed.

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

Output Detail

Displays the progress of a test run and messages logged by the TestCase.log method at the specified verbosity level or lower.

For example, tests run with the Output Detail value of 3: Detailed have the 'Verbosity' option of runtests set to 3.

When you select a test option, the selection persists for the duration of your current MATLAB session.

See Also

Was this topic helpful?