Run Tests in Editor - MATLAB & Simulink (original) (raw)

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 namedsampleTest.m (orsampleTest.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 create a test file, the Run section in theEditor (or Live Editor) tab changes to Run Tests and lets you run the tests in the file.

Run Tests section on the Editor or Live Editor tab

In the Run Tests section, click. 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 clickRun Current Test. MATLAB runs only testB.

Running sampleTest . Done sampleTest


In addition to running tests, you can customize the test run by using the test options in the Run Tests section. To access the full list of test options, select Run Tests > 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 Option Description
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 astrue.
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 astrue.
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 astrue.
Use Test Browser Use the test browser to run tests and display results. If you choose not to use the test browser by clearing theUse Test Browser option, then your tests run in the Command Window instead. This selection persists across different MATLAB sessions.
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 theOutputDetail name-value argument ofruntests 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 theLoggingLevel name-value argument ofruntests as that value.

Note

The Strict, Parallel,Output Detail, and Logging Level options are synchronized across the Run Tests section on the Editor (orLive 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 Tests section on the toolstrip.

See Also

Apps

Functions

Topics