Run Tests for Various Workflows

Set Up Example Tests

To explore different ways to run tests, create a class-based test and a function-based test in your current working folder. For the class-based test file use the DocPolynomTest example test presented in the matlab.unittest.qualifications.Verifiable example. For the function-based test file use the axesPropertiesTest example test presented in Write Test Using Setup and Teardown Functions.

Run All Tests in Class or Function

Use the run method of the TestCase class to directly run tests contained in a single test file. When running tests directly, you do not need to explicitly create a Test array.

% Directly run a single file of class-based tests
results1 = run(DocPolynomTest); 

% Directly run a single file of function-based tests
results2 = run(axesPropertiesTest);

You can also assign the test file output to a variable and run the tests using the functional form or dot notation.

% Create Test or TestCase objects
t1 = DocPolynomTest;      % TestCase object from class-based test
t2 = axesPropertiesTest;  % Test object from function-based test

% Run tests using functional form
results1 = run(t1); 
results2 = run(t2);

% Run tests using dot notation
results1 = t1.run; 
results2 = t2.run;

Alternatively, you can run tests contained in a single file by using runtests.

Run Single Test in Class or Function

Run a single test from within a class-based test file by specifying the test method as an input argument to the run method. For example, only run the test, testMultiplication, from the DocPolynomTest file.

results1 = run(DocPolynomTest,'testMultiplication');

Function-based test files return an array of Test objects instead of a single TestCase object. You can run a particular test by indexing into the array. However, you must examine the Name field in the test array to ensure you run the correct test. For example, only run the test, surfaceColorTest, from the axesPropertiesTest file.

t2 = axesPropertiesTest;  % Test object from function-based test
t2(:).Name
ans =

axesPropertiesTest/testDefaultXLim


ans =

axesPropertiesTest/surfaceColorTest

The surfaceColorTest test corresponds to the second element in the array.

Only run the surfaceColorTest test.

results2 = t2(2).run; % or results2 = run(t2(2));

Run Test Suites by Name

You can run a group, or suite, of tests together. To run the test suite using runtests, the suite is defined as a cell array of strings representing a test file, a test class, a package that contains tests or a folder that contains tests.

suite = {'axesPropertiesTest','DocPolynomTest'};
runtests(suite);

Run all tests in the current folder using the pwd as input to the runtests function.

 runtests(pwd);

Alternatively, you can explicitly create Test arrays and use the run method to run them.

Run Test Suites from Test Array

You can explicitly create Test arrays and use the run method in the TestSuite class to run them. Using this approach, you explicitly define TestSuite objects and, therefore, can examine the contents. The runtests function does not return the TestSuite object.

import matlab.unittest.TestSuite
s1 = TestSuite.fromClass(?DocPolynomTest);
s2 = TestSuite.fromFile('axesPropertiesTest.m');

% generate test suite and then run
fullSuite = [s1 s2];
result = run(fullSuite); 

Since the suite is explicitly defined, it is easy for you to perform further analysis on the suite, such as rerunning failed tests.

failedTests = fullSuite([result.Failed]);
result2 = run(failedTests);

Run Tests with Customized Test Runner

You can specialize the test running by defining a custom test runner and adding plugins. The run method of the TestRunner class operates on a TestSuite object.

import matlab.unittest.TestRunner
import matlab.unittest.TestSuite
import matlab.unittest.plugins.TestRunProgressPlugin

% Generate TestSuite.
s1 = TestSuite.fromClass(?DocPolynomTest);
s2 = TestSuite.fromFile('axesPropertiesTest.m');
suite = [s1 s2];

% Create silent test runner.
runner = TestRunner.withNoPlugins;

% Add plugin to display test progress.
runner.addPlugin(TestRunProgressPlugin.withVerbosity(2))

% Run tests using customized runner.
result = run(runner,[s1 s2]);

See Also

| | |

Was this topic helpful?