runInParallel
类: matlab.unittest.TestRunner
包: matlab.unittest
并行运行 TestSuite
数组中的所有测试
说明
results = runInParallel(
将指定的测试套件分成多个组,并使用指定的测试运行程序在 runner
,suite
)gcp
(Parallel Computing Toolbox) 函数返回的并行池上运行每个组。该方法以 TestResult
对象数组形式返回结果。
当测试并行(需要 Parallel Computing Toolbox™)运行时,测试套件部分在 MATLAB® 工作进程上独立运行。例如,如果您的测试类有 TestClassSetup
方法,则该方法在每个工作进程上本地运行。工作进程使用其对应的 Test
元素中的信息来运行测试。每个 Test
元素为工作进程提供运行一个测试所需的所有信息。
注意
测试框架可能会改变组的顺序和数量,或每个组中所包含的测试。
输入参数
runner
— 并行测试组的测试运行程序
matlab.unittest.TestRunner
实例
并行测试组的测试运行程序,指定为 matlab.unittest.TestRunner
实例。
并行运行测试前,请先考虑测试运行程序配置。由于 runInParallel
方法在不同的工作进程上运行不同的测试组,有些插件(例如 StopOnFailuresPlugin
)并不适合并行处理。测试框架支持使用自定义插件并行运行测试,前提是该插件会子类化 Parallelizable
接口。
suite
— 要并行运行的测试集合
matlab.unittest.Test
数组
要并行运行的测试集合,指定为 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 中推出R2021a: 在基于线程的池中并行运行测试
您可以通过启动线程工作进程的并行池,然后调用 runInParallel
方法,在基于线程的池上运行测试(需要 Parallel Computing Toolbox)。
在基于线程的池上使用 runInParallel
运行的测试存在以下限制:
您的测试和源代码只能使用线程工作进程支持的功能。有关基于线程的环境的限制的详细信息,请参阅Choose Between Thread-Based and Process-Based Environments (Parallel Computing Toolbox)。
基于线程的池不支持使用
matlab.unittest.TestSuite.fromFile
、matlab.unittest.TestSuite.fromFolder
或matlab.unittest.TestSuite.fromProject
创建的测试套件。基于线程的池不支持存储测试工件。
基于线程的环境不支持 Simulink®。因此,使用 Simulink Test™ 编写的测试无法在基于线程的池上运行。
R2020b: 在集群和云上并行运行测试
您可以使用 runInParallel
在集群和云上并行运行测试(需要 MATLAB Parallel Server 和 Parallel Computing Toolbox)。
R2020b: 使用独立应用程序并行运行测试
您可以创建支持并行运行测试的独立应用程序(需要 MATLAB Compiler™ 和 Parallel Computing Toolbox)。在代码中使用指令 %#function parallel.Pool
,以便 MATLAB Compiler 可以找到并打包并行运行测试所需的所有组件。有关详细信息,请参阅编译 MATLAB 单元测试。
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)