Write Unit Tests
Write tests using the MATLAB® unit testing framework to check that the outputs of MATLAB scripts, functions, or classes are as you expect. For example, you can test that actual output values match expected values, or you can test that outputs have the expected size and type.
The unit testing framework lets you write tests using scripts, functions, or classes:
Script-based tests provide basic test authoring functionality. You can perform basic qualifications by using the
assert
function in your test scripts.Function-based tests follow the xUnit testing philosophy and provide extensive test authoring functionality. For example, you can use advanced qualification capabilities, including constraints, tolerances, and test diagnostics.
Class-based tests give you access to the full framework functionality. For example, you can use shared test fixtures, parameterize tests, and reuse test content.
For more information, see Ways to Write Unit Tests.
Functions
assert | Throw error if condition false |
functiontests | Create array of tests from handles to local functions |
Classes
matlab.unittest.FunctionTestCase | Test case for function-based tests |
matlab.unittest.TestCase | Superclass of all test classes |
matlab.automation.diagnostics.Diagnostic | Fundamental interface for diagnostics |
matlab.automation.Verbosity | Verbosity level enumeration class |
Namespaces
matlab.unittest | Summary of classes and namespaces in MATLAB unit testing framework |
matlab.unittest.constraints | Summary of classes in MATLAB constraints interface |
matlab.unittest.fixtures | Summary of classes in MATLAB fixtures interface |
matlab.unittest.parameters | Summary of classes associated with MATLAB unit testing parameters |
matlab.unittest.qualifications | Summary of classes in MATLAB qualifications interface |
Topics
Write Class-Based Tests
- Class-Based Unit Tests
Test your MATLAB source code by creating class-based unit tests. - Write Simple Test Case Using Classes
Write class-based unit tests to determine the correctness of your program. - Write Setup and Teardown Code Using Classes
Specify setup and teardown code that runs before and after eachTest
method or allTest
methods in a test class. - Write Tests Using Shared Fixtures
Share fixtures across test classes when creating tests. - 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. - Insert Test Code Using Editor
Write a parameterized test interactively by using the options in the MATLAB Editor. - Table of Verifications, Assertions, and Other Qualifications
Test values and respond to failures using verifications, assumptions, assertions, and fatal assertions. - Write Independent and Repeatable Tests
Write class-based tests following best practices for test independence and repeatability. - Ways to Write Unit Tests
Choose a test authoring scheme that best suits your requirements and your preferred workflow.
Write Parameterized Tests
- Use Parameters in Class-Based Tests
Use parameters to provide data for your tests to use iteratively. - Create Basic Parameterized Test
Test a function by creating a test that is parameterized in theTest
methods
block. - Create Advanced Parameterized Test
Create a test that is parameterized in theTestClassSetup
,TestMethodSetup
, andTest
methods
blocks. - Define Parameters at Suite Creation Time
Use aTestParameterDefinition
method to define parameters at test suite creation time. - Use External Parameters in Parameterized Test
Use data-driven testing to inject variable inputs into your parameterized test.
Write Function-Based Tests
- Function-Based Unit Tests
A test function is a single MATLAB file that contains a main function and your individual local test functions. Optionally, you can include file fixture and fresh fixture functions. - Write Simple Test Case Using Functions
Write function-based unit tests to determine the correctness of your program. - Write Test Using Setup and Teardown Functions
Write a function-based test with setup and teardown functions that run once in your test file and before and after each test function in the file. - Extend Function-Based Tests
Access additional functionality using function-based tests, including application of fixtures, test selection, programmatic access of test diagnostics, and test runner customization.
Write Script-Based Tests
- Write Script-Based Unit Tests
Write a script to test a function that you create. - Write Test Using Live Script
Write a live script that tests a function that you create. - Write Script-Based Test Using Local Functions
Write a script-based test that uses local functions as helper functions. - Extend Script-Based Tests
Access additional functionality using script-based tests, including test selection, programmatic access of test diagnostics, and test runner customization.