Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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
matlab.unittest.TestSuite.runRun TestSuite array using TestRunner object configured for text output
matlab.unittest.TestRunner.runRun all tests in TestSuite array

Classes

matlab.unittest.TestCaseSuperclass of all matlab.unittest test classes
matlab.unittest.TestSuiteClass for grouping tests to run
matlab.unittest.TestSpecification of a 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

Examples and How To

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.

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.

Create Basic Parameterized Test

This example shows how to create a basic parameterized test.

Create Advanced Parameterized Test

This example shows how to create a test that is parameterized in the TestClassSetup, TestMethodSetup, and Test methods blocks.

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 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 test 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 rerun failed tests.

Concepts

Types of Qualifications

Qualifications are functions for testing values and responding to failures. This summary describes the four types of qualifications and qualification functions that are available in the MATLAB unit testing framework.

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.

Was this topic helpful?