Main Content

runInParallel

Class: matlab.unittest.TestRunner
Namespace: matlab.unittest

Run all tests in test suite in parallel

Description

results = runInParallel(runner,suite) divides the specified test suite into groups and uses the specified test runner to run each group on the parallel pool returned by the gcp (Parallel Computing Toolbox) function. The method returns the results as an array of TestResult objects.

When tests run in parallel (requires Parallel Computing Toolbox™), test suite portions run independently on MATLAB® workers. For example, if your test class has a TestClassSetup method, the method runs locally on each worker. Workers use the information in their corresponding Test elements to run the tests. Each Test element provides the worker with all the information required to run a test.

Note

The testing framework might vary the order and number of groups or which tests it includes in each group.

example

Input Arguments

expand all

Test runner for parallel test groups, specified as a matlab.unittest.TestRunner instance.

Consider your test runner configuration before running tests in parallel. Since the runInParallel method runs separate groups of tests on different workers, some plugins, such as StopOnFailuresPlugin, are not well suited for parallelization. The testing framework supports running tests in parallel with a custom plugin, provided that the plugin subclasses the Parallelizable interface.

Set of tests to run in parallel, specified as a matlab.unittest.Test array.

Examples

expand all

Create the following parameterized test in a file in your current working folder.

classdef TestRand < matlab.unittest.TestCase    
    properties (TestParameter)
        dim1 = createDimensionSizes;
        dim2 = createDimensionSizes;
        dim3 = createDimensionSizes;
        type = {'single','double'};
    end
    
    methods (Test)
        function testRepeatable(testCase,dim1,dim2,dim3)
            state = rng;
            firstRun = rand(dim1,dim2,dim3);
            rng(state)
            secondRun = rand(dim1,dim2,dim3);
            testCase.verifyEqual(firstRun,secondRun);
        end
        function testClass(testCase,dim1,dim2,type)
            testCase.verifyClass(rand(dim1,dim2,type),type)
        end
    end
end
 
function sizes = createDimensionSizes
% Create logarithmically spaced sizes up to 100
sizes = num2cell(round(logspace(0,2,10)));
end

At the command prompt, create a suite from TestRand.m and a test runner that displays text in the Command Window.

suite = matlab.unittest.TestSuite.fromClass(?TestRand);
runner = matlab.unittest.TestRunner.withTextOutput();

The suite contains 1200 test elements. Run the tests in parallel.

results = runInParallel(runner,suite)
Split tests into 12 groups and running them on 4 workers.
----------------
Finished Group 2
----------------
Running TestRand
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
.........
Done TestRand
__________


----------------
Finished Group 4
----------------
Running TestRand
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
.....
Done TestRand
__________


----------------
Finished Group 3
----------------
Running TestRand
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
.......
Done TestRand
__________


----------------
Finished Group 1
----------------
Running TestRand
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..
Done TestRand
__________


----------------
Finished Group 7
----------------
Running TestRand
..........
..........
..........
..........
..........
..........
..........
..........
..........
.........
Done TestRand
__________


----------------
Finished Group 5
----------------
Running TestRand
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
...
Done TestRand
__________


----------------
Finished Group 6
----------------
Running TestRand
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
.
Done TestRand
__________


----------------
Finished Group 8
----------------
Running TestRand
..........
..........
..........
..........
..........
..........
..........
..........
..........
.......
Done TestRand
__________


-----------------
Finished Group 11
-----------------
Running TestRand
..........
..........
..........
..........
..........
..........
..........
..........
..........
.
Done TestRand
__________


-----------------
Finished Group 12
-----------------
Running TestRand
..........
..........
..........
..........
..........
..........
..........
..........
........
Done TestRand
__________


-----------------
Finished Group 10
-----------------
Running TestRand
..........
..........
..........
..........
..........
..........
..........
..........
..........
...
Done TestRand
__________


----------------
Finished Group 9
----------------
Running TestRand
..........
..........
..........
..........
..........
..........
..........
..........
..........
.....
Done TestRand
__________



results = 

  1200x1 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

Totals:
   1200 Passed, 0 Failed, 0 Incomplete.
   11.4023 seconds testing time.

Tips

  • When you select a test suite to run in parallel, consider possible resource contention. For example, if your test fixtures access global resources, such as a database or a shared file on the same network, the parallel sessions could conflict with each other. In such cases, consider using a prebuilt shared test fixture.

  • When you run tests on a remote parallel pool (requires MATLAB Parallel Server™ and Parallel Computing Toolbox), MATLAB first copies the local folders containing your tests to the remote workers. To minimize the overhead associated with this step, make sure that these folders include only files that are relevant to your tests.

Version History

Introduced in R2015a

expand all