Main Content

append

Class: matlab.unittest.plugins.plugindata.ResultDetails
Namespace: matlab.unittest.plugins.plugindata

Add data to test result details

Since R2020a

Description

append(resultDetails,field,data) appends data to a field of the Details property of a TestResult array. If field does not exist, the method adds it to the Details structure and stores data in the added field.

When you invoke append within the scope of a plugin class method, the append operation applies to all of the TestResult objects affected by the plugin method. For example, if you invoke append within the scope of the runSession method of TestRunnerPlugin, then the same data is added to all TestResult objects belonging to the test session.

example

Input Arguments

expand all

Modifier of the test result details, specified as an instance of the matlab.unittest.plugins.plugindata.ResultDetails class.

Field name, specified as a character vector or string scalar. Valid field names begin with a letter and can contain letters, digits, and underscores. The maximum length of a field name is the value that the namelengthmax function returns.

Data to append to the field, specified as a scalar or an array of objects. For example, you can specify data as a numeric scalar, string array, cell array, structure, or class object.

You can call the append method on a specific field and append data to that field as many times as you want. If you append elements of unlike classes to a field, MATLAB® converts some elements so that all elements in the field are of the same type. For more information, see Valid Combinations of Unlike Classes.

Examples

expand all

The testing framework can divide the test suite into separate groups and run each group on the current parallel pool (requires Parallel Computing Toolbox™). Create a plugin that adds the group number to TestResult objects.

In a file in your current folder, create the parallelizable plugin class ExamplePlugin, which overrides the runTestSuite method of TestRunnerPlugin. Add a Group field containing the group number to the Details property of the TestResult objects corresponding to the group.

classdef ExamplePlugin < ...
        matlab.unittest.plugins.TestRunnerPlugin & ...
        matlab.unittest.plugins.Parallelizable
    
    methods (Access = protected)
        function runTestSuite(plugin,pluginData)
            
            % Inspect pluginData to get the TestSuite group number
            groupNumber = pluginData.Group;
            
            % Add the group number to TestResult objects
            resultDetails = pluginData.ResultDetails;
            resultDetails.append('Group',groupNumber)
            
            % Invoke the superclass method
            runTestSuite@matlab.unittest.plugins.TestRunnerPlugin(plugin,pluginData);
        end    
    end
end

In your current folder, create a file named ExampleTest.m containing this parameterized test class. This class results in 300 tests for comparing pseudorandom integers between 1 and 10.

classdef ExampleTest < matlab.unittest.TestCase
    properties (TestParameter)
        num1 = num2cell(randi(10,1,10));
        num2 = num2cell(randi(10,1,10));
    end
    
    methods(Test)
        function testAssert(testCase,num1,num2)
            testCase.assertNotEqual(num1,num2)
        end
        function testVerify(testCase,num1,num2)
            testCase.verifyNotEqual(num1,num2)
        end
        function testAssume(testCase,num1,num2)
            testCase.assumeNotEqual(num1,num2)
        end
    end
end

At the command prompt, create a test suite from the ExampleTest class.

suite = testsuite('ExampleTest');

Create a TestRunner instance with no plugins, add ExamplePlugin to the runner, and then run the tests in parallel.

import matlab.unittest.TestRunner
runner = TestRunner.withNoPlugins;
runner.addPlugin(ExamplePlugin)
result = runner.runInParallel(suite);
Split tests into 18 groups and running them on 6 workers.
----------------
Finished Group 1
----------------

----------------
Finished Group 2
----------------

----------------
Finished Group 3
----------------

----------------
Finished Group 4
----------------

----------------
Finished Group 5
----------------

----------------
Finished Group 6
----------------

----------------
Finished Group 7
----------------

----------------
Finished Group 8
----------------

----------------
Finished Group 9
----------------

-----------------
Finished Group 10
-----------------

-----------------
Finished Group 11
-----------------

-----------------
Finished Group 12
-----------------

-----------------
Finished Group 13
-----------------

-----------------
Finished Group 14
-----------------

-----------------
Finished Group 15
-----------------

-----------------
Finished Group 16
-----------------

-----------------
Finished Group 17
-----------------

-----------------
Finished Group 18
-----------------

Retrieve the group number for the first and last Test elements.

groupOfFirst = result(1).Details.Group
groupOfLast = result(end).Details.Group
groupOfFirst =

     1


groupOfLast =

    18

Extended Capabilities

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced in R2020a