Documentation Center

  • Trial Software
  • Product Updates

matlab.unittest.plugins.OutputStream class

Package: matlab.unittest.plugins

Interface that determines where to send text output

Description

The OutputStream interface is an abstract interface class that you can use as a base class to specify where plugins direct their text output. To create a custom output stream, implement a print method that correctly handles the formatted text information the testing framework passes to it. Many text-oriented plugins accept an OutputStream to redirect the text they produce in a configurable manner.

Methods

printPrint string to output stream

Copy Semantics

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

Examples

expand all

Create Custom Output Stream

In a file in your working directory, create a new output stream class in the file ToFigure.m. This class allows plugin output to be redirected to a figure.

classdef ToFigure < matlab.unittest.plugins.OutputStream
    
    properties(SetAccess=private)
        Figure
    end
    properties(Dependent, Access=private)
        CurrentText
    end
    properties(Access=private)
        ListBox
    end

This class uses three properties. Figure is the figure that receives and displays the output. CurrentText is the current text displayed in the list box and a dependent property that you can use to easily access and set the current text in the list box. ListBox is a handle to the list box that displays the text.

In the same file, add the following methods block.

    methods
        function print(stream, formatSpec, varargin)
            if isempty(stream.Figure) || ~ishghandle(stream.Figure)
                stream.createFigure;
            end
            newStr = sprintf(formatSpec, varargin{:});
            str = sprintf('%s%s', stream.CurrentText, newStr);
            stream.CurrentText = str;
        end
        
        function str = get.CurrentText(stream)
            strCell = cellstr(get(stream.ListBox, 'String'));
            str = strjoin(strCell','\n');
        end
        
        function set.CurrentText(stream, str)
            strCell = strsplit(str, '\n');
            set(stream.ListBox, 'String', strCell, 'ListBoxTop',numel(strCell));
            drawnow;
        end
    end

You must implement the print method for any subclass of OutputStream. In this example, the method creates a new figure (if necessary), formats the incoming text, and then adds it to the output stream. The get.CurrentText function gets the text from the list box on the figure. The set.CurrentText function adds the text onto the list box on the figure.

In the same file, add the following methods block containing a helper function to create the figure.

    methods(Access=private)
        function createFigure(stream)
            stream.Figure = figure;
            stream.ListBox = uicontrol( ...
                'Parent',   stream.Figure, ...
                'Style',    'listbox', ...
                'Units',    'normalized', ...
                'Position',	[.05 .05 .9 .9], ...
                'Max',      2, ...
                'FontSize', 13, ...
                'HorizontalAlignment', 'left');
        end
    end
end

In an new file in your working folder, create 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.TestSuite;
import matlab.unittest.TestRunner;
import matlab.unittest.plugins.DiagnosticsValidationPlugin;

suite   = TestSuite.fromClass(?ExampleTest);

Create a test runner that displays output to the command window.

runner = TestRunner.withTextOutput;

Create a DiagnosticsValidationPlugin that explicitly specifies that its output should go to a figure via the ToFigure output stream.

plugin = DiagnosticsValidationPlugin(ToFigure);

Add the plugin to the TestRunner and run the suite.

runner.addPlugin(plugin);
result = runner.run(suite);
Running ExampleTest

================================================================================
Verification failed in ExampleTest/testOne.

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

    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEqual failed.
    --> The values are not equal using "isequaln".
    
    Actual Value:
             5
    Expected Value:
             4

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

Failure Summary:

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

Only the test failures produce output to the screen. By default, TestRunner.withTextOutput uses a FailureDiagnosticsPlugin to display output on the screen.

In addition to the default text output being displayed on the screen, the DiagnosticsValidationPlugin output is directed to a figure. The figure shows the following text.

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

The DiagnosticsValidationPlugin displays the diagnostic information regardless of whether the tests encounter failure conditions.

See Also

| |

Was this topic helpful?