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

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 to TestCase instance
applyFixtureUse fixture with TestCase
forInteractiveUseCreate TestCase for interactive use
getSharedTestFixturesProvide access to shared test fixtures
logRecord diagnostic information during test execution
onFailureDynamically add diagnostics for test failures
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.
TestTagsClass block to contain tests tagged with a specified value. You must define TestTags as a cell array of non-empty character vectors or an array of non-empty strings, where each element is a tag for the test.

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.
ParameterCombination

Method 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.

TestTagsMethod block to contain tests tagged with a specified value. You must define TestTags as a cell array of non-empty character vectors or an array of non-empty strings, where each element is a tag for the test.

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 the TestMethodSetup 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

collapse all

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
Introduced in R2013a