Typically, with script-based tests, you create a test file, and pass the file name to the runtests
function without explicitly creating a suite of Test
objects. If you create an explicit test suite, there are additional features available in script-based testing. These features include selecting tests and using plugins to customize the test runner. For additional functionality, consider using Function-Based Unit Tests or Class-Based Unit Tests.
To create a test suite from a script-based test directly, use the testsuite
function. For a more explicit test suite creation, use the matlab.unittest.TestSuite.fromFile
method of TestSuite
. Then you can use the run
method instead of the runtests
function to run the tests. For example, if you have a script-based test in a file rightTriTolTest.m
, these three approaches are equivalent.
% Implicit test suite result = runtests('rightTriTolTest.m'); % Explicit test suite suite = testsuite('rightTriTolTest.m'); result = run(suite); % Explicit test suite suite = matlab.unittest.TestSuite.fromFile('rightTriTolTest.m'); result = run(suite);
Also, you can create a test suite from all the test files in a specified folder using the matlab.unittest.TestSuite.fromFolder
method. If you know the name of a particular test in your script-based test file, you can create a test suite from that test using matlab.unittest.TestSuite.fromName
.
With an explicit test suite, use selectors to refine your suite. Several of the selectors are applicable only for class-based tests, but you can select tests for your suite based on the test name:
Use the 'Name'
name-value pair
argument in a suite generation method, such as matlab.unittest.TestSuite.fromFile
.
Use a selectors
instance and optional constraints
instance.
Use these approaches in a suite generation method,
such as matlab.unittest.TestSuite.fromFile
, or
create a suite and filter it using the selectIf
method.
For example, in this listing, the four values of suite
are
equivalent.
import matlab.unittest.selectors.HasName import matlab.unittest.constraints.ContainsSubstring import matlab.unittest.TestSuite.fromFile f = 'rightTriTolTest.m'; selector = HasName(ContainsSubstring('Triangle')); % fromFile, name-value pair suite = TestSuite.fromFile(f,'Name','*Triangle*') % fromFile, selector suite = TestSuite.fromFile(f,selector) % selectIf, name-value pair fullSuite = TestSuite.fromFile(f); suite = selectIf(fullSuite,'Name','*Triangle*') % selectIf, selector fullSuite = TestSuite.fromFile(f); suite = selectIf(fullSuite,selector)
If you use one of the suite creation methods with a selector
or name-value pair, the testing framework creates the filtered suite.
If you use the selectIf
method,
the testing framework creates a full test suite and then filters it.
For large test suites, this approach can have performance implications.
If you run tests with the runtests
function
or the run
method of TestSuite
or TestCase
,
the test framework uses a DiagnosticsRecordingPlugin
plugin
that records diagnostics on test results.
After you run tests, you can access recorded diagnostics via
the DiagnosticRecord
field in the Details
property
on TestResult
. For example, if your test results are
stored in the variable results
, find the recorded
diagnostics for the second test in the suite by invoking records
= result(2).Details.DiagnosticRecord
.
The recorded diagnostics are DiagnosticRecord
objects.
To access particular types of test diagnostics for a particular test,
use the selectFailed
, selectPassed
, selectIncomplete
,
and selectLogged
methods of the DiagnosticRecord
class.
By default, the DiagnosticsRecordingPlugin
plugin
records qualification failures and logged events at the matlab.unittest.Verbosity.Terse
level
of verbosity. For more information, see DiagnosticsRecordingPlugin
and DiagnosticRecord
.
Use a TestRunner
object to customize the way
the framework runs a test suite. With a TestRunner
object
you can:
Produce no output in the command window using the withNoPlugins
method.
Run tests in parallel using the runInParallel
method.
Add plugins to the test runner using the addPlugin
method.
For example,use test suite, suite
, to create
a silent test runner and run the tests with the run
method of TestRunner
.
runner = matlab.unittest.TestRunner.withNoPlugins; results = runner.run(suite);
Use plugins to customize the test runner further. For example,
you can redirect output, determine code coverage, or change how the
test runner responds to warnings. For more information, see Add Plugin to Test Runner and the plugins
classes.
TestRunner
| TestSuite
| matlab.unittest.constraints
| plugins
| selectors