Main Content

createTestMethodInstance

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

Extend creation of method-level TestCase instances

Description

example

tc = createTestMethodInstance(plugin,pluginData) extends the creation of method-level TestCase instances and returns the modified TestCase instance. The testing framework evaluates this method within the scope of the runTest method.

A typical implementation of this method is to add listeners to various events originating from the method-level instance. Because the TestCase class inherits from the handle class, add listeners by calling the addlistener method from within the createTestMethodInstance method. The testing framework creates instances for every element of the matlab.unittest.Test array and passes each instance to its corresponding Test methods and to any method with the TestMethodSetup or TestMethodTeardown attribute.

Input Arguments

expand all

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

Method-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 createTestMethodInstance method to count the number of method-level assumption failures.

Add a listener to listen for assumption failures. Use the captureMethodLevelAssumptionFailureData helper method to populate the TestMethodAssumptionFailureData property.

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

    methods (Access=protected)
        function testCase = createTestMethodInstance(plugin,pluginData)
            testCase = createTestMethodInstance@ ...
                matlab.unittest.plugins.TestRunnerPlugin(plugin,pluginData);

            instanceName = pluginData.Name;
            testCase.addlistener('AssumptionFailed',@(~,evd) ...
                plugin.captureMethodLevelAssumptionFailureData(evd,instanceName))
        end
    end

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

Version History

Introduced in R2014a