To test a MATLAB® program, write a unit test using qualifications that are methods for testing values and responding to failures.
A test class must inherit from
matlab.unittest.TestCase and contain a
methods block with the
Test attribute. The
methods block contains functions, each of which is a unit test. A general, basic class definition follows.
%% Test Class Definition classdef MyComponentTest < matlab.unittest.TestCase %% Test Method Block methods (Test) % includes unit test functions end end
A unit test is a method that determines the correctness of a unit of software. Each unit test is contained within a methods block. The function must accept a
TestCase instance as an input.
%% Test Class Definition classdef MyComponentTest < matlab.unittest.TestCase %% Test Method Block methods (Test) %% Test Function function testASolution(testCase) %% Exercise function under test % act = the value from the function under test %% Verify using test qualification % exp = your expected value % testCase.<qualification method>(act,exp); end end end
Qualifications are methods for testing values and responding to failures. This table lists the types of qualifications.
Use this qualification to produce and record failures without throwing an exception. The remaining tests run to completion.
Use this qualification to ensure that a test runs only when certain preconditions are satisfied. However, running the test without satisfying the preconditions does not produce a test failure. When an assumption failure occurs, the testing framework marks the test as filtered.
Use this qualification to ensure that the preconditions of the current test are met.
Use this qualification when the failure at the assertion point renders the remainder of the current test method invalid or the state is unrecoverable.
The MATLAB unit testing framework provides approximately 25 qualification methods
for each type of qualification. For example, use
assertClass to test that a value is of an expected class, and
fatalAssertTrue to test if the
actual value is true. For a summary of qualification methods, see Table of Verifications, Assertions, and Other Qualifications.
Often, each unit test function obtains an actual value by exercising the code that you are testing and defines the associated expected value. For example, if you are testing the
plus function, the actual value might be
plus(2,3) and the expected value
5. Within the test function, you pass the actual and expected values to a qualification method. For example:
For an example of a basic unit test, see Write Simple Test Case Using Classes.
The MATLAB unit testing framework includes several features for authoring more advanced test classes:
Setup and teardown methods blocks to implicitly set up the pretest state of the system and return it to the original state after running the tests. For an example of a test class with setup and teardown code, see Write Setup and Teardown Code Using Classes.
Ready-to-use fixtures for handling the setup and teardown of frequently used testing actions and for sharing fixtures between classes. For more information, see
matlab.unittest.fixtures and Write Tests Using Shared Fixtures.