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
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 these approaches in a suite generation method,
create a suite and filter it using the
For example, in this listing, the four values of
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
the testing framework creates a full test suite and then filters it.
For large test suites, this approach can have performance implications.
In certain cases, the testing framework uses a
DiagnosticsRecordingPlugin plugin to record diagnostics on test results. The
framework uses the plugin by default if you do any of these:
After you run tests, you can access recorded diagnostics using the
DiagnosticRecord field in the
TestResult object. For example, if your
test results are stored in the variable
result(2).Details.DiagnosticRecord contains the recorded diagnostics
for the second test in the suite.
The recorded diagnostics are
DiagnosticRecord objects. To access particular
types of test diagnostics for a test, use the
selectLogged methods of the
By default, the
records qualification failures and logged events at the
of verbosity. For more information, see
For example,use test suite,
suite, to create
a silent test runner and run the tests with the
run method of
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