Main Content

createSharedTestFixture

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

Extend creation of shared test fixture instances

Description

example

f = createSharedTestFixture(plugin,pluginData) extends the creation of shared test fixtures and returns the modified Fixture instance, f. The testing framework uses the fixture instance to customize running tests that use shared fixtures. The framework evaluates this method within the scope of the runTestSuite method of TestRunnerPlugin for each shared test fixture it needs to set up. A typical implementation of this method is to add listeners to various events originating from the shared test fixture instance. Since the Fixture inherits from the handle class, add listeners by calling the addlistener method from within the createSharedTestFixture method.

Input Arguments

plugin

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

pluginData

Shared test fixture 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 shared test fixture assertion failures.

classdef ExamplePlugin < matlab.unittest.plugins.TestRunnerPlugin
    
    properties (SetAccess = private)
        FixtureAssertionFailureData = {};
    end
    
    methods (Access = protected)
        function fixture = createSharedTestFixture(plugin, pluginData)
            % Invoke the super class method
            fixture = createSharedTestFixture@...
                matlab.unittest.plugins.TestRunnerPlugin(plugin, pluginData);
            
            % Get the fixture name
            fixtureName = pluginData.Name;
            
            % Add a listener to fixture assertion failures
            % and capture the qualification failure information
            fixture.addlistener('AssertionFailed', @(~,evd) ...
                plugin.captureFixtureAssertionFailureData(evd, fixtureName))
        end
    end
    
    methods (Access = private)
        function captureFixtureAssertionFailureData(plugin, eventData, fixtureName)
            plugin.FixtureAssertionFailureData{end+1} = struct(...
                'FixtureName', fixtureName, ...
                'ActualValue', eventData.ActualValue, ...
                'Constraint' , eventData.Constraint, ...
                'Stack'      , eventData.Stack);
        end
    end
end
Introduced in R2014a