Main Content

matlab.automation.streams.ToFile class

Package: matlab.automation.streams
Superclasses: matlab.automation.streams.OutputStream

Output stream to write text to file

Renamed from matlab.unittest.plugins.ToFile in R2023a

Description

The matlab.automation.streams.ToFile class creates an output stream to write text to a UTF-8 encoded file. Whenever text prints to this stream, the output stream opens the file, appends the text, and closes the file.

Construction

stream = matlab.automation.streams.ToFile(fname) creates an output stream to write text to the file fname.

Input Arguments

fname

Name of file to write the text, specified as a character vector or string scalar. If fname exists, the text from the stream is appended to the file.

Properties

Filename

Name of file to redirect text output from the plugin, specified in the input argument, fname.

Copy Semantics

Handle. To learn how handle classes affect copy operations, see Copying Objects.

Examples

collapse all

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

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

import matlab.unittest.TestRunner
import matlab.unittest.TestSuite
import matlab.unittest.plugins.TAPPlugin
import matlab.automation.streams.ToFile

suite = TestSuite.fromClass(?ExampleTest);

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

runner = TestRunner.withTextOutput;

Create a TAPPlugin that explicitly specifies that its output should go to the file, MyTapOutput.tap.

filename = 'MyTapOutput.tap';
plugin = TAPPlugin.producingOriginalFormat(ToFile(filename));

Add the plugin to the TestRunner and run the suite. Only the test failures produce output to the screen. By default, TestRunner.withTextOutput uses a DiagnosticsOutputPlugin instance to display output on the screen.

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

================================================================================
Verification failed in ExampleTest/testOne.
    ----------------
    Test Diagnostic:
    ----------------
    Testing 5==4
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEqual failed.
    --> The numeric values are not equal using "isequaln".
    --> Failure table:
            Actual    Expected    Error    RelativeError
            ______    ________    _____    _____________
                                                        
              5          4          1          0.25     
    
    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.

Observe contents in the file created by the plugin.

disp(fileread(filename))
1..3
not ok 1 - ExampleTest/testOne
# ================================================================================
# Verification failed in ExampleTest/testOne.
#     ----------------
#     Test Diagnostic:
#     ----------------
#     Testing 5==4
#     ---------------------
#     Framework Diagnostic:
#     ---------------------
#     verifyEqual failed.
#     --> The numeric values are not equal using "isequaln".
#     --> Failure table:
#             Actual    Expected    Error    RelativeError
#             ______    ________    _____    _____________
#                                                         
#               5          4          1          0.25     
#     
#     Actual Value:
#          5
#     Expected Value:
#          4
#     ------------------
#     Stack Information:
#     ------------------
#     In C:\work\ExampleTest.m (ExampleTest.testOne) at 4
# ================================================================================
ok 2 - ExampleTest/testTwo
ok 3 - ExampleTest/testThree

Extended Capabilities

Version History

Introduced in R2014a

expand all