Main Content

createTestClassInstance

Class: matlab.unittest.plugins.TestRunnerPlugin
Namespace: matlab.unittest.plugins

Extend creation of class-level TestCase instances

Description

tc = createTestClassInstance(plugin,pluginData) extends the creation of class-level TestCase instances and returns the modified TestCase instance. The testing framework uses the TestCase instance to customize running tests that belong to the same test class. The framework evaluates this method within the scope of the runTestClass method.

A typical implementation of this method is to add listeners to various events originating from the class-level instance. Because the TestCase class inherits from the handle class, add listeners by calling the addlistener method from within the createTestClassInstance method. For each class, the testing framework passes the instance to any method with the TestClassSetup or TestClassTeardown attribute.

example

Input Arguments

expand all

Plugin, specified as a matlab.unittest.plugins.TestRunnerPlugin object.

Class-level TestCase creation information, specified as a matlab.unittest.plugins.plugindata.TestContentCreationPluginData object. The testing framework uses this information to describe the test content to the plugin.

Attributes

Accessprotected

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Create a plugin and override the createTestClassInstance method to count the number of class-level assumption failures.

Add a listener to listen for assumption failures. Use the captureClassLevelAssumptionFailureData helper method to populate the TestClassAssumptionFailureData property.

classdef ExamplePlugin < matlab.unittest.plugins.TestRunnerPlugin
    properties (SetAccess=private)
        TestClassAssumptionFailureData = {};
    end

    methods (Access=protected)
        function testCase = createTestClassInstance(plugin,pluginData)
            % Invoke super class method
            testCase = createTestClassInstance@ ...
                matlab.unittest.plugins.TestRunnerPlugin(plugin,pluginData);

            % Get the test class name
            instanceName = pluginData.Name;

            % Add a listener to capture assumption failures
            testCase.addlistener('AssumptionFailed',@(~,evd) ...
                plugin.captureClassLevelAssumptionFailureData(evd,instanceName))
        end
    end

    methods (Access=private)
        function captureClassLevelAssumptionFailureData(plugin,eventData,instanceName)
            plugin.TestClassAssumptionFailureData{end+1} = struct( ...
                'InstanceName',instanceName, ...
                'ActualValue' ,eventData.ActualValue, ...
                'Constraint'  ,eventData.Constraint, ...
                'Stack'       ,eventData.Stack);
        end
    end
end

Version History

Introduced in R2014a