Class-Based Unit Tests

Write class-based tests; parameterize tests; apply fixtures; select and run tests

Write xUnit-style tests to check that the output of MATLAB® code is as you expect. Class-based unit tests give you access to the full unit testing framework functionality. For example, you can write parameterized tests, tag your tests, or use shared test fixtures.

To get started, see Author Class-Based Unit Tests in MATLAB and Write Simple Test Case Using Classes.

Functions

runtestsRun set of tests
testsuiteCreate suite of tests
run (TestSuite)Run TestSuite array using TestRunner object configured for text output
run (TestRunner)Run all tests in TestSuite array
run (TestCase)Run TestCase test

Classes

matlab.unittest.TestCaseSuperclass of all matlab.unittest test classes
matlab.unittest.TestSuiteClass for grouping tests to run
matlab.unittest.TestSpecification of single test method
matlab.unittest.TestRunnerClass for running tests in matlab.unittest framework
matlab.unittest.TestResultResult of running test suite

Packages

matlab.unittestSummary of packages and classes in MATLAB Unit Test Framework
matlab.unittest.qualificationsSummary of classes in MATLAB Qualifications Interface
matlab.unittest.constraintsSummary of classes in MATLAB Constraints Interface
matlab.unittest.diagnosticsSummary of classes in MATLAB Diagnostics Interface
matlab.unittest.fixturesSummary of classes in MATLAB Fixtures Interface
matlab.unittest.parametersSummary of classes associated with MATLAB Unit Test parameters
matlab.unittest.pluginsSummary of classes in MATLAB Plugins Interface
matlab.unittest.selectorsSummary of classes in MATLAB Selectors Interface
Simulink.sdi.constraints.MatchesSignalConstraint that compares time series data with tolerances using the Simulation Data Inspector

Topics

Write Tests

Author Class-Based Unit Tests in MATLAB

To test a MATLAB program, write a unit test using qualifications that are methods for testing values and responding to failures.

Table of Verifications, Assertions, and Other Qualifications

Test values and respond to failures using verifications, assumptions, assertions, and fatal assertions.

Write Simple Test Case Using Classes

This example shows how to write a class-based unit test.

Write Setup and Teardown Code Using Classes

Setup and teardown code, or test fixtures, sets up the pretest state of the system and returns it to the original state after running the test. You can define setup and teardown methods that run before and after each test method or all test methods in a test class.

Tag Unit Tests

Use test tags to group tests into categories and then run tests with specified tags. Typical test tags identify a particular feature or describe the type of test.

Write Tests Using Shared Fixtures

You can share test fixtures across test classes. This example shows how to use shared fixtures when creating tests.

Write Parameterized Test

Use Parameters in Class-Based Tests

Use parameters to provide data that should be used iteratively by your tests.

Create Basic Parameterized Test

Test a function by creating a test that is parameterized in the Test methods block.

Create Advanced Parameterized Test

Create a test that is parameterized in the TestClassSetup, TestMethodSetup, and Test methods blocks.

Use External Parameters in Parameterized Test

Use data-driven testing to inject variable inputs into your parameterized test.

Run Tests and Analyze Results

Create Simple Test Suites

This example shows how to combine tests into test suites, whether they are organized in packages and classes or files and folders, or both.

Run Tests in Editor

Run your tests while working in the Editor or Live Editor.

Run Tests for Various Workflows

Explore different ways to run tests.

Programmatically Access Test Diagnostics

If you run tests with the runtests function or the run method of TestSuite or TestCase, the testing framework uses a DiagnosticsRecordingPlugin plugin that records diagnostics on test results.

Add Plugin to Test Runner

This example shows how to add a plugin to the test runner.

Analyze Test Case Results

This example shows how to analyze the information returned by a test runner.

Analyze Failed Test Results

This example shows how to identify and handle failed tests.

Rerun Failed Tests

Rerun failed tests quickly and conveniently.

Dynamically Filtered Tests

Assumption failures produce filtered tests that are marked as Incomplete instead of producing test failures. To avoid test code that is not executed, monitor the filtered tests.

Compile MATLAB Unit Tests

Create a standalone application to run your tests on target machines that do not have MATLAB installed.