Main Content

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

matlab.unittest.TestRunner 类

命名空间: matlab.unittest

用于在 matlab.unittest 框架中运行测试的类

描述

matlab.unittest.TestRunner 类是用于在 matlab.unittest 框架中运行一套测试的基本 API。该类对 TestSuite 数组运行和操作。使用该类可自定义正在运行的测试。

matlab.unittest.TestRunner 类是一个 handle 类。

类属性

Sealed
true

有关类属性的信息,请参阅类属性

创建对象

要创建简单的静默 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 的值来自 string(tempdir),但您可以将其设置为任何可写文件夹。

测试运行期间生成的任何工件都存储在 ArtifactsRootFolder 的子文件夹中。子文件夹名称是与特定测试运行相关联的唯一标识符。仅当测试运行产生工件时,MATLAB 才创建子文件夹。

例如,假设 ArtifactsRootFolder 设置为 "C:\Temp",并且自动生成的测试运行标识符为 "1231df38-7515-4dbe-a869-c3d9f885f379"。如果测试运行产生工件 "artifact.txt",则工件存储为 "C:\Temp\1231df38-7515-4dbe-a869-c3d9f885f379\artifact.txt"

在测试运行器外设置的脚手架,指定为 matlab.unittest.fixtures.Fixture 实例的标量或行向量。使用此属性指定在脚手架设置和拆解期间手动进行环境配置,而不是自动执行配置操作。

测试运行器会认为这些脚手架已设置妥当,并且不会尝试设置或拆解由 PrebuiltFixtures 属性指定的任何脚手架。如果测试套件需要使用共享测试脚手架,且该脚手架被指定为预置脚手架,则测试运行器不会执行设置或拆解操作。

注意

仅当预置脚手架是由 PrebuiltFixtures 属性指定,且作为 SharedTestFixture 在测试类定义中列出时,测试运行器才会使用它。如果预置脚手架是使用 TestCase.applyFixture 方法注册的,则测试运行器不会使用它。

方法

全部展开

示例

全部折叠

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 中推出