matlab.unittest.TestCase class

Package: matlab.unittest

Superclass of all matlab.unittest test classes

Description

The TestCase class is the means by which a test is written in the matlab.unittest framework. It provides the means to write and identify test content, as well as test fixture setup and teardown routines. Creating such a test requires deriving from TestCase to produce a TestCase subclass. Then, subclasses can leverage the metadata attributes to specify tests and test fixtures.

Construction

    Note:   The ability to directly construct TestCase instances by calling the matlab.unittest.TestCase constructor will be removed in a future release. For interactive, command line use, instantiate a TestCase using the forInteractiveUse static method.

Use the forInteractiveUse static method to create a TestCase for interactive, command line use. When tests are run in the framework, TestCase instances are constructed by the matlab.unittest.TestRunner.

Methods

addTeardownDynamically add teardown routine
applyFixtureUse fixture with TestCase
forInteractiveUseCreate TestCase for interactive use
getSharedTestFixturesProvide access to shared test fixtures
logRecord diagnostic information
runRun TestCase test

Inherited Methods

The TestCase class inherits methods from the following classes:

matlab.unittest.qualifications.AssertableQualification to validate preconditions of a test
matlab.unittest.qualifications.AssumableQualification to filter test content
matlab.unittest.qualifications.FatalAssertableQualification to abort test execution
matlab.unittest.qualifications.VerifiableQualification to produce soft-failure conditions

Attributes

Class Attributes

TestCase objects support the following class level attributes. Specify class-level attributes in the classdef block before the class name.

SharedTestFixturesClass block to contain shared test fixtures. You must define SharedTestFixtures as a cell array of matlab.unittest.fixtures.Fixture instances.

Method Attributes

Classes that derive from TestCase can define methods blocks which contain matlab.unittest framework-specific attributes to specify test content.

TestMethod block to contain test methods.
TestMethodSetupMethod block to contain setup code.
TestMethodTeardownMethod block to contain teardown code.
TestClassSetupMethod block to contain class level setup code.
TestClassTeardownMethod block to contain class level teardown code.
ParameterCombinationMethod block to contain parameterized testing code. This attribute accepts the following values:
  • 'exhaustive' (default): Test methods are invoked for all combinations of parameters.

  • 'sequential': Test methods are invoked with corresponding values from each parameter. Each parameter must contain the same number of values.

  • 'pairwise': Test methods are invoked for every pair of parameter values at least once.

Property Attributes

Classes that derive from TestCase can define properties blocks which contain matlab.unittest framework-specific attributes to specify test content.

ClassSetupParameterProperty block to define parameterized testing properties for methods in the TestClassSetup block
MethodSetupParameterProperty block to define parameterized testing properties for methods in theMethodSetup block
TestParameterProperty block to define parameterized testing properties for methods in the Test block

Events

VerificationFailed

Triggered upon failing verification. A QualificationEventData object is passed to listener callback functions.

VerificationPassed

Triggered upon passing verification. A QualificationEventData object is passed to listener callback functions.

AssertionFailed

Triggered upon failing assertion. A QualificationEventData object is passed to listener callback functions.

AssertionPassed

Triggered upon passing assertion. A QualificationEventData object is passed to listener callback functions.

FatalAssertionFailed

Triggered upon failing fatal assertion. A QualificationEventData object is passed to listener callback functions.

FatalAssertionPassed

Triggered upon passing fatal assertion. A QualificationEventData object is passed to listener callback functions.

AssumptionFailed

Triggered upon failing assumption. A QualificationEventData object is passed to listener callback functions.

AssumptionPassed

Triggered upon passing assumption. A QualificationEventData object is passed to listener callback functions.

ExceptionThrown

Triggered by the TestRunner when an exception is thrown. An ExceptionEventData object is passed to listener callback functions.

DiagnosticLogged

Triggered by the TestRunner upon a call to the log method. A LoggedDiagnosticEventData object is passed to the listener callback functions.

Examples

expand all

Create Test Case Class

Create a test case class, FigurePropertiesTest, with TestMethodSetup and TestMethodTeardown methods.

classdef FigurePropertiesTest < matlab.unittest.TestCase
 
    properties
        TestFigure
    end
 
    methods(TestMethodSetup)
        function createFigure(testCase)
            testCase.TestFigure = figure;
        end
    end
 
    methods(TestMethodTeardown)
        function closeFigure(testCase)
            close(testCase.TestFigure)
        end
    end
 
    methods(Test)
 
        function defaultCurrentPoint(testCase)
 
            cp = testCase.TestFigure.CurrentPoint;
            testCase.verifyEqual(cp, [0 0], ...
                'Default current point is incorrect')
        end
 
        function defaultCurrentObject(testCase)
            import matlab.unittest.constraints.IsEmpty
 
            co = testCase.TestFigure.CurrentObject;
            testCase.verifyThat(co, IsEmpty, ...
                'Default current object should be empty')
        end
 
    end
 
end
Was this topic helpful?