Extend Function-Based Tests
Typically, with function-based tests, you create a test file and pass the file name to the
runtests function without explicitly creating a suite of
Test objects. However, if you create an explicit test suite, additional features are available in function-based testing. These features include:
Test logging and verbosity
Plugins to customize the test runner
For additional functionality, consider using Class-Based Unit Tests.
Fixtures for Setup and Teardown Code
When writing tests, use the
applyFixture method to handle setup and teardown code for actions such as:
Changing the current working folder
Adding a folder to the path
Creating a temporary folder
Suppressing the display of warnings
fixtures take the place of manually coding the actions in the
teardown functions of your function-based test.
For example, if you manually write setup and teardown code to set up a temporary folder for each test, and then you make that folder your current working folder, your
teardown functions could look like this.
function setup(testCase) % store current folder testCase.TestData.origPath = pwd; % create temporary folder testCase.TestData.tmpFolder = ['tmpFolder' datestr(now,30)]; mkdir(testCase.TestData.tmpFolder) % change to temporary folder cd(testCase.TestData.tmpFolder) end function teardown(testCase) % change to original folder cd(testCase.TestData.origPath) % delete temporary folder rmdir(testCase.TestData.tmpFolder) end
However, you also can use a fixture to replace both of those functions with just a modified
setup function. The fixture stores the information necessary to restore the initial state and performs the teardown actions.
function setup(testCase) % create temporary folder f = testCase.applyFixture(matlab.unittest.fixtures.TemporaryFolderFixture); % change to temporary folder testCase.applyFixture(matlab.unittest.fixtures.CurrentFolderFixture(f.Folder)); end
Test Logging and Verbosity
Your test functions can use the
log method. By default, the test runner reports diagnostics logged at verbosity level 1 (
Terse). Use the
matlab.unittest.plugins.LoggingPlugin.withVerbosity method to respond to messages of other verbosity levels. Construct a
TestRunner object, add the
LoggingPlugin, and run the suite with the
run method. For more information on creating a test runner, see Test Runner Customization.
Test Suite Creation
Calling your function-based test returns a suite of
Test objects. You also can use the
testsuite function or the
matlab.unittest.TestSuite.fromFile method. If you want a particular test and you know the test name, you can use
matlab.unittest.TestSuite.fromName. If you want to create a suite from all tests in a particular folder, you can use
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.
There are several ways to run a function-based test.
|To Run All Tests||Use Function|
|In a file|
|In a suite|
|In a suite with a custom test runner|
For more information, see Run Tests for Various Workflows.
Programmatic Access of Test Diagnostics
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
Test Runner Customization
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