Main Content

matlab.unittest.TestCase class

Package: matlab.unittest
Superclasses: matlab.unittest.qualifications.Assertable, matlab.unittest.qualifications.Assumable, matlab.unittest.qualifications.FatalAssertable, matlab.unittest.qualifications.Verifiable

Superclass of all test classes

Description

The matlab.unittest.TestCase class is the superclass of all test classes in MATLAB®. It provides an interface to write and identify test content, including test fixture setup and teardown routines.

Creating class-based tests requires subclassing the TestCase class. To specify tests and test fixtures, subclasses can leverage framework-specific attributes. For more information, see TestCase Class Attributes, TestCase Method Attributes, and TestCase Property Attributes.

The matlab.unittest.TestCase class is a handle class.

Class Attributes

Abstract
true

For information on class attributes, see Class Attributes.

Creation

In most cases, you are not required to create an instance of the TestCase class directly. The test runner automatically creates TestCase instances when running the tests.

To create a TestCase instance for interactive, command-line testing, use the forInteractiveUse static method.

Methods

expand all

Events

This table lists the events of the TestCase class. In addition to these events, the TestCase class inherits events from the Assertable, Assumable, FatalAssertable, and Verifiable classes.

Event NameTriggerEvent DataEvent Attributes
ExceptionThrownTriggered when the test runner catches an exception in the test content. An ExceptionEventData object is passed to listener callback functions.matlab.unittest.qualifications.ExceptionEventData

NotifyAccess: private

ListenAccess: public

DiagnosticLoggedTriggered upon a call to the log method. A LoggedDiagnosticEventData object is passed to listener callback functions.matlab.unittest.diagnostics.LoggedDiagnosticEventData

NotifyAccess: private

ListenAccess: public

Examples

collapse all

Test the properties of a figure by creating and running a test class.

In a file named FigurePropertiesTest.m in your current folder, create the FigurePropertiesTest test class by:

  • Subclassing the matlab.unittest.TestCase class

  • Adding a property to represent the figure to test

  • Adding setup and teardown code for each test in a TestMethodSetup methods block

  • Adding tests in a Test methods block

classdef FigurePropertiesTest < matlab.unittest.TestCase
    properties
        TestFigure
    end

    methods (TestMethodSetup)
        function createFigure(testCase)
            testCase.TestFigure = figure;
            testCase.addTeardown(@close,testCase.TestFigure)
        end
    end

    methods (Test)
        function defaultCurrentPoint(testCase)
            cp = testCase.TestFigure.CurrentPoint;
            testCase.verifyEqual(cp,[0 0], ...
                "Default current point must be [0 0].")
        end

        function defaultCurrentObject(testCase)
            import matlab.unittest.constraints.IsEmpty
            co = testCase.TestFigure.CurrentObject;
            testCase.verifyThat(co,IsEmpty, ...
                "Default current object must be empty.")
        end
    end
end

Run the tests in the test class. In this example, both of the tests pass.

results = runtests("FigurePropertiesTest")
Running FigurePropertiesTest
.
.
Done FigurePropertiesTest
__________
results = 
  1×2 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

Totals:
   2 Passed, 0 Failed, 0 Incomplete.
   1.1899 seconds testing time.

More About

expand all

Tips

  • Defining constructor or destructor methods in a TestCase subclass is not recommended. TestCase constructor and destructor methods are not considered test content and should not be used to perform qualifications. For example, the SampleTest class specifies qualifications using a constructor method and a Test method. However, the qualification in the constructor method does not produce a test failure. The testing framework reports only one test failure as a result of the qualification performed within the testSize method.

    classdef SampleTest < matlab.unittest.TestCase
        methods
            function testCase = SampleTest % Constructor method not recommended
                testCase.verifyEqual(1,2)  % Does not produce a test failure
            end
        end
    
        methods (Test)
           function testSize(testCase)
               testCase.verifySize([1 2 3; 4 5 6],[2 4]) % Produces a test failure
           end
        end
    end
    

Version History

Introduced in R2013a

expand all