matlab.unittest.TestRunner 类
命名空间: matlab.unittest
用于在 matlab.unittest
框架中运行测试的类
描述
matlab.unittest.TestRunner
类是用于在 matlab.unittest
框架中运行一套测试的基本 API。该类对 TestSuite
数组运行和操作。使用该类可自定义正在运行的测试。
matlab.unittest.TestRunner
类是一个 handle
类。
创建对象
要创建简单的静默 TestRunner
对象,请调用 withNoPlugins
静态方法。
runner = matlab.unittest.TestRunner.withNoPlugins;
要创建 TestRunner
对象以通过 MATLAB® 命令行窗口运行测试,请调用 withTextOutput
静态方法。
runner = matlab.unittest.TestRunner.withTextOutput;
要创建自定义的 TestRunner
对象,请调用 addPlugin
方法。
runner = matlab.unittest.TestRunner.withNoPlugins; runner.addPlugin(SomePlugin())
属性
ArtifactsRootFolder
— 存储测试运行工件的根文件夹
字符串标量 | 字符向量
存储测试运行工件的根文件夹,指定为字符串标量或字符向量。默认情况下,ArtifactsRootFolder
的值来自 string(tempdir)
,但您可以将其设置为任何可写文件夹。
测试运行期间生成的任何工件都存储在 ArtifactsRootFolder
的子文件夹中。子文件夹名称是与特定测试运行相关联的唯一标识符。仅当测试运行产生工件时,MATLAB 才创建子文件夹。
例如,假设 ArtifactsRootFolder
设置为 "C:\Temp"
,并且自动生成的测试运行标识符为 "1231df38-7515-4dbe-a869-c3d9f885f379"
。如果测试运行产生工件 "artifact.txt"
,则工件存储为 "C:\Temp\1231df38-7515-4dbe-a869-c3d9f885f379\artifact.txt"
。
PrebuiltFixtures
— 在测试运行器外设置的脚手架
Fixture
实例标量 | Fixture
实例的行向量
在测试运行器外设置的脚手架,指定为 matlab.unittest.fixtures.Fixture
实例的标量或行向量。使用此属性指定在脚手架设置和拆解期间手动进行环境配置,而不是自动执行配置操作。
测试运行器会认为这些脚手架已设置妥当,并且不会尝试设置或拆解由 PrebuiltFixtures
属性指定的任何脚手架。如果测试套件需要使用共享测试脚手架,且该脚手架被指定为预置脚手架,则测试运行器不会执行设置或拆解操作。
注意
仅当预置脚手架是由 PrebuiltFixtures
属性指定,且作为 SharedTestFixture
在测试类定义中列出时,测试运行器才会使用它。如果预置脚手架是使用 TestCase.applyFixture
方法注册的,则测试运行器不会使用它。
方法
公共方法
matlab.unittest.TestRunner.withNoPlugins | 创建没有插件的最小测试运行器 |
matlab.unittest.TestRunner.withTextOutput | 创建用于命令行窗口输出的 TestRunner 对象 |
addPlugin | 将插件添加到测试运行器 |
addModelCoverage (Simulink Test) | Enable model coverage collection for Simulink tests |
addSimulinkTestResults (Simulink Test) | Enable pushing test results to Simulink Test Manager |
run | 运行测试套件 |
runInParallel | 并行运行 TestSuite 数组中的所有测试 |
示例
创建以文本方式显示输出的 TestRunner 对象
将 matlab.unittest
类添加到当前导入列表。
import matlab.unittest.TestRunner import matlab.unittest.TestSuite
创建一个 TestSuite
数组。
suite = TestSuite.fromClass(?mypackage.MyTestClass);
创建 TestRunner
对象并运行套件。
runner = TestRunner.withTextOutput; result = run(runner,suite);
包含预置脚手架
此示例使用一个共享测试脚手架,然后将其指定为预置脚手架。测试运行器不会对预置脚手架进行设置或拆解。由于测试假设脚手架存在,因此您必须执行脚手架通常会执行的设置工作。
在工作文件夹下的文件中创建测试类。测试类使用 PathFixture
作为共享测试脚手架。此示例假定工作文件夹中存在子文件夹 helperFiles
。
classdef (SharedTestFixtures={ ... matlab.unittest.fixtures.PathFixture('helperFiles')}) ... SampleTest < matlab.unittest.TestCase methods(Test) function test1(testCase) f = testCase.getSharedTestFixtures; import matlab.unittest.constraints.ContainsSubstring testCase.assertThat(path,ContainsSubstring(f.Folder)) end end end
在命令提示符下创建一个测试套件和测试运行器。
import matlab.unittest.TestRunner import matlab.unittest.TestSuite suite = TestSuite.fromClass(?SampleTest); runner = TestRunner.withTextOutput;
使用共享测试脚手架运行测试。在这种情况下,脚手架非预置脚手架。
runner.run(suite);
Setting up PathFixture Done setting up PathFixture: Added 'C:\Work\helperFiles' to the path. __________ Running SampleTest . Done SampleTest __________ Tearing down PathFixture Done tearing down PathFixture: Restored the path to its original state. __________
测试运行器会对共享测试脚手架进行设置和拆解。
创建一个脚手架实例并将其添加到测试运行器。
f = matlab.unittest.fixtures.PathFixture('helperFiles');
runner.PrebuiltFixtures = f;
手动将 'helperFiles'
文件夹添加到您的路径中。PathFixture
将指定文件夹添加到您的路径中,测试将依赖此设置进行操作。但是,由于脚手架被定义为预置脚手架,因此测试运行器不会执行设置或拆解操作,您必须手动执行这些操作。在这种情况下,如果您不手动将其添加到您的路径中,测试将会失败。
p = fullfile(pwd,'helperFiles');
oldPath = addpath(p);
运行测试。
runner.run(suite);
Running SampleTest . Done SampleTest __________
测试运行器假设脚手架为内置脚手架,不会对其进行设置或拆解。
手动重置路径。
path(oldPath)
版本历史记录
在 R2013a 中推出
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)