主要内容

matlab.automation.streams.ToStandardOutput 类

命名空间: matlab.automation.streams
超类: matlab.automation.streams.OutputStream

将文本写入屏幕的输出流

matlab.unittest.plugins.ToStandardOutput 起已在 R2023a 中重命名

描述

matlab.automation.streams.ToStandardOutput 类提供将文本写入屏幕的输出流。许多接受输出流的面向文本的插件使用 ToStandardOutput 实例作为其默认流。

matlab.automation.streams.ToStandardOutput 类是一个 handle 类。

创建对象

stream = matlab.automation.streams.ToStandardOutput 创建输出流以将文本写入屏幕。

示例

全部折叠

使用插件运行测试,该插件将正在运行的测试的名称定向到输出流。将 ToStandardOutput 实例传递给插件,以便它将生成的文本定向到屏幕上。

创建自定义插件

在当前文件夹中名为 ExamplePlugin.m 的文件中,创建 ExamplePlugin 类,该类覆盖 TestRunnerPluginrunTest 方法。该插件将正在运行的每个测试的名称定向至插件构造期间指定的输出流。

classdef ExamplePlugin < matlab.unittest.plugins.TestRunnerPlugin
    properties (SetAccess=immutable)
        Output
    end

    methods
        function plugin = ExamplePlugin(stream)
            arguments
                stream (1,1) matlab.automation.streams.OutputStream
            end
            plugin.Output = stream;
        end
    end

    methods (Access=protected)
        function runTest(plugin,pluginData)
            print(plugin.Output,"### Running test: %s\n",pluginData.Name)
            % Invoke the superclass method
            runTest@matlab.unittest.plugins.TestRunnerPlugin( ...
                plugin,pluginData)
        end
    end
end

创建示例测试类

在当前文件夹中名为 ZerosTest.m 的文件中,创建 ZerosTest 类来测试 zeros 函数。

classdef ZerosTest < matlab.unittest.TestCase
    properties (TestParameter)
        type = {'single','double','uint16'};
        size = struct("s2d",[3 3],"s3d",[2 5 4]);
    end
    
    methods (Test)
        function testClass(testCase,size,type)
            testCase.verifyClass(zeros(size,type),type)
        end
        
        function testSize(testCase,size)
            testCase.verifySize(zeros(size),size)
        end
        
        function testDefaultClass(testCase)
            testCase.verifyClass(zeros,"double")
        end

        function testDefaultSize(testCase)
            testCase.verifySize(zeros,[1 1])
        end
        
        function testDefaultValue(testCase)
            testCase.verifyEqual(zeros,0)
        end
    end
end

向测试运行器添加插件并运行测试

要运行测试,首先导入此示例中使用的类。

import matlab.unittest.TestRunner
import matlab.automation.streams.ToStandardOutput

根据 ZerosTest 类创建一个测试套件。

suite = testsuite("ZerosTest");

创建一个不含任何插件的测试运行器。以下代码将创建一个不产生任何输出的静默运行器。

runner = testrunner("minimal");

您现在可以添加任何您选择的插件。创建一个将文本输出定向到屏幕的 ExamplePlugin 实例。

plugin = ExamplePlugin(ToStandardOutput);

将该插件添加到测试运行器中并运行测试。测试运行时,测试的名称会出现在屏幕上。

runner.addPlugin(plugin)
results = runner.run(suite);
### Running test: ZerosTest/testClass(size=s2d,type=single)
### Running test: ZerosTest/testClass(size=s2d,type=double)
### Running test: ZerosTest/testClass(size=s2d,type=uint16)
### Running test: ZerosTest/testClass(size=s3d,type=single)
### Running test: ZerosTest/testClass(size=s3d,type=double)
### Running test: ZerosTest/testClass(size=s3d,type=uint16)
### Running test: ZerosTest/testSize(size=s2d)
### Running test: ZerosTest/testSize(size=s3d)
### Running test: ZerosTest/testDefaultClass
### Running test: ZerosTest/testDefaultSize
### Running test: ZerosTest/testDefaultValue

版本历史记录

在 R2014a 中推出

全部展开