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 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 displays the command it uses to run the tests in the Command Window, and runs all the tests in the file.

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.45951 seconds testing time.

Now, 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: 0.0037
       Details: [1×1 struct]

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

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 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. 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 when running tests. For example, the framework generates a qualification failure if a test issues a warning.

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

Parallel

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

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

Debug

Applies 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.

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

Output Detail

Controls the amount of event detail displayed for a test run.

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

Logging Level

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

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

See Also

Related Topics