Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

runInParallel

类: matlab.unittest.TestRunner
命名空间: matlab.unittest

并行运行 TestSuite 数组中的所有测试

说明

示例

results = runInParallel(runner,suite) 将指定的测试套件分成多个组,并使用指定的测试运行器在 gcp (Parallel Computing Toolbox) 函数返回的并行池上运行每个组。该方法以 TestResult 对象数组形式返回结果。

当测试并行(需要 Parallel Computing Toolbox™)运行时,测试套件部分在 MATLAB® 工作进程上独立运行。例如,如果您的测试类有 TestClassSetup 方法,则该方法在每个工作进程上本地运行。工作进程使用其对应的 Test 元素中的信息来运行测试。每个 Test 元素为工作进程提供运行一个测试所需的所有信息。

注意

测试框架可能会改变组的顺序和数量,或每个组中所包含的测试。

输入参数

全部展开

并行测试组的测试运行器,指定为 matlab.unittest.TestRunner 实例。

并行运行测试前,请先考虑测试运行器配置。由于 runInParallel 方法在不同的工作进程上运行不同的测试组,有些插件(例如 StopOnFailuresPlugin)并不适合并行处理。测试框架支持使用自定义插件并行运行测试,前提是该插件会子类化 Parallelizable 接口。

要并行运行的测试集合,指定为 matlab.unittest.Test 数组。

示例

全部展开

在您的当前工作文件夹下的文件中,创建以下参数化测试。

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

在命令提示符下,基于 TestRand.m 创建一个套件和一个会在命令行窗口中显示文本的测试运行器。

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

套件包含 1200 个测试元素。并行运行测试。

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.

提示

  • 选择要并行运行的测试套件时,请考虑可能的资源争用。例如,如果您的测试脚手架要访问全局资源(例如同一网络上的数据库或共享文件),并行会话可能会相互冲突。在这种情况下,可考虑使用预置的共享测试脚手架。

  • 当您在远程并行池上运行测试时(需要 MATLAB Parallel Server™ 和 Parallel Computing Toolbox),MATLAB 首先将包含您的测试的本地文件夹复制到远程工作进程。为了最小化与此步骤相关联的开销,请确保这些文件夹只包含与您的测试相关的文件。

版本历史记录

在 R2015a 中推出

全部展开