createTestClassInstance

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

Extend creation of class-level TestCase instances

Description

example

tc = createTestClassInstance(plugin,pluginData) extends the creation of class-level TestCase instances, and returns the modified TestCase instance, tc. 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 of 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 testing framework passes the instance to any method with the TestClassSetup or TestClassTeardown attribute.

Input Arguments

plugin

Plugin object, specified as an instance of the matlab.unittest.plugins.TestRunnerPlugin class.

pluginData

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

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