Main Content

matlab.automation.streams.ToStandardOutput Class

Namespace: matlab.automation.streams
Superclasses: matlab.automation.streams.OutputStream

Output stream to write text to screen

Renamed from matlab.unittest.plugins.ToStandardOutput in R2023a

Description

The matlab.automation.streams.ToStandardOutput class provides an output stream to write text to the screen. Many text-oriented plugins that accept an output stream use a ToStandardOutput instance as their default stream.

The matlab.automation.streams.ToStandardOutput class is a handle class.

Creation

stream = matlab.automation.streams.ToStandardOutput creates an output stream to write text to the screen.

Examples

collapse all

Run tests using a plugin that directs the names of tests being run to an output stream. Pass a ToStandardOutput instance to the plugin so that it directs the text it produces to the screen.

Create Custom Plugin

In a file named ExamplePlugin.m in your current folder, create the ExamplePlugin class, which overrides the runTest method of TestRunnerPlugin. The plugin directs the name of each test being run to the output stream specified during construction of the plugin.

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

Create Example Test Class

In a file named ZerosTest.m in your current folder, create the ZerosTest class, which tests the zeros function.

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

Add Plugin to Test Runner and Run Tests

To run the tests, first import the classes used in this example.

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

Create a test suite from the ZerosTest class.

suite = testsuite("ZerosTest");

Create a test runner with no plugins. This code creates a silent runner that produces no output.

runner = testrunner("minimal");

You can now add any plugins you choose. Create an ExamplePlugin instance that directs text output to the screen.

plugin = ExamplePlugin(ToStandardOutput);

Add the plugin to the test runner and run the tests. As the tests run, the names of the tests appear on the screen.

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

Version History

Introduced in R2014a

expand all