matlab.unittest.plugins.ToStandardOutput class

Package: matlab.unittest.plugins
Superclasses: matlab.unittest.plugins.OutputStream

Output stream to display text information to screen

Description

The ToStandardOutput class creates an output stream to display text output to the screen. Many plugins that accept an output stream use ToStandardOutput as their default stream.

Construction

matlab.unittest.plugins.ToStandardOutput creates an OutputStream that prints text output to the screen.

Copy Semantics

Handle. To learn how handle classes affect copy operations, see Copying Objects in the MATLAB® documentation.

Examples

expand all

Direct Plugin Output Text to Standard Output

In your working folder, create the file ExampleTest.m containing the following test class.

classdef ExampleTest < matlab.unittest.TestCase
    methods(Test)
        function testOne(testCase)  % Test fails
            testCase.verifyEqual(5, 4, 'Testing 5==4')
        end
        function testTwo(testCase)  % Test passes
            testCase.verifyEqual(5, 5, 'Testing 5==5')
        end
        function testThree(testCase)
            % test code
        end
    end
end

The verifyEqual qualification in testOne causes a test failure. The qualifications in testOne and testTwo include an instance of a matlab.unittest.diagnostics.StringDiagnostic.

At the command prompt, create a test suite from the ExampleTest class.

import matlab.unittest.TestRunner
import matlab.unittest.TestSuite
import matlab.unittest.plugins.FailureDiagnosticsPlugin
import matlab.unittest.plugins.ToStandardOutput

suite   = TestSuite.fromClass(?ExampleTest);

Create a test runner with no plugins. This code creates a silent runner and provides you with complete control over the installed plugins.

runner = TestRunner.withNoPlugins;

Create a FailureDiagnosticsPlugin that explicitly specifies that its output should go to the screen.

plugin = FailureDiagnosticsPlugin(ToStandardOutput);

Add the plugin to the TestRunner and run the suite.

runner.addPlugin(plugin)
result = runner.run(suite);
================================================================================
Verification failed in ExampleTest/testOne.

    ----------------
    Test Diagnostic:
    ----------------
    Testing 5==4

    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEqual failed.
    --> The values are not equal using "isequaln".
    --> Failure table:
                Index    Actual    Expected    Error    RelativeError
                _____    ______    ________    _____    _____________
            
                1        5         4           1        0.25         
    
    Actual Value:
             5
    Expected Value:
             4

    ------------------
    Stack Information:
    ------------------
    In C:\work\ExampleTest.m (ExampleTest.testOne) at 4
================================================================================
Failure Summary:

     Name                 Failed  Incomplete  Reason(s)
    ==================================================================
     ExampleTest/testOne    X                 Failed by verification.

Only the test failures produce output to the screen.

Was this topic helpful?