createTestClassInstance

Class: matlab.unittest.plugins.TestRunnerPlugin
Package: matlab.unittest.plugins

Extend creation of class-level TestCase instances

Syntax

tc = createTestClassInstance(plugin,pluginData)

Description

example

tc = createTestClassInstance(plugin,pluginData) extends the creation of class-level TestCase instances, and returns the modified TestCase instance, tc. The test framework uses the TestCase instance to customize running tests that belong to the same test class. The test framework evaluates this method within the scope of the runTestClass method of the TestRunnerPlugin. A typical implementation of this method is to add listeners to various events originating from the class level instance. Since the TestCase inherits from the handle class, add listeners by calling the addlistener method from within the createTestClassInstance method. For each class, the test framework passes the instance to any method with the TestClassSetup or TestClassTeardown attribute.

Input Arguments

plugin

Instance of matlab.unittest.plugins.TestRunnerPlugin.

pluginData

Class-level TestCase creation information, specified as an instance of matlab.unittest.plugins.plugindata.TestContentCreationPluginData. The test framework uses this information to introspect into the test content.

Examples

expand all

Extend the running of tests to count the number of class-level assumption failures.

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

Introduced in R2014a

Was this topic helpful?