Extend MATLAB Unit Testing Framework with MATLAB Test
MATLAB® Test™ extends the capabilities of the MATLAB unit testing framework. For example, with MATLAB Test, you can:
Measure the decision coverage, condition coverage, and modified condition/decision coverage (MC/DC) of your source code.
Generate a standalone code coverage report.
Filter a test suite by source code dependency.
Create and run a suite of tests that verify requirements (requires Requirements Toolbox™).
Run tests by using the
"parameter combination.n-wise"
This topic provides a summary of how MATLAB Test extends the functionality provided by existing MATLAB apps, classes, and functions. For more information about the MATLAB unit testing framework, see Ways to Write Unit Tests.
Measure Decision Coverage, Condition Coverage, and MC/DC
The unit testing framework lets you measure the statement coverage and function
coverage of your MATLAB source code. You can collect and access these metrics either
programmatically by using the matlab.unittest.plugins.CodeCoveragePlugin
class or interactively by using the Test Browser
app. If you have a MATLAB
Test license, you can use CodeCoveragePlugin and Test
Browser to also measure the decision coverage, condition coverage, and MC/DC
of your source code.
Generate Standalone Code Coverage Report
Since R2024a
If you have a MATLAB
Test license, you can generate a standalone code coverage report as a single
file by using the generateStandaloneReport method of the matlab.coverage.Result class. Alternatively, you can generate a standalone
report if you create a CodeCoveragePlugin instance using the matlabtest.plugins.codecoverage.StandaloneReport class.
Justify Missing Coverage
Since R2024b
If you have a MATLAB
Test license, you can create justifications to filter blocks of source code
that were not covered by tests. You can then create new coverage results by using the
applyFilter and resetFilter methods of the matlab.coverage.Result class. Additionally, you can reuse the
justifications previously created for the source code under test by specifying the
Filter name-value argument when you create a plugin using one
of the static methods of the CodeCoveragePlugin class. For more
information, see Justify Missing Coverage for MATLAB Source Code.
Filter Test Suite by Source Code Dependency
If you have a MATLAB
Test license, you can filter a test suite by test file dependency on specified
source code. To specify the source files and folders, use the matlabtest.selectors.DependsOn class or the DependsOn
name-value argument. You can specify the DependsOn name-value
argument for these methods and functions of the unit testing framework:
Static methods of the
matlab.unittest.TestSuiteclass, such asmatlab.unittest.TestSuite.fromFolder
Create and Run Suite of Tests That Verify Requirements
If you have MATLAB
Test and Requirements Toolbox licenses, you can create a test suite from tests that verify requirements
by using matlab.unittest.TestSuite.fromRequirements, which is a static method of
the matlab.unittest.TestSuite class. You can also use the
testsuite and runtests functions to
respectively create a suite of tests and run tests that verify requirement sets.
Run Tests Using "n-wise" Parameter Combination
n-wise"When you pass more than one parameterization property to a method of a matlab.unittest.TestCase subclass,
MATLAB
Test can invoke the method for every
-tuple of parameter values. To use
this type of parameter combination, specify the nParameterCombination
attribute of the method as ", where
n-wise" is an integer between 2 and 10.
The n" parameter combination
generalizes the n-wise""pairwise" combination.
Create Baseline Tests
Since R2024b
If you have a MATLAB
Test license, you can compare the output of your code against baseline data by
creating and running baseline tests. For example, you can create a test class with a
parameterization property set to a scalar baseline parameter and then specify that
property as an input to methods that test against the baseline data using the verifyEqualsBaseline method of the matlab.unittest.qualifications.Verifiable class. For more information, see
Create Baseline Tests for MATLAB Code.
See Also
Apps
Functions
Classes
matlab.unittest.TestCase|matlab.unittest.TestSuite|matlab.unittest.plugins.CodeCoveragePlugin|matlab.coverage.Result